IF EXISTS (SELECT * FROM dbo.SysObjects WHERE id = object_id(N'dbo.AUDIT_upd_ReservationConferenceLine') AND OBJECTPROPERTY(id, N'IsTrigger') = 1) DROP TRIGGER dbo.AUDIT_upd_ReservationConferenceLine 
GO
CREATE TRIGGER [dbo].[AUDIT_upd_ReservationConferenceLine]
ON [dbo].[ReservationConferenceLine] 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(
		'ReservationConferenceLine',
		'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([ReservationConfLineNumber])
	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
		)
		SELECT
			@AUDIT_LOG_TRANSACTION_ID,
		    convert(nvarchar(1500), IsNull('[ReservationConfLineNumber]='+CONVERT(nvarchar(4000), IsNull(OLD.[ReservationConfLineNumber], NEW.[ReservationConfLineNumber]), 0), '[ReservationConfLineNumber] Is Null')),
		    'ReservationConfLineNumber',
			CONVERT(nvarchar(4000), OLD.[ReservationConfLineNumber], 0),
			CONVERT(nvarchar(4000), NEW.[ReservationConfLineNumber], 0),
			'A'
			, IsNULL( CONVERT(nvarchar(500), CONVERT(nvarchar(4000), OLD.[ReservationConfLineNumber], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[ReservationConfLineNumber], 0)))
			
		FROM deleted OLD Full Outer Join inserted NEW On
			(CONVERT(nvarchar(4000), NEW.[ReservationConfLineNumber], 0)=CONVERT(nvarchar(4000), OLD.[ReservationConfLineNumber], 0) or (NEW.[ReservationConfLineNumber] Is Null and OLD.[ReservationConfLineNumber] Is Null))
			WHERE (
			
			
				(
					NEW.[ReservationConfLineNumber] <>
					OLD.[ReservationConfLineNumber]
				) Or
			
				(
					NEW.[ReservationConfLineNumber] Is Null And
					OLD.[ReservationConfLineNumber] Is Not Null
				) Or
				(
					NEW.[ReservationConfLineNumber] Is Not Null And
					OLD.[ReservationConfLineNumber] Is Null
				)
				)
        
		SET @Inserted = CASE WHEN @@ROWCOUNT > 0 Then 1 Else @Inserted End
	END
	
	If UPDATE([BookedConferenceRoomTypeNumber])
	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
		)
		SELECT
			@AUDIT_LOG_TRANSACTION_ID,
		    convert(nvarchar(1500), IsNull('[ReservationConfLineNumber]='+CONVERT(nvarchar(4000), IsNull(OLD.[ReservationConfLineNumber], NEW.[ReservationConfLineNumber]), 0), '[ReservationConfLineNumber] Is Null')),
		    'BookedConferenceRoomTypeNumber',
			CONVERT(nvarchar(4000), OLD.[BookedConferenceRoomTypeNumber], 0),
			CONVERT(nvarchar(4000), NEW.[BookedConferenceRoomTypeNumber], 0),
			'A'
			, IsNULL( CONVERT(nvarchar(500), CONVERT(nvarchar(4000), OLD.[ReservationConfLineNumber], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[ReservationConfLineNumber], 0)))
			
		FROM deleted OLD Inner Join inserted NEW On 
			(CONVERT(nvarchar(4000), NEW.[ReservationConfLineNumber], 0)=CONVERT(nvarchar(4000), OLD.[ReservationConfLineNumber], 0) or (NEW.[ReservationConfLineNumber] Is Null and OLD.[ReservationConfLineNumber] Is Null))
			where (
			
			
				(
					NEW.[BookedConferenceRoomTypeNumber] <>
					OLD.[BookedConferenceRoomTypeNumber]
				) Or
			
				(
					NEW.[BookedConferenceRoomTypeNumber] Is Null And
					OLD.[BookedConferenceRoomTypeNumber] Is Not Null
				) Or
				(
					NEW.[BookedConferenceRoomTypeNumber] Is Not Null And
					OLD.[BookedConferenceRoomTypeNumber] Is Null
				)
				)
        
		SET @Inserted = CASE WHEN @@ROWCOUNT > 0 Then 1 Else @Inserted End
	END
	
	If UPDATE([ArrivalDate])
	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
		)
		SELECT
			@AUDIT_LOG_TRANSACTION_ID,
		    convert(nvarchar(1500), IsNull('[ReservationConfLineNumber]='+CONVERT(nvarchar(4000), IsNull(OLD.[ReservationConfLineNumber], NEW.[ReservationConfLineNumber]), 0), '[ReservationConfLineNumber] Is Null')),
		    'ArrivalDate',
			CONVERT(nvarchar(4000), OLD.[ArrivalDate], 0),
			CONVERT(nvarchar(4000), NEW.[ArrivalDate], 0),
			'A'
			, IsNULL( CONVERT(nvarchar(500), CONVERT(nvarchar(4000), OLD.[ReservationConfLineNumber], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[ReservationConfLineNumber], 0)))
			
		FROM deleted OLD Inner Join inserted NEW On 
			(CONVERT(nvarchar(4000), NEW.[ReservationConfLineNumber], 0)=CONVERT(nvarchar(4000), OLD.[ReservationConfLineNumber], 0) or (NEW.[ReservationConfLineNumber] Is Null and OLD.[ReservationConfLineNumber] Is Null))
			where (
			
			
				(
					NEW.[ArrivalDate] <>
					OLD.[ArrivalDate]
				) Or
			
				(
					NEW.[ArrivalDate] Is Null And
					OLD.[ArrivalDate] Is Not Null
				) Or
				(
					NEW.[ArrivalDate] Is Not Null And
					OLD.[ArrivalDate] Is Null
				)
				)
        
		SET @Inserted = CASE WHEN @@ROWCOUNT > 0 Then 1 Else @Inserted End
	END
	
	If UPDATE([ArrivalTime])
	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
		)
		SELECT
			@AUDIT_LOG_TRANSACTION_ID,
		    convert(nvarchar(1500), IsNull('[ReservationConfLineNumber]='+CONVERT(nvarchar(4000), IsNull(OLD.[ReservationConfLineNumber], NEW.[ReservationConfLineNumber]), 0), '[ReservationConfLineNumber] Is Null')),
		    'ArrivalTime',
			CONVERT(nvarchar(4000), OLD.[ArrivalTime], 0),
			CONVERT(nvarchar(4000), NEW.[ArrivalTime], 0),
			'A'
			, IsNULL( CONVERT(nvarchar(500), CONVERT(nvarchar(4000), OLD.[ReservationConfLineNumber], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[ReservationConfLineNumber], 0)))
			
		FROM deleted OLD Inner Join inserted NEW On 
			(CONVERT(nvarchar(4000), NEW.[ReservationConfLineNumber], 0)=CONVERT(nvarchar(4000), OLD.[ReservationConfLineNumber], 0) or (NEW.[ReservationConfLineNumber] Is Null and OLD.[ReservationConfLineNumber] Is Null))
			where (
			
			
				(
					NEW.[ArrivalTime] <>
					OLD.[ArrivalTime]
				) Or
			
				(
					NEW.[ArrivalTime] Is Null And
					OLD.[ArrivalTime] Is Not Null
				) Or
				(
					NEW.[ArrivalTime] Is Not Null And
					OLD.[ArrivalTime] Is Null
				)
				)
        
		SET @Inserted = CASE WHEN @@ROWCOUNT > 0 Then 1 Else @Inserted End
	END
	
	If UPDATE([DepartureDate])
	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
		)
		SELECT
			@AUDIT_LOG_TRANSACTION_ID,
		    convert(nvarchar(1500), IsNull('[ReservationConfLineNumber]='+CONVERT(nvarchar(4000), IsNull(OLD.[ReservationConfLineNumber], NEW.[ReservationConfLineNumber]), 0), '[ReservationConfLineNumber] Is Null')),
		    'DepartureDate',
			CONVERT(nvarchar(4000), OLD.[DepartureDate], 0),
			CONVERT(nvarchar(4000), NEW.[DepartureDate], 0),
			'A'
			, IsNULL( CONVERT(nvarchar(500), CONVERT(nvarchar(4000), OLD.[ReservationConfLineNumber], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[ReservationConfLineNumber], 0)))
			
		FROM deleted OLD Inner Join inserted NEW On 
			(CONVERT(nvarchar(4000), NEW.[ReservationConfLineNumber], 0)=CONVERT(nvarchar(4000), OLD.[ReservationConfLineNumber], 0) or (NEW.[ReservationConfLineNumber] Is Null and OLD.[ReservationConfLineNumber] Is Null))
			where (
			
			
				(
					NEW.[DepartureDate] <>
					OLD.[DepartureDate]
				) Or
			
				(
					NEW.[DepartureDate] Is Null And
					OLD.[DepartureDate] Is Not Null
				) Or
				(
					NEW.[DepartureDate] Is Not Null And
					OLD.[DepartureDate] Is Null
				)
				)
        
		SET @Inserted = CASE WHEN @@ROWCOUNT > 0 Then 1 Else @Inserted End
	END
	
	If UPDATE([DepartureTime])
	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
		)
		SELECT
			@AUDIT_LOG_TRANSACTION_ID,
		    convert(nvarchar(1500), IsNull('[ReservationConfLineNumber]='+CONVERT(nvarchar(4000), IsNull(OLD.[ReservationConfLineNumber], NEW.[ReservationConfLineNumber]), 0), '[ReservationConfLineNumber] Is Null')),
		    'DepartureTime',
			CONVERT(nvarchar(4000), OLD.[DepartureTime], 0),
			CONVERT(nvarchar(4000), NEW.[DepartureTime], 0),
			'A'
			, IsNULL( CONVERT(nvarchar(500), CONVERT(nvarchar(4000), OLD.[ReservationConfLineNumber], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[ReservationConfLineNumber], 0)))
			
		FROM deleted OLD Inner Join inserted NEW On 
			(CONVERT(nvarchar(4000), NEW.[ReservationConfLineNumber], 0)=CONVERT(nvarchar(4000), OLD.[ReservationConfLineNumber], 0) or (NEW.[ReservationConfLineNumber] Is Null and OLD.[ReservationConfLineNumber] Is Null))
			where (
			
			
				(
					NEW.[DepartureTime] <>
					OLD.[DepartureTime]
				) Or
			
				(
					NEW.[DepartureTime] Is Null And
					OLD.[DepartureTime] Is Not Null
				) Or
				(
					NEW.[DepartureTime] Is Not Null And
					OLD.[DepartureTime] Is Null
				)
				)
        
		SET @Inserted = CASE WHEN @@ROWCOUNT > 0 Then 1 Else @Inserted End
	END
	
	If UPDATE([ProductNumber])
	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
		)
		SELECT
			@AUDIT_LOG_TRANSACTION_ID,
		    convert(nvarchar(1500), IsNull('[ReservationConfLineNumber]='+CONVERT(nvarchar(4000), IsNull(OLD.[ReservationConfLineNumber], NEW.[ReservationConfLineNumber]), 0), '[ReservationConfLineNumber] Is Null')),
		    'ProductNumber',
			CONVERT(nvarchar(4000), OLD.[ProductNumber], 0),
			CONVERT(nvarchar(4000), NEW.[ProductNumber], 0),
			'A'
			, IsNULL( CONVERT(nvarchar(500), CONVERT(nvarchar(4000), OLD.[ReservationConfLineNumber], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[ReservationConfLineNumber], 0)))
			
		FROM deleted OLD Inner Join inserted NEW On 
			(CONVERT(nvarchar(4000), NEW.[ReservationConfLineNumber], 0)=CONVERT(nvarchar(4000), OLD.[ReservationConfLineNumber], 0) or (NEW.[ReservationConfLineNumber] Is Null and OLD.[ReservationConfLineNumber] Is Null))
			where (
			
			
				(
					NEW.[ProductNumber] <>
					OLD.[ProductNumber]
				) Or
			
				(
					NEW.[ProductNumber] Is Null And
					OLD.[ProductNumber] Is Not Null
				) Or
				(
					NEW.[ProductNumber] Is Not Null And
					OLD.[ProductNumber] Is Null
				)
				)
        
		SET @Inserted = CASE WHEN @@ROWCOUNT > 0 Then 1 Else @Inserted End
	END
	
	If UPDATE([PackageNumber])
	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
		)
		SELECT
			@AUDIT_LOG_TRANSACTION_ID,
		    convert(nvarchar(1500), IsNull('[ReservationConfLineNumber]='+CONVERT(nvarchar(4000), IsNull(OLD.[ReservationConfLineNumber], NEW.[ReservationConfLineNumber]), 0), '[ReservationConfLineNumber] Is Null')),
		    'PackageNumber',
			CONVERT(nvarchar(4000), OLD.[PackageNumber], 0),
			CONVERT(nvarchar(4000), NEW.[PackageNumber], 0),
			'A'
			, IsNULL( CONVERT(nvarchar(500), CONVERT(nvarchar(4000), OLD.[ReservationConfLineNumber], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[ReservationConfLineNumber], 0)))
			
		FROM deleted OLD Inner Join inserted NEW On 
			(CONVERT(nvarchar(4000), NEW.[ReservationConfLineNumber], 0)=CONVERT(nvarchar(4000), OLD.[ReservationConfLineNumber], 0) or (NEW.[ReservationConfLineNumber] Is Null and OLD.[ReservationConfLineNumber] Is Null))
			where (
			
			
				(
					NEW.[PackageNumber] <>
					OLD.[PackageNumber]
				) Or
			
				(
					NEW.[PackageNumber] Is Null And
					OLD.[PackageNumber] Is Not Null
				) Or
				(
					NEW.[PackageNumber] Is Not Null And
					OLD.[PackageNumber] Is Null
				)
				)
        
		SET @Inserted = CASE WHEN @@ROWCOUNT > 0 Then 1 Else @Inserted End
	END
	
	If UPDATE([Price])
	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
		)
		SELECT
			@AUDIT_LOG_TRANSACTION_ID,
		    convert(nvarchar(1500), IsNull('[ReservationConfLineNumber]='+CONVERT(nvarchar(4000), IsNull(OLD.[ReservationConfLineNumber], NEW.[ReservationConfLineNumber]), 0), '[ReservationConfLineNumber] Is Null')),
		    'Price',
			CONVERT(nvarchar(4000), OLD.[Price], 2),
			CONVERT(nvarchar(4000), NEW.[Price], 2),
			'A'
			, IsNULL( CONVERT(nvarchar(500), CONVERT(nvarchar(4000), OLD.[ReservationConfLineNumber], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[ReservationConfLineNumber], 0)))
			
		FROM deleted OLD Inner Join inserted NEW On 
			(CONVERT(nvarchar(4000), NEW.[ReservationConfLineNumber], 0)=CONVERT(nvarchar(4000), OLD.[ReservationConfLineNumber], 0) or (NEW.[ReservationConfLineNumber] Is Null and OLD.[ReservationConfLineNumber] Is Null))
			where (
			
			
				(
					NEW.[Price] <>
					OLD.[Price]
				) Or
			
				(
					NEW.[Price] Is Null And
					OLD.[Price] Is Not Null
				) Or
				(
					NEW.[Price] Is Not Null And
					OLD.[Price] Is Null
				)
				)
        
		SET @Inserted = CASE WHEN @@ROWCOUNT > 0 Then 1 Else @Inserted End
	END
	
	If UPDATE([TotalPrice])
	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
		)
		SELECT
			@AUDIT_LOG_TRANSACTION_ID,
		    convert(nvarchar(1500), IsNull('[ReservationConfLineNumber]='+CONVERT(nvarchar(4000), IsNull(OLD.[ReservationConfLineNumber], NEW.[ReservationConfLineNumber]), 0), '[ReservationConfLineNumber] Is Null')),
		    'TotalPrice',
			CONVERT(nvarchar(4000), OLD.[TotalPrice], 2),
			CONVERT(nvarchar(4000), NEW.[TotalPrice], 2),
			'A'
			, IsNULL( CONVERT(nvarchar(500), CONVERT(nvarchar(4000), OLD.[ReservationConfLineNumber], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[ReservationConfLineNumber], 0)))
			
		FROM deleted OLD Inner Join inserted NEW On 
			(CONVERT(nvarchar(4000), NEW.[ReservationConfLineNumber], 0)=CONVERT(nvarchar(4000), OLD.[ReservationConfLineNumber], 0) or (NEW.[ReservationConfLineNumber] Is Null and OLD.[ReservationConfLineNumber] Is Null))
			where (
			
			
				(
					NEW.[TotalPrice] <>
					OLD.[TotalPrice]
				) Or
			
				(
					NEW.[TotalPrice] Is Null And
					OLD.[TotalPrice] Is Not Null
				) Or
				(
					NEW.[TotalPrice] Is Not Null And
					OLD.[TotalPrice] Is Null
				)
				)
        
		SET @Inserted = CASE WHEN @@ROWCOUNT > 0 Then 1 Else @Inserted End
	END
	
	If UPDATE([PriceOverWritten])
	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
		)
		SELECT
			@AUDIT_LOG_TRANSACTION_ID,
		    convert(nvarchar(1500), IsNull('[ReservationConfLineNumber]='+CONVERT(nvarchar(4000), IsNull(OLD.[ReservationConfLineNumber], NEW.[ReservationConfLineNumber]), 0), '[ReservationConfLineNumber] Is Null')),
		    'PriceOverWritten',
			CONVERT(nvarchar(4000), OLD.[PriceOverWritten], 0),
			CONVERT(nvarchar(4000), NEW.[PriceOverWritten], 0),
			'A'
			, IsNULL( CONVERT(nvarchar(500), CONVERT(nvarchar(4000), OLD.[ReservationConfLineNumber], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[ReservationConfLineNumber], 0)))
			
		FROM deleted OLD Inner Join inserted NEW On 
			(CONVERT(nvarchar(4000), NEW.[ReservationConfLineNumber], 0)=CONVERT(nvarchar(4000), OLD.[ReservationConfLineNumber], 0) or (NEW.[ReservationConfLineNumber] Is Null and OLD.[ReservationConfLineNumber] Is Null))
			where (
			
			
				(
					NEW.[PriceOverWritten] <>
					OLD.[PriceOverWritten]
				) Or
			
				(
					NEW.[PriceOverWritten] Is Null And
					OLD.[PriceOverWritten] Is Not Null
				) Or
				(
					NEW.[PriceOverWritten] Is Not Null And
					OLD.[PriceOverWritten] Is Null
				)
				)
        
		SET @Inserted = CASE WHEN @@ROWCOUNT > 0 Then 1 Else @Inserted End
	END
	
	If UPDATE([ConferenceRoomNumber])
	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
		)
		SELECT
			@AUDIT_LOG_TRANSACTION_ID,
		    convert(nvarchar(1500), IsNull('[ReservationConfLineNumber]='+CONVERT(nvarchar(4000), IsNull(OLD.[ReservationConfLineNumber], NEW.[ReservationConfLineNumber]), 0), '[ReservationConfLineNumber] Is Null')),
		    'ConferenceRoomNumber',
			CONVERT(nvarchar(4000), OLD.[ConferenceRoomNumber], 0),
			CONVERT(nvarchar(4000), NEW.[ConferenceRoomNumber], 0),
			'A'
			, IsNULL( CONVERT(nvarchar(500), CONVERT(nvarchar(4000), OLD.[ReservationConfLineNumber], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[ReservationConfLineNumber], 0)))
			
		FROM deleted OLD Inner Join inserted NEW On 
			(CONVERT(nvarchar(4000), NEW.[ReservationConfLineNumber], 0)=CONVERT(nvarchar(4000), OLD.[ReservationConfLineNumber], 0) or (NEW.[ReservationConfLineNumber] Is Null and OLD.[ReservationConfLineNumber] Is Null))
			where (
			
			
				(
					NEW.[ConferenceRoomNumber] <>
					OLD.[ConferenceRoomNumber]
				) Or
			
				(
					NEW.[ConferenceRoomNumber] Is Null And
					OLD.[ConferenceRoomNumber] Is Not Null
				) Or
				(
					NEW.[ConferenceRoomNumber] Is Not Null And
					OLD.[ConferenceRoomNumber] Is Null
				)
				)
        
		SET @Inserted = CASE WHEN @@ROWCOUNT > 0 Then 1 Else @Inserted End
	END
	
	If UPDATE([Adults])
	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
		)
		SELECT
			@AUDIT_LOG_TRANSACTION_ID,
		    convert(nvarchar(1500), IsNull('[ReservationConfLineNumber]='+CONVERT(nvarchar(4000), IsNull(OLD.[ReservationConfLineNumber], NEW.[ReservationConfLineNumber]), 0), '[ReservationConfLineNumber] Is Null')),
		    'Adults',
			CONVERT(nvarchar(4000), OLD.[Adults], 0),
			CONVERT(nvarchar(4000), NEW.[Adults], 0),
			'A'
			, IsNULL( CONVERT(nvarchar(500), CONVERT(nvarchar(4000), OLD.[ReservationConfLineNumber], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[ReservationConfLineNumber], 0)))
			
		FROM deleted OLD Inner Join inserted NEW On 
			(CONVERT(nvarchar(4000), NEW.[ReservationConfLineNumber], 0)=CONVERT(nvarchar(4000), OLD.[ReservationConfLineNumber], 0) or (NEW.[ReservationConfLineNumber] Is Null and OLD.[ReservationConfLineNumber] Is Null))
			where (
			
			
				(
					NEW.[Adults] <>
					OLD.[Adults]
				) Or
			
				(
					NEW.[Adults] Is Null And
					OLD.[Adults] Is Not Null
				) Or
				(
					NEW.[Adults] Is Not Null And
					OLD.[Adults] Is Null
				)
				)
        
		SET @Inserted = CASE WHEN @@ROWCOUNT > 0 Then 1 Else @Inserted End
	END
	
	If UPDATE([Children1])
	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
		)
		SELECT
			@AUDIT_LOG_TRANSACTION_ID,
		    convert(nvarchar(1500), IsNull('[ReservationConfLineNumber]='+CONVERT(nvarchar(4000), IsNull(OLD.[ReservationConfLineNumber], NEW.[ReservationConfLineNumber]), 0), '[ReservationConfLineNumber] Is Null')),
		    'Children1',
			CONVERT(nvarchar(4000), OLD.[Children1], 0),
			CONVERT(nvarchar(4000), NEW.[Children1], 0),
			'A'
			, IsNULL( CONVERT(nvarchar(500), CONVERT(nvarchar(4000), OLD.[ReservationConfLineNumber], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[ReservationConfLineNumber], 0)))
			
		FROM deleted OLD Inner Join inserted NEW On 
			(CONVERT(nvarchar(4000), NEW.[ReservationConfLineNumber], 0)=CONVERT(nvarchar(4000), OLD.[ReservationConfLineNumber], 0) or (NEW.[ReservationConfLineNumber] Is Null and OLD.[ReservationConfLineNumber] Is Null))
			where (
			
			
				(
					NEW.[Children1] <>
					OLD.[Children1]
				) Or
			
				(
					NEW.[Children1] Is Null And
					OLD.[Children1] Is Not Null
				) Or
				(
					NEW.[Children1] Is Not Null And
					OLD.[Children1] Is Null
				)
				)
        
		SET @Inserted = CASE WHEN @@ROWCOUNT > 0 Then 1 Else @Inserted End
	END
	
	If UPDATE([ReferenceNumber])
	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
		)
		SELECT
			@AUDIT_LOG_TRANSACTION_ID,
		    convert(nvarchar(1500), IsNull('[ReservationConfLineNumber]='+CONVERT(nvarchar(4000), IsNull(OLD.[ReservationConfLineNumber], NEW.[ReservationConfLineNumber]), 0), '[ReservationConfLineNumber] Is Null')),
		    'ReferenceNumber',
			CONVERT(nvarchar(4000), OLD.[ReferenceNumber], 0),
			CONVERT(nvarchar(4000), NEW.[ReferenceNumber], 0),
			'A'
			, IsNULL( CONVERT(nvarchar(500), CONVERT(nvarchar(4000), OLD.[ReservationConfLineNumber], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[ReservationConfLineNumber], 0)))
			
		FROM deleted OLD Inner Join inserted NEW On 
			(CONVERT(nvarchar(4000), NEW.[ReservationConfLineNumber], 0)=CONVERT(nvarchar(4000), OLD.[ReservationConfLineNumber], 0) or (NEW.[ReservationConfLineNumber] Is Null and OLD.[ReservationConfLineNumber] Is Null))
			where (
			
			
				(
					NEW.[ReferenceNumber] <>
					OLD.[ReferenceNumber]
				) Or
			
				(
					NEW.[ReferenceNumber] Is Null And
					OLD.[ReferenceNumber] Is Not Null
				) Or
				(
					NEW.[ReferenceNumber] Is Not Null And
					OLD.[ReferenceNumber] Is Null
				)
				)
        
		SET @Inserted = CASE WHEN @@ROWCOUNT > 0 Then 1 Else @Inserted End
	END
	
	If UPDATE([TableBooking])
	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
		)
		SELECT
			@AUDIT_LOG_TRANSACTION_ID,
		    convert(nvarchar(1500), IsNull('[ReservationConfLineNumber]='+CONVERT(nvarchar(4000), IsNull(OLD.[ReservationConfLineNumber], NEW.[ReservationConfLineNumber]), 0), '[ReservationConfLineNumber] Is Null')),
		    'TableBooking',
			CONVERT(nvarchar(4000), OLD.[TableBooking], 0),
			CONVERT(nvarchar(4000), NEW.[TableBooking], 0),
			'A'
			, IsNULL( CONVERT(nvarchar(500), CONVERT(nvarchar(4000), OLD.[ReservationConfLineNumber], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[ReservationConfLineNumber], 0)))
			
		FROM deleted OLD Inner Join inserted NEW On 
			(CONVERT(nvarchar(4000), NEW.[ReservationConfLineNumber], 0)=CONVERT(nvarchar(4000), OLD.[ReservationConfLineNumber], 0) or (NEW.[ReservationConfLineNumber] Is Null and OLD.[ReservationConfLineNumber] Is Null))
			where (
			
			
				(
					NEW.[TableBooking] <>
					OLD.[TableBooking]
				) Or
			
				(
					NEW.[TableBooking] Is Null And
					OLD.[TableBooking] Is Not Null
				) Or
				(
					NEW.[TableBooking] Is Not Null And
					OLD.[TableBooking] Is Null
				)
				)
        
		SET @Inserted = CASE WHEN @@ROWCOUNT > 0 Then 1 Else @Inserted End
	END
	
	If UPDATE([TableStatus])
	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
		)
		SELECT
			@AUDIT_LOG_TRANSACTION_ID,
		    convert(nvarchar(1500), IsNull('[ReservationConfLineNumber]='+CONVERT(nvarchar(4000), IsNull(OLD.[ReservationConfLineNumber], NEW.[ReservationConfLineNumber]), 0), '[ReservationConfLineNumber] Is Null')),
		    'TableStatus',
			CONVERT(nvarchar(4000), OLD.[TableStatus], 0),
			CONVERT(nvarchar(4000), NEW.[TableStatus], 0),
			'A'
			, IsNULL( CONVERT(nvarchar(500), CONVERT(nvarchar(4000), OLD.[ReservationConfLineNumber], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[ReservationConfLineNumber], 0)))
			
		FROM deleted OLD Inner Join inserted NEW On 
			(CONVERT(nvarchar(4000), NEW.[ReservationConfLineNumber], 0)=CONVERT(nvarchar(4000), OLD.[ReservationConfLineNumber], 0) or (NEW.[ReservationConfLineNumber] Is Null and OLD.[ReservationConfLineNumber] Is Null))
			where (
			
			
				(
					NEW.[TableStatus] <>
					OLD.[TableStatus]
				) Or
			
				(
					NEW.[TableStatus] Is Null And
					OLD.[TableStatus] Is Not Null
				) Or
				(
					NEW.[TableStatus] Is Not Null And
					OLD.[TableStatus] Is Null
				)
				)
        
		SET @Inserted = CASE WHEN @@ROWCOUNT > 0 Then 1 Else @Inserted End
	END
	
	If UPDATE([GuestNo])
	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
		)
		SELECT
			@AUDIT_LOG_TRANSACTION_ID,
		    convert(nvarchar(1500), IsNull('[ReservationConfLineNumber]='+CONVERT(nvarchar(4000), IsNull(OLD.[ReservationConfLineNumber], NEW.[ReservationConfLineNumber]), 0), '[ReservationConfLineNumber] Is Null')),
		    'GuestNo',
			CONVERT(nvarchar(4000), OLD.[GuestNo], 0),
			CONVERT(nvarchar(4000), NEW.[GuestNo], 0),
			'A'
			, IsNULL( CONVERT(nvarchar(500), CONVERT(nvarchar(4000), OLD.[ReservationConfLineNumber], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[ReservationConfLineNumber], 0)))
			
		FROM deleted OLD Inner Join inserted NEW On 
			(CONVERT(nvarchar(4000), NEW.[ReservationConfLineNumber], 0)=CONVERT(nvarchar(4000), OLD.[ReservationConfLineNumber], 0) or (NEW.[ReservationConfLineNumber] Is Null and OLD.[ReservationConfLineNumber] Is Null))
			where (
			
			
				(
					NEW.[GuestNo] <>
					OLD.[GuestNo]
				) Or
			
				(
					NEW.[GuestNo] Is Null And
					OLD.[GuestNo] Is Not Null
				) Or
				(
					NEW.[GuestNo] Is Not Null And
					OLD.[GuestNo] Is Null
				)
				)
        
		SET @Inserted = CASE WHEN @@ROWCOUNT > 0 Then 1 Else @Inserted End
	END
	
	If UPDATE([Children2])
	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
		)
		SELECT
			@AUDIT_LOG_TRANSACTION_ID,
		    convert(nvarchar(1500), IsNull('[ReservationConfLineNumber]='+CONVERT(nvarchar(4000), IsNull(OLD.[ReservationConfLineNumber], NEW.[ReservationConfLineNumber]), 0), '[ReservationConfLineNumber] Is Null')),
		    'Children2',
			CONVERT(nvarchar(4000), OLD.[Children2], 0),
			CONVERT(nvarchar(4000), NEW.[Children2], 0),
			'A'
			, IsNULL( CONVERT(nvarchar(500), CONVERT(nvarchar(4000), OLD.[ReservationConfLineNumber], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[ReservationConfLineNumber], 0)))
			
		FROM deleted OLD Inner Join inserted NEW On 
			(CONVERT(nvarchar(4000), NEW.[ReservationConfLineNumber], 0)=CONVERT(nvarchar(4000), OLD.[ReservationConfLineNumber], 0) or (NEW.[ReservationConfLineNumber] Is Null and OLD.[ReservationConfLineNumber] Is Null))
			where (
			
			
				(
					NEW.[Children2] <>
					OLD.[Children2]
				) Or
			
				(
					NEW.[Children2] Is Null And
					OLD.[Children2] Is Not Null
				) Or
				(
					NEW.[Children2] Is Not Null And
					OLD.[Children2] Is Null
				)
				)
        
		SET @Inserted = CASE WHEN @@ROWCOUNT > 0 Then 1 Else @Inserted End
	END
	
	If UPDATE([PriceIsUserOverwritten])
	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
		)
		SELECT
			@AUDIT_LOG_TRANSACTION_ID,
		    convert(nvarchar(1500), IsNull('[ReservationConfLineNumber]='+CONVERT(nvarchar(4000), IsNull(OLD.[ReservationConfLineNumber], NEW.[ReservationConfLineNumber]), 0), '[ReservationConfLineNumber] Is Null')),
		    'PriceIsUserOverwritten',
			CONVERT(nvarchar(4000), OLD.[PriceIsUserOverwritten], 0),
			CONVERT(nvarchar(4000), NEW.[PriceIsUserOverwritten], 0),
			'A'
			, IsNULL( CONVERT(nvarchar(500), CONVERT(nvarchar(4000), OLD.[ReservationConfLineNumber], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[ReservationConfLineNumber], 0)))
			
		FROM deleted OLD Inner Join inserted NEW On 
			(CONVERT(nvarchar(4000), NEW.[ReservationConfLineNumber], 0)=CONVERT(nvarchar(4000), OLD.[ReservationConfLineNumber], 0) or (NEW.[ReservationConfLineNumber] Is Null and OLD.[ReservationConfLineNumber] Is Null))
			where (
			
			
				(
					NEW.[PriceIsUserOverwritten] <>
					OLD.[PriceIsUserOverwritten]
				) Or
			
				(
					NEW.[PriceIsUserOverwritten] Is Null And
					OLD.[PriceIsUserOverwritten] Is Not Null
				) Or
				(
					NEW.[PriceIsUserOverwritten] Is Not Null And
					OLD.[PriceIsUserOverwritten] 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
