IF EXISTS (SELECT * FROM dbo.SysObjects WHERE id = object_id(N'dbo.AUDIT_upd_RateLevelRoomTypeRestrictionMulti') AND OBJECTPROPERTY(id, N'IsTrigger') = 1) DROP TRIGGER dbo.AUDIT_upd_RateLevelRoomTypeRestrictionMulti 
GO
CREATE TRIGGER [dbo].[AUDIT_upd_RateLevelRoomTypeRestrictionMulti]
ON [dbo].[RateLevelRoomTypeRestrictionMulti] FOR UPDATE NOT FOR REPLICATION AS
BEGIN
	

	DECLARE 
		@IDENTITY_SAVE			varchar(50),
		@AUDIT_LOG_TRANSACTION_ID	Int,
		@PRIM_KEY			nvarchar(4000),
		@Inserted	    		bit,
 		@ROWS_COUNT				int,
		@FK_UserIdNumber INT,
		@FK_CashBox INT,
		@ProgramPart SMALLINT,
		@TerminalName VARCHAR(50),
		@SystemDateStr VARCHAR(8),
		@SystemDateTime DATETIME

	SET NOCOUNT ON

	Select @ROWS_COUNT=count(*) from inserted
	SET @IDENTITY_SAVE = CAST(IsNull(@@IDENTITY,1) AS varchar(50))

    SELECT TOP 1 @FK_UserIdNumber = FK_UserIdNumber,
	@FK_CashBox = FK_CashBox,
	@ProgramPart = ProgramPart,
	@TerminalName = TerminalName
	FROM PersonnelLogInfo WITH (NOLOCK)
	WHERE spid = @@spid 
	ORDER BY CreateDate DESC

	IF ISNULL(@FK_UserIdNumber, 0) = 0
	  SET @FK_UserIdNumber = 1
	IF ISNULL(@FK_CashBox, -1) <= 0
	  SET @FK_CashBox = -1
	IF ISNULL(@ProgramPart, -1) <= 0
	  SET @ProgramPart = 14 -- ppUnknown from SystemAll
	IF ISNULL(@TerminalName, '') = ''
	  SET @TerminalName = 'Unknown'

    SELECT @SystemDateStr = SystemDate FROM System WITH (NOLOCK) WHERE Number = 1
    SET @SystemDateTime = CONVERT(DATETIME, @SystemDateStr + ' ' + CONVERT(CHAR(8), CURRENT_TIMESTAMP, 108))

	INSERT INTO dbo.AUDIT_LOG_TRANSACTIONS
	(
		TABLE_NAME,
		TABLE_SCHEMA,
		AUDIT_ACTION_ID,
		HOST_NAME,
		APP_NAME,
		MODIFIED_BY,
		MODIFIED_DATE,
		AFFECTED_ROWS,
		FK_UserIdNumber,
		FK_CashBox,
		ProgramPart,
		TerminalName,
		RealDateTime
	)
	values(
		'RateLevelRoomTypeRestrictionMulti',
		'dbo',
		1,	--	ACTION ID For UPDATE
		CASE 
		  WHEN LEN(HOST_NAME()) < 1 THEN ' '
		  ELSE HOST_NAME()
		END,
		CASE 
		  WHEN LEN(APP_NAME()) < 1 THEN ' '
		  ELSE APP_NAME()
		END,
		SUSER_SNAME(),
		@SystemDateTime,
		@ROWS_COUNT,
		@FK_UserIdNumber,
		@FK_CashBox,
		@ProgramPart,
		@TerminalName,
		GETDATE()
	)	
	
	Set @AUDIT_LOG_TRANSACTION_ID = SCOPE_IDENTITY()
	
	SET @Inserted = 0
	
	If UPDATE([FKRateLevelRoomTypeXTabId])
	BEGIN
    
		INSERT
		INTO dbo.AUDIT_LOG_DATA
		(
			AUDIT_LOG_TRANSACTION_ID,
			PRIMARY_KEY_DATA,
			COL_NAME,
			OLD_VALUE_LONG,
			NEW_VALUE_LONG,
			DATA_TYPE
			, KEY1, KEY2
		)
		SELECT
			@AUDIT_LOG_TRANSACTION_ID,
		    convert(nvarchar(1500), IsNull('[FKRateLevelRoomTypeXTabId]='+CONVERT(nvarchar(4000), IsNull(OLD.[FKRateLevelRoomTypeXTabId], NEW.[FKRateLevelRoomTypeXTabId]), 0), '[FKRateLevelRoomTypeXTabId] Is Null')+' AND '+IsNull('[DateStamp]=N'''+replace(CONVERT(nvarchar(4000), IsNull(OLD.[DateStamp], NEW.[DateStamp]), 121),'''' ,'''''')+'''', '[DateStamp] Is Null')),
		    'FKRateLevelRoomTypeXTabId',
			CONVERT(nvarchar(4000), OLD.[FKRateLevelRoomTypeXTabId], 0),
			CONVERT(nvarchar(4000), NEW.[FKRateLevelRoomTypeXTabId], 0),
			'A'
			, IsNULL( CONVERT(nvarchar(500), CONVERT(nvarchar(4000), OLD.[FKRateLevelRoomTypeXTabId], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[FKRateLevelRoomTypeXTabId], 0))), IsNULL( CONVERT(nvarchar(500), CONVERT(nvarchar(4000), OLD.[DateStamp], 121)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[DateStamp], 121)))
			
		FROM deleted OLD Full Outer Join inserted NEW On
			(CONVERT(nvarchar(4000), NEW.[FKRateLevelRoomTypeXTabId], 0)=CONVERT(nvarchar(4000), OLD.[FKRateLevelRoomTypeXTabId], 0) or (NEW.[FKRateLevelRoomTypeXTabId] Is Null and OLD.[FKRateLevelRoomTypeXTabId] Is Null)) AND (CONVERT(nvarchar(4000), NEW.[DateStamp], 121)=CONVERT(nvarchar(4000), OLD.[DateStamp], 121) or (NEW.[DateStamp] Is Null and OLD.[DateStamp] Is Null))
			WHERE (
			
			
				(
					NEW.[FKRateLevelRoomTypeXTabId] <>
					OLD.[FKRateLevelRoomTypeXTabId]
				) Or
			
				(
					NEW.[FKRateLevelRoomTypeXTabId] Is Null And
					OLD.[FKRateLevelRoomTypeXTabId] Is Not Null
				) Or
				(
					NEW.[FKRateLevelRoomTypeXTabId] Is Not Null And
					OLD.[FKRateLevelRoomTypeXTabId] Is Null
				)
				)
        
		SET @Inserted = CASE WHEN @@ROWCOUNT > 0 Then 1 Else @Inserted End
	END
	
	If UPDATE([DateStamp])
	BEGIN
    
		INSERT
		INTO dbo.AUDIT_LOG_DATA
		(
			AUDIT_LOG_TRANSACTION_ID,
			PRIMARY_KEY_DATA,
			COL_NAME,
			OLD_VALUE_LONG,
			NEW_VALUE_LONG,
			DATA_TYPE
			, KEY1, KEY2
		)
		SELECT
			@AUDIT_LOG_TRANSACTION_ID,
		    convert(nvarchar(1500), IsNull('[FKRateLevelRoomTypeXTabId]='+CONVERT(nvarchar(4000), IsNull(OLD.[FKRateLevelRoomTypeXTabId], NEW.[FKRateLevelRoomTypeXTabId]), 0), '[FKRateLevelRoomTypeXTabId] Is Null')+' AND '+IsNull('[DateStamp]=N'''+replace(CONVERT(nvarchar(4000), IsNull(OLD.[DateStamp], NEW.[DateStamp]), 121),'''' ,'''''')+'''', '[DateStamp] Is Null')),
		    'DateStamp',
			CONVERT(nvarchar(4000), OLD.[DateStamp], 121),
			CONVERT(nvarchar(4000), NEW.[DateStamp], 121),
			'A'
			, IsNULL( CONVERT(nvarchar(500), CONVERT(nvarchar(4000), OLD.[FKRateLevelRoomTypeXTabId], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[FKRateLevelRoomTypeXTabId], 0))), IsNULL( CONVERT(nvarchar(500), CONVERT(nvarchar(4000), OLD.[DateStamp], 121)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[DateStamp], 121)))
			
		FROM deleted OLD Full Outer Join inserted NEW On
			(CONVERT(nvarchar(4000), NEW.[FKRateLevelRoomTypeXTabId], 0)=CONVERT(nvarchar(4000), OLD.[FKRateLevelRoomTypeXTabId], 0) or (NEW.[FKRateLevelRoomTypeXTabId] Is Null and OLD.[FKRateLevelRoomTypeXTabId] Is Null)) AND (CONVERT(nvarchar(4000), NEW.[DateStamp], 121)=CONVERT(nvarchar(4000), OLD.[DateStamp], 121) or (NEW.[DateStamp] Is Null and OLD.[DateStamp] Is Null))
			WHERE (
			
			
				(
					NEW.[DateStamp] <>
					OLD.[DateStamp]
				) Or
			
				(
					NEW.[DateStamp] Is Null And
					OLD.[DateStamp] Is Not Null
				) Or
				(
					NEW.[DateStamp] Is Not Null And
					OLD.[DateStamp] Is Null
				)
				)
        
		SET @Inserted = CASE WHEN @@ROWCOUNT > 0 Then 1 Else @Inserted End
	END
	
	If UPDATE([StopSell])
	BEGIN
    
		INSERT
		INTO dbo.AUDIT_LOG_DATA
		(
			AUDIT_LOG_TRANSACTION_ID,
			PRIMARY_KEY_DATA,
			COL_NAME,
			OLD_VALUE_LONG,
			NEW_VALUE_LONG,
			DATA_TYPE
			, KEY1, KEY2
		)
		SELECT
			@AUDIT_LOG_TRANSACTION_ID,
		    convert(nvarchar(1500), IsNull('[FKRateLevelRoomTypeXTabId]='+CONVERT(nvarchar(4000), IsNull(OLD.[FKRateLevelRoomTypeXTabId], NEW.[FKRateLevelRoomTypeXTabId]), 0), '[FKRateLevelRoomTypeXTabId] Is Null')+' AND '+IsNull('[DateStamp]=N'''+replace(CONVERT(nvarchar(4000), IsNull(OLD.[DateStamp], NEW.[DateStamp]), 121),'''' ,'''''')+'''', '[DateStamp] Is Null')),
		    'StopSell',
			CONVERT(nvarchar(4000), OLD.[StopSell], 0),
			CONVERT(nvarchar(4000), NEW.[StopSell], 0),
			'A'
			, IsNULL( CONVERT(nvarchar(500), CONVERT(nvarchar(4000), OLD.[FKRateLevelRoomTypeXTabId], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[FKRateLevelRoomTypeXTabId], 0))), IsNULL( CONVERT(nvarchar(500), CONVERT(nvarchar(4000), OLD.[DateStamp], 121)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[DateStamp], 121)))
			
		FROM deleted OLD Inner Join inserted NEW On 
			(CONVERT(nvarchar(4000), NEW.[FKRateLevelRoomTypeXTabId], 0)=CONVERT(nvarchar(4000), OLD.[FKRateLevelRoomTypeXTabId], 0) or (NEW.[FKRateLevelRoomTypeXTabId] Is Null and OLD.[FKRateLevelRoomTypeXTabId] Is Null)) AND (CONVERT(nvarchar(4000), NEW.[DateStamp], 121)=CONVERT(nvarchar(4000), OLD.[DateStamp], 121) or (NEW.[DateStamp] Is Null and OLD.[DateStamp] Is Null))
			where (
			
			
				(
					NEW.[StopSell] <>
					OLD.[StopSell]
				) Or
			
				(
					NEW.[StopSell] Is Null And
					OLD.[StopSell] Is Not Null
				) Or
				(
					NEW.[StopSell] Is Not Null And
					OLD.[StopSell] Is Null
				)
				)
        
		SET @Inserted = CASE WHEN @@ROWCOUNT > 0 Then 1 Else @Inserted End
	END
	
	If UPDATE([SoftClose])
	BEGIN
    
		INSERT
		INTO dbo.AUDIT_LOG_DATA
		(
			AUDIT_LOG_TRANSACTION_ID,
			PRIMARY_KEY_DATA,
			COL_NAME,
			OLD_VALUE_LONG,
			NEW_VALUE_LONG,
			DATA_TYPE
			, KEY1, KEY2
		)
		SELECT
			@AUDIT_LOG_TRANSACTION_ID,
		    convert(nvarchar(1500), IsNull('[FKRateLevelRoomTypeXTabId]='+CONVERT(nvarchar(4000), IsNull(OLD.[FKRateLevelRoomTypeXTabId], NEW.[FKRateLevelRoomTypeXTabId]), 0), '[FKRateLevelRoomTypeXTabId] Is Null')+' AND '+IsNull('[DateStamp]=N'''+replace(CONVERT(nvarchar(4000), IsNull(OLD.[DateStamp], NEW.[DateStamp]), 121),'''' ,'''''')+'''', '[DateStamp] Is Null')),
		    'SoftClose',
			CONVERT(nvarchar(4000), OLD.[SoftClose], 0),
			CONVERT(nvarchar(4000), NEW.[SoftClose], 0),
			'A'
			, IsNULL( CONVERT(nvarchar(500), CONVERT(nvarchar(4000), OLD.[FKRateLevelRoomTypeXTabId], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[FKRateLevelRoomTypeXTabId], 0))), IsNULL( CONVERT(nvarchar(500), CONVERT(nvarchar(4000), OLD.[DateStamp], 121)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[DateStamp], 121)))
			
		FROM deleted OLD Inner Join inserted NEW On 
			(CONVERT(nvarchar(4000), NEW.[FKRateLevelRoomTypeXTabId], 0)=CONVERT(nvarchar(4000), OLD.[FKRateLevelRoomTypeXTabId], 0) or (NEW.[FKRateLevelRoomTypeXTabId] Is Null and OLD.[FKRateLevelRoomTypeXTabId] Is Null)) AND (CONVERT(nvarchar(4000), NEW.[DateStamp], 121)=CONVERT(nvarchar(4000), OLD.[DateStamp], 121) or (NEW.[DateStamp] Is Null and OLD.[DateStamp] Is Null))
			where (
			
			
				(
					NEW.[SoftClose] <>
					OLD.[SoftClose]
				) Or
			
				(
					NEW.[SoftClose] Is Null And
					OLD.[SoftClose] Is Not Null
				) Or
				(
					NEW.[SoftClose] Is Not Null And
					OLD.[SoftClose] Is Null
				)
				)
        
		SET @Inserted = CASE WHEN @@ROWCOUNT > 0 Then 1 Else @Inserted End
	END
	
	If UPDATE([CTA])
	BEGIN
    
		INSERT
		INTO dbo.AUDIT_LOG_DATA
		(
			AUDIT_LOG_TRANSACTION_ID,
			PRIMARY_KEY_DATA,
			COL_NAME,
			OLD_VALUE_LONG,
			NEW_VALUE_LONG,
			DATA_TYPE
			, KEY1, KEY2
		)
		SELECT
			@AUDIT_LOG_TRANSACTION_ID,
		    convert(nvarchar(1500), IsNull('[FKRateLevelRoomTypeXTabId]='+CONVERT(nvarchar(4000), IsNull(OLD.[FKRateLevelRoomTypeXTabId], NEW.[FKRateLevelRoomTypeXTabId]), 0), '[FKRateLevelRoomTypeXTabId] Is Null')+' AND '+IsNull('[DateStamp]=N'''+replace(CONVERT(nvarchar(4000), IsNull(OLD.[DateStamp], NEW.[DateStamp]), 121),'''' ,'''''')+'''', '[DateStamp] Is Null')),
		    'CTA',
			CONVERT(nvarchar(4000), OLD.[CTA], 0),
			CONVERT(nvarchar(4000), NEW.[CTA], 0),
			'A'
			, IsNULL( CONVERT(nvarchar(500), CONVERT(nvarchar(4000), OLD.[FKRateLevelRoomTypeXTabId], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[FKRateLevelRoomTypeXTabId], 0))), IsNULL( CONVERT(nvarchar(500), CONVERT(nvarchar(4000), OLD.[DateStamp], 121)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[DateStamp], 121)))
			
		FROM deleted OLD Inner Join inserted NEW On 
			(CONVERT(nvarchar(4000), NEW.[FKRateLevelRoomTypeXTabId], 0)=CONVERT(nvarchar(4000), OLD.[FKRateLevelRoomTypeXTabId], 0) or (NEW.[FKRateLevelRoomTypeXTabId] Is Null and OLD.[FKRateLevelRoomTypeXTabId] Is Null)) AND (CONVERT(nvarchar(4000), NEW.[DateStamp], 121)=CONVERT(nvarchar(4000), OLD.[DateStamp], 121) or (NEW.[DateStamp] Is Null and OLD.[DateStamp] Is Null))
			where (
			
			
				(
					NEW.[CTA] <>
					OLD.[CTA]
				) Or
			
				(
					NEW.[CTA] Is Null And
					OLD.[CTA] Is Not Null
				) Or
				(
					NEW.[CTA] Is Not Null And
					OLD.[CTA] Is Null
				)
				)
        
		SET @Inserted = CASE WHEN @@ROWCOUNT > 0 Then 1 Else @Inserted End
	END
	
	If UPDATE([CTD])
	BEGIN
    
		INSERT
		INTO dbo.AUDIT_LOG_DATA
		(
			AUDIT_LOG_TRANSACTION_ID,
			PRIMARY_KEY_DATA,
			COL_NAME,
			OLD_VALUE_LONG,
			NEW_VALUE_LONG,
			DATA_TYPE
			, KEY1, KEY2
		)
		SELECT
			@AUDIT_LOG_TRANSACTION_ID,
		    convert(nvarchar(1500), IsNull('[FKRateLevelRoomTypeXTabId]='+CONVERT(nvarchar(4000), IsNull(OLD.[FKRateLevelRoomTypeXTabId], NEW.[FKRateLevelRoomTypeXTabId]), 0), '[FKRateLevelRoomTypeXTabId] Is Null')+' AND '+IsNull('[DateStamp]=N'''+replace(CONVERT(nvarchar(4000), IsNull(OLD.[DateStamp], NEW.[DateStamp]), 121),'''' ,'''''')+'''', '[DateStamp] Is Null')),
		    'CTD',
			CONVERT(nvarchar(4000), OLD.[CTD], 0),
			CONVERT(nvarchar(4000), NEW.[CTD], 0),
			'A'
			, IsNULL( CONVERT(nvarchar(500), CONVERT(nvarchar(4000), OLD.[FKRateLevelRoomTypeXTabId], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[FKRateLevelRoomTypeXTabId], 0))), IsNULL( CONVERT(nvarchar(500), CONVERT(nvarchar(4000), OLD.[DateStamp], 121)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[DateStamp], 121)))
			
		FROM deleted OLD Inner Join inserted NEW On 
			(CONVERT(nvarchar(4000), NEW.[FKRateLevelRoomTypeXTabId], 0)=CONVERT(nvarchar(4000), OLD.[FKRateLevelRoomTypeXTabId], 0) or (NEW.[FKRateLevelRoomTypeXTabId] Is Null and OLD.[FKRateLevelRoomTypeXTabId] Is Null)) AND (CONVERT(nvarchar(4000), NEW.[DateStamp], 121)=CONVERT(nvarchar(4000), OLD.[DateStamp], 121) or (NEW.[DateStamp] Is Null and OLD.[DateStamp] Is Null))
			where (
			
			
				(
					NEW.[CTD] <>
					OLD.[CTD]
				) Or
			
				(
					NEW.[CTD] Is Null And
					OLD.[CTD] Is Not Null
				) Or
				(
					NEW.[CTD] Is Not Null And
					OLD.[CTD] Is Null
				)
				)
        
		SET @Inserted = CASE WHEN @@ROWCOUNT > 0 Then 1 Else @Inserted End
	END
	
	If UPDATE([MLOS])
	BEGIN
    
		INSERT
		INTO dbo.AUDIT_LOG_DATA
		(
			AUDIT_LOG_TRANSACTION_ID,
			PRIMARY_KEY_DATA,
			COL_NAME,
			OLD_VALUE_LONG,
			NEW_VALUE_LONG,
			DATA_TYPE
			, KEY1, KEY2
		)
		SELECT
			@AUDIT_LOG_TRANSACTION_ID,
		    convert(nvarchar(1500), IsNull('[FKRateLevelRoomTypeXTabId]='+CONVERT(nvarchar(4000), IsNull(OLD.[FKRateLevelRoomTypeXTabId], NEW.[FKRateLevelRoomTypeXTabId]), 0), '[FKRateLevelRoomTypeXTabId] Is Null')+' AND '+IsNull('[DateStamp]=N'''+replace(CONVERT(nvarchar(4000), IsNull(OLD.[DateStamp], NEW.[DateStamp]), 121),'''' ,'''''')+'''', '[DateStamp] Is Null')),
		    'MLOS',
			CONVERT(nvarchar(4000), OLD.[MLOS], 0),
			CONVERT(nvarchar(4000), NEW.[MLOS], 0),
			'A'
			, IsNULL( CONVERT(nvarchar(500), CONVERT(nvarchar(4000), OLD.[FKRateLevelRoomTypeXTabId], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[FKRateLevelRoomTypeXTabId], 0))), IsNULL( CONVERT(nvarchar(500), CONVERT(nvarchar(4000), OLD.[DateStamp], 121)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[DateStamp], 121)))
			
		FROM deleted OLD Inner Join inserted NEW On 
			(CONVERT(nvarchar(4000), NEW.[FKRateLevelRoomTypeXTabId], 0)=CONVERT(nvarchar(4000), OLD.[FKRateLevelRoomTypeXTabId], 0) or (NEW.[FKRateLevelRoomTypeXTabId] Is Null and OLD.[FKRateLevelRoomTypeXTabId] Is Null)) AND (CONVERT(nvarchar(4000), NEW.[DateStamp], 121)=CONVERT(nvarchar(4000), OLD.[DateStamp], 121) or (NEW.[DateStamp] Is Null and OLD.[DateStamp] Is Null))
			where (
			
			
				(
					NEW.[MLOS] <>
					OLD.[MLOS]
				) Or
			
				(
					NEW.[MLOS] Is Null And
					OLD.[MLOS] Is Not Null
				) Or
				(
					NEW.[MLOS] Is Not Null And
					OLD.[MLOS] Is Null
				)
				)
        
		SET @Inserted = CASE WHEN @@ROWCOUNT > 0 Then 1 Else @Inserted End
	END
	
	If UPDATE([MaxLOS])
	BEGIN
    
		INSERT
		INTO dbo.AUDIT_LOG_DATA
		(
			AUDIT_LOG_TRANSACTION_ID,
			PRIMARY_KEY_DATA,
			COL_NAME,
			OLD_VALUE_LONG,
			NEW_VALUE_LONG,
			DATA_TYPE
			, KEY1, KEY2
		)
		SELECT
			@AUDIT_LOG_TRANSACTION_ID,
		    convert(nvarchar(1500), IsNull('[FKRateLevelRoomTypeXTabId]='+CONVERT(nvarchar(4000), IsNull(OLD.[FKRateLevelRoomTypeXTabId], NEW.[FKRateLevelRoomTypeXTabId]), 0), '[FKRateLevelRoomTypeXTabId] Is Null')+' AND '+IsNull('[DateStamp]=N'''+replace(CONVERT(nvarchar(4000), IsNull(OLD.[DateStamp], NEW.[DateStamp]), 121),'''' ,'''''')+'''', '[DateStamp] Is Null')),
		    'MaxLOS',
			CONVERT(nvarchar(4000), OLD.[MaxLOS], 0),
			CONVERT(nvarchar(4000), NEW.[MaxLOS], 0),
			'A'
			, IsNULL( CONVERT(nvarchar(500), CONVERT(nvarchar(4000), OLD.[FKRateLevelRoomTypeXTabId], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[FKRateLevelRoomTypeXTabId], 0))), IsNULL( CONVERT(nvarchar(500), CONVERT(nvarchar(4000), OLD.[DateStamp], 121)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[DateStamp], 121)))
			
		FROM deleted OLD Inner Join inserted NEW On 
			(CONVERT(nvarchar(4000), NEW.[FKRateLevelRoomTypeXTabId], 0)=CONVERT(nvarchar(4000), OLD.[FKRateLevelRoomTypeXTabId], 0) or (NEW.[FKRateLevelRoomTypeXTabId] Is Null and OLD.[FKRateLevelRoomTypeXTabId] Is Null)) AND (CONVERT(nvarchar(4000), NEW.[DateStamp], 121)=CONVERT(nvarchar(4000), OLD.[DateStamp], 121) or (NEW.[DateStamp] Is Null and OLD.[DateStamp] Is Null))
			where (
			
			
				(
					NEW.[MaxLOS] <>
					OLD.[MaxLOS]
				) Or
			
				(
					NEW.[MaxLOS] Is Null And
					OLD.[MaxLOS] Is Not Null
				) Or
				(
					NEW.[MaxLOS] Is Not Null And
					OLD.[MaxLOS] Is Null
				)
				)
        
		SET @Inserted = CASE WHEN @@ROWCOUNT > 0 Then 1 Else @Inserted End
	END
	
	If UPDATE([ExactLOS])
	BEGIN
    
		INSERT
		INTO dbo.AUDIT_LOG_DATA
		(
			AUDIT_LOG_TRANSACTION_ID,
			PRIMARY_KEY_DATA,
			COL_NAME,
			OLD_VALUE_LONG,
			NEW_VALUE_LONG,
			DATA_TYPE
			, KEY1, KEY2
		)
		SELECT
			@AUDIT_LOG_TRANSACTION_ID,
		    convert(nvarchar(1500), IsNull('[FKRateLevelRoomTypeXTabId]='+CONVERT(nvarchar(4000), IsNull(OLD.[FKRateLevelRoomTypeXTabId], NEW.[FKRateLevelRoomTypeXTabId]), 0), '[FKRateLevelRoomTypeXTabId] Is Null')+' AND '+IsNull('[DateStamp]=N'''+replace(CONVERT(nvarchar(4000), IsNull(OLD.[DateStamp], NEW.[DateStamp]), 121),'''' ,'''''')+'''', '[DateStamp] Is Null')),
		    'ExactLOS',
			CONVERT(nvarchar(4000), OLD.[ExactLOS], 0),
			CONVERT(nvarchar(4000), NEW.[ExactLOS], 0),
			'A'
			, IsNULL( CONVERT(nvarchar(500), CONVERT(nvarchar(4000), OLD.[FKRateLevelRoomTypeXTabId], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[FKRateLevelRoomTypeXTabId], 0))), IsNULL( CONVERT(nvarchar(500), CONVERT(nvarchar(4000), OLD.[DateStamp], 121)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[DateStamp], 121)))
			
		FROM deleted OLD Inner Join inserted NEW On 
			(CONVERT(nvarchar(4000), NEW.[FKRateLevelRoomTypeXTabId], 0)=CONVERT(nvarchar(4000), OLD.[FKRateLevelRoomTypeXTabId], 0) or (NEW.[FKRateLevelRoomTypeXTabId] Is Null and OLD.[FKRateLevelRoomTypeXTabId] Is Null)) AND (CONVERT(nvarchar(4000), NEW.[DateStamp], 121)=CONVERT(nvarchar(4000), OLD.[DateStamp], 121) or (NEW.[DateStamp] Is Null and OLD.[DateStamp] Is Null))
			where (
			
			
				(
					NEW.[ExactLOS] <>
					OLD.[ExactLOS]
				) Or
			
				(
					NEW.[ExactLOS] Is Null And
					OLD.[ExactLOS] Is Not Null
				) Or
				(
					NEW.[ExactLOS] Is Not Null And
					OLD.[ExactLOS] Is Null
				)
				)
        
		SET @Inserted = CASE WHEN @@ROWCOUNT > 0 Then 1 Else @Inserted End
	END
	
	If UPDATE([ReleaseDays])
	BEGIN
    
		INSERT
		INTO dbo.AUDIT_LOG_DATA
		(
			AUDIT_LOG_TRANSACTION_ID,
			PRIMARY_KEY_DATA,
			COL_NAME,
			OLD_VALUE_LONG,
			NEW_VALUE_LONG,
			DATA_TYPE
			, KEY1, KEY2
		)
		SELECT
			@AUDIT_LOG_TRANSACTION_ID,
		    convert(nvarchar(1500), IsNull('[FKRateLevelRoomTypeXTabId]='+CONVERT(nvarchar(4000), IsNull(OLD.[FKRateLevelRoomTypeXTabId], NEW.[FKRateLevelRoomTypeXTabId]), 0), '[FKRateLevelRoomTypeXTabId] Is Null')+' AND '+IsNull('[DateStamp]=N'''+replace(CONVERT(nvarchar(4000), IsNull(OLD.[DateStamp], NEW.[DateStamp]), 121),'''' ,'''''')+'''', '[DateStamp] Is Null')),
		    'ReleaseDays',
			CONVERT(nvarchar(4000), OLD.[ReleaseDays], 0),
			CONVERT(nvarchar(4000), NEW.[ReleaseDays], 0),
			'A'
			, IsNULL( CONVERT(nvarchar(500), CONVERT(nvarchar(4000), OLD.[FKRateLevelRoomTypeXTabId], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[FKRateLevelRoomTypeXTabId], 0))), IsNULL( CONVERT(nvarchar(500), CONVERT(nvarchar(4000), OLD.[DateStamp], 121)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[DateStamp], 121)))
			
		FROM deleted OLD Inner Join inserted NEW On 
			(CONVERT(nvarchar(4000), NEW.[FKRateLevelRoomTypeXTabId], 0)=CONVERT(nvarchar(4000), OLD.[FKRateLevelRoomTypeXTabId], 0) or (NEW.[FKRateLevelRoomTypeXTabId] Is Null and OLD.[FKRateLevelRoomTypeXTabId] Is Null)) AND (CONVERT(nvarchar(4000), NEW.[DateStamp], 121)=CONVERT(nvarchar(4000), OLD.[DateStamp], 121) or (NEW.[DateStamp] Is Null and OLD.[DateStamp] Is Null))
			where (
			
			
				(
					NEW.[ReleaseDays] <>
					OLD.[ReleaseDays]
				) Or
			
				(
					NEW.[ReleaseDays] Is Null And
					OLD.[ReleaseDays] Is Not Null
				) Or
				(
					NEW.[ReleaseDays] Is Not Null And
					OLD.[ReleaseDays] Is Null
				)
				)
        
		SET @Inserted = CASE WHEN @@ROWCOUNT > 0 Then 1 Else @Inserted End
	END
	
	If UPDATE([CutOffDays])
	BEGIN
    
		INSERT
		INTO dbo.AUDIT_LOG_DATA
		(
			AUDIT_LOG_TRANSACTION_ID,
			PRIMARY_KEY_DATA,
			COL_NAME,
			OLD_VALUE_LONG,
			NEW_VALUE_LONG,
			DATA_TYPE
			, KEY1, KEY2
		)
		SELECT
			@AUDIT_LOG_TRANSACTION_ID,
		    convert(nvarchar(1500), IsNull('[FKRateLevelRoomTypeXTabId]='+CONVERT(nvarchar(4000), IsNull(OLD.[FKRateLevelRoomTypeXTabId], NEW.[FKRateLevelRoomTypeXTabId]), 0), '[FKRateLevelRoomTypeXTabId] Is Null')+' AND '+IsNull('[DateStamp]=N'''+replace(CONVERT(nvarchar(4000), IsNull(OLD.[DateStamp], NEW.[DateStamp]), 121),'''' ,'''''')+'''', '[DateStamp] Is Null')),
		    'CutOffDays',
			CONVERT(nvarchar(4000), OLD.[CutOffDays], 0),
			CONVERT(nvarchar(4000), NEW.[CutOffDays], 0),
			'A'
			, IsNULL( CONVERT(nvarchar(500), CONVERT(nvarchar(4000), OLD.[FKRateLevelRoomTypeXTabId], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[FKRateLevelRoomTypeXTabId], 0))), IsNULL( CONVERT(nvarchar(500), CONVERT(nvarchar(4000), OLD.[DateStamp], 121)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[DateStamp], 121)))
			
		FROM deleted OLD Inner Join inserted NEW On 
			(CONVERT(nvarchar(4000), NEW.[FKRateLevelRoomTypeXTabId], 0)=CONVERT(nvarchar(4000), OLD.[FKRateLevelRoomTypeXTabId], 0) or (NEW.[FKRateLevelRoomTypeXTabId] Is Null and OLD.[FKRateLevelRoomTypeXTabId] Is Null)) AND (CONVERT(nvarchar(4000), NEW.[DateStamp], 121)=CONVERT(nvarchar(4000), OLD.[DateStamp], 121) or (NEW.[DateStamp] Is Null and OLD.[DateStamp] Is Null))
			where (
			
			
				(
					NEW.[CutOffDays] <>
					OLD.[CutOffDays]
				) Or
			
				(
					NEW.[CutOffDays] Is Null And
					OLD.[CutOffDays] Is Not Null
				) Or
				(
					NEW.[CutOffDays] Is Not Null And
					OLD.[CutOffDays] Is Null
				)
				)
        
		SET @Inserted = CASE WHEN @@ROWCOUNT > 0 Then 1 Else @Inserted End
	END
	
	-- Watch
	
	-- Lookup
	
	IF @Inserted = 0
	BEGIN
		DELETE FROM dbo.AUDIT_LOG_TRANSACTIONS WHERE AUDIT_LOG_TRANSACTION_ID = @AUDIT_LOG_TRANSACTION_ID
	END
	-- Restore @@IDENTITY Value  
    DECLARE @maxprec AS varchar(2)
    SET @maxprec=CAST(@@MAX_PRECISION as varchar(2))
    EXEC('SELECT IDENTITY(decimal('+@maxprec+',0),'+@IDENTITY_SAVE+',1) id INTO #tmp')
End
