IF EXISTS (SELECT * FROM dbo.SysObjects WHERE id = object_id(N'dbo.AUDIT_ins_RESERVATIONS') AND OBJECTPROPERTY(id, N'IsTrigger') = 1) DROP TRIGGER dbo.AUDIT_ins_RESERVATIONS 
GO
CREATE TRIGGER [dbo].[AUDIT_ins_RESERVATIONS]
ON dbo.Reservations FOR INSERT NOT FOR REPLICATION AS
BEGIN
    
    
	DECLARE 
		@IDENTITY_SAVE				varchar(50),
		@AUDIT_LOG_TRANSACTION_ID		Int,
		@PRIM_KEY				nvarchar(4000),
		@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(
		'RESERVATIONS',
		'dbo',
		2,	--	ACTION ID For INSERT
		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()
	

	
	INSERT INTO dbo.AUDIT_LOG_DATA
	(
		AUDIT_LOG_TRANSACTION_ID,
		PRIMARY_KEY_DATA,
		COL_NAME,
		NEW_VALUE_LONG,
		DATA_TYPE
		, KEY1, KEY2
	)
	SELECT
		@AUDIT_LOG_TRANSACTION_ID,
		convert(nvarchar(1500), IsNull('[ReferenceNumber]='+CONVERT(nvarchar(4000), NEW.[ReferenceNumber], 0), '[ReferenceNumber] Is Null') + ' AND ' + IsNull('[GuestNumber]='+CONVERT(nvarchar(4000), NEW.[GuestNumber], 0), '[GuestNumber] Is Null')),
		'Status',
		CONVERT(nvarchar(4000), NEW.[Status], 0),
		'A'
		, CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[ReferenceNumber], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[GuestNumber], 0))
	FROM inserted NEW
	
	  WHERE NEW.[Status] IS NOT NULL
	
    
	INSERT INTO dbo.AUDIT_LOG_DATA
	(
		AUDIT_LOG_TRANSACTION_ID,
		PRIMARY_KEY_DATA,
		COL_NAME,
		NEW_VALUE_LONG,
		DATA_TYPE
		, KEY1, KEY2
	)
	SELECT
		@AUDIT_LOG_TRANSACTION_ID,
		convert(nvarchar(1500), IsNull('[ReferenceNumber]='+CONVERT(nvarchar(4000), NEW.[ReferenceNumber], 0), '[ReferenceNumber] Is Null') + ' AND ' + IsNull('[GuestNumber]='+CONVERT(nvarchar(4000), NEW.[GuestNumber], 0), '[GuestNumber] Is Null')),
		'MARKETSEGMENT',
		CONVERT(nvarchar(4000), NEW.[MARKETSEGMENT], 0),
		'A'
		, CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[ReferenceNumber], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[GuestNumber], 0))
	FROM inserted NEW
	
	  WHERE NEW.[MARKETSEGMENT] IS NOT NULL
	
    
	INSERT INTO dbo.AUDIT_LOG_DATA
	(
		AUDIT_LOG_TRANSACTION_ID,
		PRIMARY_KEY_DATA,
		COL_NAME,
		NEW_VALUE_LONG,
		DATA_TYPE
		, KEY1, KEY2
	)
	SELECT
		@AUDIT_LOG_TRANSACTION_ID,
		convert(nvarchar(1500), IsNull('[ReferenceNumber]='+CONVERT(nvarchar(4000), NEW.[ReferenceNumber], 0), '[ReferenceNumber] Is Null') + ' AND ' + IsNull('[GuestNumber]='+CONVERT(nvarchar(4000), NEW.[GuestNumber], 0), '[GuestNumber] Is Null')),
		'POINTOFSALE',
		CONVERT(nvarchar(4000), NEW.[POINTOFSALE], 0),
		'A'
		, CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[ReferenceNumber], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[GuestNumber], 0))
	FROM inserted NEW
	
	  WHERE NEW.[POINTOFSALE] IS NOT NULL
	
    
	INSERT INTO dbo.AUDIT_LOG_DATA
	(
		AUDIT_LOG_TRANSACTION_ID,
		PRIMARY_KEY_DATA,
		COL_NAME,
		NEW_VALUE_LONG,
		DATA_TYPE
		, KEY1, KEY2
	)
	SELECT
		@AUDIT_LOG_TRANSACTION_ID,
		convert(nvarchar(1500), IsNull('[ReferenceNumber]='+CONVERT(nvarchar(4000), NEW.[ReferenceNumber], 0), '[ReferenceNumber] Is Null') + ' AND ' + IsNull('[GuestNumber]='+CONVERT(nvarchar(4000), NEW.[GuestNumber], 0), '[GuestNumber] Is Null')),
		'GUESTTYPE',
		CONVERT(nvarchar(4000), NEW.[GUESTTYPE], 0),
		'A'
		, CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[ReferenceNumber], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[GuestNumber], 0))
	FROM inserted NEW
	
	  WHERE NEW.[GUESTTYPE] IS NOT NULL
	
    
	INSERT INTO dbo.AUDIT_LOG_DATA
	(
		AUDIT_LOG_TRANSACTION_ID,
		PRIMARY_KEY_DATA,
		COL_NAME,
		NEW_VALUE_LONG,
		DATA_TYPE
		, KEY1, KEY2
	)
	SELECT
		@AUDIT_LOG_TRANSACTION_ID,
		convert(nvarchar(1500), IsNull('[ReferenceNumber]='+CONVERT(nvarchar(4000), NEW.[ReferenceNumber], 0), '[ReferenceNumber] Is Null') + ' AND ' + IsNull('[GuestNumber]='+CONVERT(nvarchar(4000), NEW.[GuestNumber], 0), '[GuestNumber] Is Null')),
		'PURPOSECODE',
		CONVERT(nvarchar(4000), NEW.[PURPOSECODE], 0),
		'A'
		, CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[ReferenceNumber], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[GuestNumber], 0))
	FROM inserted NEW
	
	  WHERE NEW.[PURPOSECODE] IS NOT NULL
	
    
	INSERT INTO dbo.AUDIT_LOG_DATA
	(
		AUDIT_LOG_TRANSACTION_ID,
		PRIMARY_KEY_DATA,
		COL_NAME,
		NEW_VALUE_LONG,
		DATA_TYPE
		, KEY1, KEY2
	)
	SELECT
		@AUDIT_LOG_TRANSACTION_ID,
		convert(nvarchar(1500), IsNull('[ReferenceNumber]='+CONVERT(nvarchar(4000), NEW.[ReferenceNumber], 0), '[ReferenceNumber] Is Null') + ' AND ' + IsNull('[GuestNumber]='+CONVERT(nvarchar(4000), NEW.[GuestNumber], 0), '[GuestNumber] Is Null')),
		'ArrivalDate',
		CONVERT(nvarchar(4000), NEW.[ArrivalDate], 0),
		'A'
		, CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[ReferenceNumber], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[GuestNumber], 0))
	FROM inserted NEW
	
	  WHERE NEW.[ArrivalDate] IS NOT NULL
	
    
	INSERT INTO dbo.AUDIT_LOG_DATA
	(
		AUDIT_LOG_TRANSACTION_ID,
		PRIMARY_KEY_DATA,
		COL_NAME,
		NEW_VALUE_LONG,
		DATA_TYPE
		, KEY1, KEY2
	)
	SELECT
		@AUDIT_LOG_TRANSACTION_ID,
		convert(nvarchar(1500), IsNull('[ReferenceNumber]='+CONVERT(nvarchar(4000), NEW.[ReferenceNumber], 0), '[ReferenceNumber] Is Null') + ' AND ' + IsNull('[GuestNumber]='+CONVERT(nvarchar(4000), NEW.[GuestNumber], 0), '[GuestNumber] Is Null')),
		'ArrivalTime',
		CONVERT(nvarchar(4000), NEW.[ArrivalTime], 0),
		'A'
		, CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[ReferenceNumber], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[GuestNumber], 0))
	FROM inserted NEW
	
	  WHERE NEW.[ArrivalTime] IS NOT NULL
	
    
	INSERT INTO dbo.AUDIT_LOG_DATA
	(
		AUDIT_LOG_TRANSACTION_ID,
		PRIMARY_KEY_DATA,
		COL_NAME,
		NEW_VALUE_LONG,
		DATA_TYPE
		, KEY1, KEY2
	)
	SELECT
		@AUDIT_LOG_TRANSACTION_ID,
		convert(nvarchar(1500), IsNull('[ReferenceNumber]='+CONVERT(nvarchar(4000), NEW.[ReferenceNumber], 0), '[ReferenceNumber] Is Null') + ' AND ' + IsNull('[GuestNumber]='+CONVERT(nvarchar(4000), NEW.[GuestNumber], 0), '[GuestNumber] Is Null')),
		'DepartureDate',
		CONVERT(nvarchar(4000), NEW.[DepartureDate], 0),
		'A'
		, CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[ReferenceNumber], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[GuestNumber], 0))
	FROM inserted NEW
	
	  WHERE NEW.[DepartureDate] IS NOT NULL
	
    
	INSERT INTO dbo.AUDIT_LOG_DATA
	(
		AUDIT_LOG_TRANSACTION_ID,
		PRIMARY_KEY_DATA,
		COL_NAME,
		NEW_VALUE_LONG,
		DATA_TYPE
		, KEY1, KEY2
	)
	SELECT
		@AUDIT_LOG_TRANSACTION_ID,
		convert(nvarchar(1500), IsNull('[ReferenceNumber]='+CONVERT(nvarchar(4000), NEW.[ReferenceNumber], 0), '[ReferenceNumber] Is Null') + ' AND ' + IsNull('[GuestNumber]='+CONVERT(nvarchar(4000), NEW.[GuestNumber], 0), '[GuestNumber] Is Null')),
		'DepartureTime',
		CONVERT(nvarchar(4000), NEW.[DepartureTime], 0),
		'A'
		, CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[ReferenceNumber], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[GuestNumber], 0))
	FROM inserted NEW
	
	  WHERE NEW.[DepartureTime] IS NOT NULL
	
    
	INSERT INTO dbo.AUDIT_LOG_DATA
	(
		AUDIT_LOG_TRANSACTION_ID,
		PRIMARY_KEY_DATA,
		COL_NAME,
		NEW_VALUE_LONG,
		DATA_TYPE
		, KEY1, KEY2
	)
	SELECT
		@AUDIT_LOG_TRANSACTION_ID,
		convert(nvarchar(1500), IsNull('[ReferenceNumber]='+CONVERT(nvarchar(4000), NEW.[ReferenceNumber], 0), '[ReferenceNumber] Is Null') + ' AND ' + IsNull('[GuestNumber]='+CONVERT(nvarchar(4000), NEW.[GuestNumber], 0), '[GuestNumber] Is Null')),
		'Adults',
		CONVERT(nvarchar(4000), NEW.[Adults], 0),
		'A'
		, CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[ReferenceNumber], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[GuestNumber], 0))
	FROM inserted NEW
	
	  WHERE NEW.[Adults] IS NOT NULL
	
    
	INSERT INTO dbo.AUDIT_LOG_DATA
	(
		AUDIT_LOG_TRANSACTION_ID,
		PRIMARY_KEY_DATA,
		COL_NAME,
		NEW_VALUE_LONG,
		DATA_TYPE
		, KEY1, KEY2
	)
	SELECT
		@AUDIT_LOG_TRANSACTION_ID,
		convert(nvarchar(1500), IsNull('[ReferenceNumber]='+CONVERT(nvarchar(4000), NEW.[ReferenceNumber], 0), '[ReferenceNumber] Is Null') + ' AND ' + IsNull('[GuestNumber]='+CONVERT(nvarchar(4000), NEW.[GuestNumber], 0), '[GuestNumber] Is Null')),
		'Children1',
		CONVERT(nvarchar(4000), NEW.[Children1], 0),
		'A'
		, CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[ReferenceNumber], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[GuestNumber], 0))
	FROM inserted NEW
	
	  WHERE NEW.[Children1] IS NOT NULL
	
    
	INSERT INTO dbo.AUDIT_LOG_DATA
	(
		AUDIT_LOG_TRANSACTION_ID,
		PRIMARY_KEY_DATA,
		COL_NAME,
		NEW_VALUE_LONG,
		DATA_TYPE
		, KEY1, KEY2
	)
	SELECT
		@AUDIT_LOG_TRANSACTION_ID,
		convert(nvarchar(1500), IsNull('[ReferenceNumber]='+CONVERT(nvarchar(4000), NEW.[ReferenceNumber], 0), '[ReferenceNumber] Is Null') + ' AND ' + IsNull('[GuestNumber]='+CONVERT(nvarchar(4000), NEW.[GuestNumber], 0), '[GuestNumber] Is Null')),
		'Children2',
		CONVERT(nvarchar(4000), NEW.[Children2], 0),
		'A'
		, CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[ReferenceNumber], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[GuestNumber], 0))
	FROM inserted NEW
	
	  WHERE NEW.[Children2] IS NOT NULL
	
    
	INSERT INTO dbo.AUDIT_LOG_DATA
	(
		AUDIT_LOG_TRANSACTION_ID,
		PRIMARY_KEY_DATA,
		COL_NAME,
		NEW_VALUE_LONG,
		DATA_TYPE
		, KEY1, KEY2
	)
	SELECT
		@AUDIT_LOG_TRANSACTION_ID,
		convert(nvarchar(1500), IsNull('[ReferenceNumber]='+CONVERT(nvarchar(4000), NEW.[ReferenceNumber], 0), '[ReferenceNumber] Is Null') + ' AND ' + IsNull('[GuestNumber]='+CONVERT(nvarchar(4000), NEW.[GuestNumber], 0), '[GuestNumber] Is Null')),
		'ProductNumber',
		CONVERT(nvarchar(4000), NEW.[ProductNumber], 0),
		'A'
		, CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[ReferenceNumber], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[GuestNumber], 0))
	FROM inserted NEW
	
	  WHERE NEW.[ProductNumber] IS NOT NULL
	
    
	INSERT INTO dbo.AUDIT_LOG_DATA
	(
		AUDIT_LOG_TRANSACTION_ID,
		PRIMARY_KEY_DATA,
		COL_NAME,
		NEW_VALUE_LONG,
		DATA_TYPE
		, KEY1, KEY2
	)
	SELECT
		@AUDIT_LOG_TRANSACTION_ID,
		convert(nvarchar(1500), IsNull('[ReferenceNumber]='+CONVERT(nvarchar(4000), NEW.[ReferenceNumber], 0), '[ReferenceNumber] Is Null') + ' AND ' + IsNull('[GuestNumber]='+CONVERT(nvarchar(4000), NEW.[GuestNumber], 0), '[GuestNumber] Is Null')),
		'ProductNumber2',
		CONVERT(nvarchar(4000), NEW.[ProductNumber2], 0),
		'A'
		, CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[ReferenceNumber], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[GuestNumber], 0))
	FROM inserted NEW
	
	  WHERE NEW.[ProductNumber2] IS NOT NULL
	
    
	INSERT INTO dbo.AUDIT_LOG_DATA
	(
		AUDIT_LOG_TRANSACTION_ID,
		PRIMARY_KEY_DATA,
		COL_NAME,
		NEW_VALUE_LONG,
		DATA_TYPE
		, KEY1, KEY2
	)
	SELECT
		@AUDIT_LOG_TRANSACTION_ID,
		convert(nvarchar(1500), IsNull('[ReferenceNumber]='+CONVERT(nvarchar(4000), NEW.[ReferenceNumber], 0), '[ReferenceNumber] Is Null') + ' AND ' + IsNull('[GuestNumber]='+CONVERT(nvarchar(4000), NEW.[GuestNumber], 0), '[GuestNumber] Is Null')),
		'ProductNumber3',
		CONVERT(nvarchar(4000), NEW.[ProductNumber3], 0),
		'A'
		, CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[ReferenceNumber], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[GuestNumber], 0))
	FROM inserted NEW
	
	  WHERE NEW.[ProductNumber3] IS NOT NULL
	
    
	INSERT INTO dbo.AUDIT_LOG_DATA
	(
		AUDIT_LOG_TRANSACTION_ID,
		PRIMARY_KEY_DATA,
		COL_NAME,
		NEW_VALUE_LONG,
		DATA_TYPE
		, KEY1, KEY2
	)
	SELECT
		@AUDIT_LOG_TRANSACTION_ID,
		convert(nvarchar(1500), IsNull('[ReferenceNumber]='+CONVERT(nvarchar(4000), NEW.[ReferenceNumber], 0), '[ReferenceNumber] Is Null') + ' AND ' + IsNull('[GuestNumber]='+CONVERT(nvarchar(4000), NEW.[GuestNumber], 0), '[GuestNumber] Is Null')),
		'ProductNumber4',
		CONVERT(nvarchar(4000), NEW.[ProductNumber4], 0),
		'A'
		, CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[ReferenceNumber], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[GuestNumber], 0))
	FROM inserted NEW
	
	  WHERE NEW.[ProductNumber4] IS NOT NULL
	
    
	INSERT INTO dbo.AUDIT_LOG_DATA
	(
		AUDIT_LOG_TRANSACTION_ID,
		PRIMARY_KEY_DATA,
		COL_NAME,
		NEW_VALUE_LONG,
		DATA_TYPE
		, KEY1, KEY2
	)
	SELECT
		@AUDIT_LOG_TRANSACTION_ID,
		convert(nvarchar(1500), IsNull('[ReferenceNumber]='+CONVERT(nvarchar(4000), NEW.[ReferenceNumber], 0), '[ReferenceNumber] Is Null') + ' AND ' + IsNull('[GuestNumber]='+CONVERT(nvarchar(4000), NEW.[GuestNumber], 0), '[GuestNumber] Is Null')),
		'Price',
		CONVERT(nvarchar(4000), NEW.[Price], 2),
		'A'
		, CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[ReferenceNumber], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[GuestNumber], 0))
	FROM inserted NEW
	
	  WHERE NEW.[Price] IS NOT NULL
	
    
	INSERT INTO dbo.AUDIT_LOG_DATA
	(
		AUDIT_LOG_TRANSACTION_ID,
		PRIMARY_KEY_DATA,
		COL_NAME,
		NEW_VALUE_LONG,
		DATA_TYPE
		, KEY1, KEY2
	)
	SELECT
		@AUDIT_LOG_TRANSACTION_ID,
		convert(nvarchar(1500), IsNull('[ReferenceNumber]='+CONVERT(nvarchar(4000), NEW.[ReferenceNumber], 0), '[ReferenceNumber] Is Null') + ' AND ' + IsNull('[GuestNumber]='+CONVERT(nvarchar(4000), NEW.[GuestNumber], 0), '[GuestNumber] Is Null')),
		'TotalPrice',
		CONVERT(nvarchar(4000), NEW.[TotalPrice], 2),
		'A'
		, CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[ReferenceNumber], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[GuestNumber], 0))
	FROM inserted NEW
	
	  WHERE NEW.[TotalPrice] IS NOT NULL
	
    
	INSERT INTO dbo.AUDIT_LOG_DATA
	(
		AUDIT_LOG_TRANSACTION_ID,
		PRIMARY_KEY_DATA,
		COL_NAME,
		NEW_VALUE_LONG,
		DATA_TYPE
		, KEY1, KEY2
	)
	SELECT
		@AUDIT_LOG_TRANSACTION_ID,
		convert(nvarchar(1500), IsNull('[ReferenceNumber]='+CONVERT(nvarchar(4000), NEW.[ReferenceNumber], 0), '[ReferenceNumber] Is Null') + ' AND ' + IsNull('[GuestNumber]='+CONVERT(nvarchar(4000), NEW.[GuestNumber], 0), '[GuestNumber] Is Null')),
		'PriceOverwritten',
		CONVERT(nvarchar(4000), NEW.[PriceOverwritten], 0),
		'A'
		, CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[ReferenceNumber], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[GuestNumber], 0))
	FROM inserted NEW
	
	  WHERE NEW.[PriceOverwritten] IS NOT NULL
	
    
	INSERT INTO dbo.AUDIT_LOG_DATA
	(
		AUDIT_LOG_TRANSACTION_ID,
		PRIMARY_KEY_DATA,
		COL_NAME,
		NEW_VALUE_LONG,
		DATA_TYPE
		, KEY1, KEY2
	)
	SELECT
		@AUDIT_LOG_TRANSACTION_ID,
		convert(nvarchar(1500), IsNull('[ReferenceNumber]='+CONVERT(nvarchar(4000), NEW.[ReferenceNumber], 0), '[ReferenceNumber] Is Null') + ' AND ' + IsNull('[GuestNumber]='+CONVERT(nvarchar(4000), NEW.[GuestNumber], 0), '[GuestNumber] Is Null')),
		'PriceOverwritten2',
		CONVERT(nvarchar(4000), NEW.[PriceOverwritten2], 0),
		'A'
		, CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[ReferenceNumber], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[GuestNumber], 0))
	FROM inserted NEW
	
	  WHERE NEW.[PriceOverwritten2] IS NOT NULL
	
    
	INSERT INTO dbo.AUDIT_LOG_DATA
	(
		AUDIT_LOG_TRANSACTION_ID,
		PRIMARY_KEY_DATA,
		COL_NAME,
		NEW_VALUE_LONG,
		DATA_TYPE
		, KEY1, KEY2
	)
	SELECT
		@AUDIT_LOG_TRANSACTION_ID,
		convert(nvarchar(1500), IsNull('[ReferenceNumber]='+CONVERT(nvarchar(4000), NEW.[ReferenceNumber], 0), '[ReferenceNumber] Is Null') + ' AND ' + IsNull('[GuestNumber]='+CONVERT(nvarchar(4000), NEW.[GuestNumber], 0), '[GuestNumber] Is Null')),
		'PriceOverwritten3',
		CONVERT(nvarchar(4000), NEW.[PriceOverwritten3], 0),
		'A'
		, CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[ReferenceNumber], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[GuestNumber], 0))
	FROM inserted NEW
	
	  WHERE NEW.[PriceOverwritten3] IS NOT NULL
	
    
	INSERT INTO dbo.AUDIT_LOG_DATA
	(
		AUDIT_LOG_TRANSACTION_ID,
		PRIMARY_KEY_DATA,
		COL_NAME,
		NEW_VALUE_LONG,
		DATA_TYPE
		, KEY1, KEY2
	)
	SELECT
		@AUDIT_LOG_TRANSACTION_ID,
		convert(nvarchar(1500), IsNull('[ReferenceNumber]='+CONVERT(nvarchar(4000), NEW.[ReferenceNumber], 0), '[ReferenceNumber] Is Null') + ' AND ' + IsNull('[GuestNumber]='+CONVERT(nvarchar(4000), NEW.[GuestNumber], 0), '[GuestNumber] Is Null')),
		'PriceOverwritten4',
		CONVERT(nvarchar(4000), NEW.[PriceOverwritten4], 0),
		'A'
		, CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[ReferenceNumber], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[GuestNumber], 0))
	FROM inserted NEW
	
	  WHERE NEW.[PriceOverwritten4] IS NOT NULL
	
    
	INSERT INTO dbo.AUDIT_LOG_DATA
	(
		AUDIT_LOG_TRANSACTION_ID,
		PRIMARY_KEY_DATA,
		COL_NAME,
		NEW_VALUE_LONG,
		DATA_TYPE
		, KEY1, KEY2
	)
	SELECT
		@AUDIT_LOG_TRANSACTION_ID,
		convert(nvarchar(1500), IsNull('[ReferenceNumber]='+CONVERT(nvarchar(4000), NEW.[ReferenceNumber], 0), '[ReferenceNumber] Is Null') + ' AND ' + IsNull('[GuestNumber]='+CONVERT(nvarchar(4000), NEW.[GuestNumber], 0), '[GuestNumber] Is Null')),
		'FirstPrice1',
		CONVERT(nvarchar(4000), NEW.[FirstPrice1], 2),
		'A'
		, CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[ReferenceNumber], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[GuestNumber], 0))
	FROM inserted NEW
	
	  WHERE NEW.[FirstPrice1] IS NOT NULL
	
    
	INSERT INTO dbo.AUDIT_LOG_DATA
	(
		AUDIT_LOG_TRANSACTION_ID,
		PRIMARY_KEY_DATA,
		COL_NAME,
		NEW_VALUE_LONG,
		DATA_TYPE
		, KEY1, KEY2
	)
	SELECT
		@AUDIT_LOG_TRANSACTION_ID,
		convert(nvarchar(1500), IsNull('[ReferenceNumber]='+CONVERT(nvarchar(4000), NEW.[ReferenceNumber], 0), '[ReferenceNumber] Is Null') + ' AND ' + IsNull('[GuestNumber]='+CONVERT(nvarchar(4000), NEW.[GuestNumber], 0), '[GuestNumber] Is Null')),
		'FirstPrice2',
		CONVERT(nvarchar(4000), NEW.[FirstPrice2], 2),
		'A'
		, CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[ReferenceNumber], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[GuestNumber], 0))
	FROM inserted NEW
	
	  WHERE NEW.[FirstPrice2] IS NOT NULL
	
    
	INSERT INTO dbo.AUDIT_LOG_DATA
	(
		AUDIT_LOG_TRANSACTION_ID,
		PRIMARY_KEY_DATA,
		COL_NAME,
		NEW_VALUE_LONG,
		DATA_TYPE
		, KEY1, KEY2
	)
	SELECT
		@AUDIT_LOG_TRANSACTION_ID,
		convert(nvarchar(1500), IsNull('[ReferenceNumber]='+CONVERT(nvarchar(4000), NEW.[ReferenceNumber], 0), '[ReferenceNumber] Is Null') + ' AND ' + IsNull('[GuestNumber]='+CONVERT(nvarchar(4000), NEW.[GuestNumber], 0), '[GuestNumber] Is Null')),
		'FirstPrice3',
		CONVERT(nvarchar(4000), NEW.[FirstPrice3], 2),
		'A'
		, CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[ReferenceNumber], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[GuestNumber], 0))
	FROM inserted NEW
	
	  WHERE NEW.[FirstPrice3] IS NOT NULL
	
    
	INSERT INTO dbo.AUDIT_LOG_DATA
	(
		AUDIT_LOG_TRANSACTION_ID,
		PRIMARY_KEY_DATA,
		COL_NAME,
		NEW_VALUE_LONG,
		DATA_TYPE
		, KEY1, KEY2
	)
	SELECT
		@AUDIT_LOG_TRANSACTION_ID,
		convert(nvarchar(1500), IsNull('[ReferenceNumber]='+CONVERT(nvarchar(4000), NEW.[ReferenceNumber], 0), '[ReferenceNumber] Is Null') + ' AND ' + IsNull('[GuestNumber]='+CONVERT(nvarchar(4000), NEW.[GuestNumber], 0), '[GuestNumber] Is Null')),
		'FirstPrice4',
		CONVERT(nvarchar(4000), NEW.[FirstPrice4], 2),
		'A'
		, CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[ReferenceNumber], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[GuestNumber], 0))
	FROM inserted NEW
	
	  WHERE NEW.[FirstPrice4] IS NOT NULL
	
    
	INSERT INTO dbo.AUDIT_LOG_DATA
	(
		AUDIT_LOG_TRANSACTION_ID,
		PRIMARY_KEY_DATA,
		COL_NAME,
		NEW_VALUE_LONG,
		DATA_TYPE
		, KEY1, KEY2
	)
	SELECT
		@AUDIT_LOG_TRANSACTION_ID,
		convert(nvarchar(1500), IsNull('[ReferenceNumber]='+CONVERT(nvarchar(4000), NEW.[ReferenceNumber], 0), '[ReferenceNumber] Is Null') + ' AND ' + IsNull('[GuestNumber]='+CONVERT(nvarchar(4000), NEW.[GuestNumber], 0), '[GuestNumber] Is Null')),
		'TotalPrice1',
		CONVERT(nvarchar(4000), NEW.[TotalPrice1], 2),
		'A'
		, CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[ReferenceNumber], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[GuestNumber], 0))
	FROM inserted NEW
	
	  WHERE NEW.[TotalPrice1] IS NOT NULL
	
    
	INSERT INTO dbo.AUDIT_LOG_DATA
	(
		AUDIT_LOG_TRANSACTION_ID,
		PRIMARY_KEY_DATA,
		COL_NAME,
		NEW_VALUE_LONG,
		DATA_TYPE
		, KEY1, KEY2
	)
	SELECT
		@AUDIT_LOG_TRANSACTION_ID,
		convert(nvarchar(1500), IsNull('[ReferenceNumber]='+CONVERT(nvarchar(4000), NEW.[ReferenceNumber], 0), '[ReferenceNumber] Is Null') + ' AND ' + IsNull('[GuestNumber]='+CONVERT(nvarchar(4000), NEW.[GuestNumber], 0), '[GuestNumber] Is Null')),
		'TotalPrice2',
		CONVERT(nvarchar(4000), NEW.[TotalPrice2], 2),
		'A'
		, CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[ReferenceNumber], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[GuestNumber], 0))
	FROM inserted NEW
	
	  WHERE NEW.[TotalPrice2] IS NOT NULL
	
    
	INSERT INTO dbo.AUDIT_LOG_DATA
	(
		AUDIT_LOG_TRANSACTION_ID,
		PRIMARY_KEY_DATA,
		COL_NAME,
		NEW_VALUE_LONG,
		DATA_TYPE
		, KEY1, KEY2
	)
	SELECT
		@AUDIT_LOG_TRANSACTION_ID,
		convert(nvarchar(1500), IsNull('[ReferenceNumber]='+CONVERT(nvarchar(4000), NEW.[ReferenceNumber], 0), '[ReferenceNumber] Is Null') + ' AND ' + IsNull('[GuestNumber]='+CONVERT(nvarchar(4000), NEW.[GuestNumber], 0), '[GuestNumber] Is Null')),
		'TotalPrice3',
		CONVERT(nvarchar(4000), NEW.[TotalPrice3], 2),
		'A'
		, CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[ReferenceNumber], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[GuestNumber], 0))
	FROM inserted NEW
	
	  WHERE NEW.[TotalPrice3] IS NOT NULL
	
    
	INSERT INTO dbo.AUDIT_LOG_DATA
	(
		AUDIT_LOG_TRANSACTION_ID,
		PRIMARY_KEY_DATA,
		COL_NAME,
		NEW_VALUE_LONG,
		DATA_TYPE
		, KEY1, KEY2
	)
	SELECT
		@AUDIT_LOG_TRANSACTION_ID,
		convert(nvarchar(1500), IsNull('[ReferenceNumber]='+CONVERT(nvarchar(4000), NEW.[ReferenceNumber], 0), '[ReferenceNumber] Is Null') + ' AND ' + IsNull('[GuestNumber]='+CONVERT(nvarchar(4000), NEW.[GuestNumber], 0), '[GuestNumber] Is Null')),
		'TotalPrice4',
		CONVERT(nvarchar(4000), NEW.[TotalPrice4], 2),
		'A'
		, CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[ReferenceNumber], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[GuestNumber], 0))
	FROM inserted NEW
	
	  WHERE NEW.[TotalPrice4] IS NOT NULL
	
  
	INSERT INTO dbo.AUDIT_LOG_DATA
	(
		AUDIT_LOG_TRANSACTION_ID,
		PRIMARY_KEY_DATA,
		COL_NAME,
		NEW_VALUE_LONG,
		DATA_TYPE
		, KEY1, KEY2
	)
	SELECT
		@AUDIT_LOG_TRANSACTION_ID,
		convert(nvarchar(1500), IsNull('[ReferenceNumber]='+CONVERT(nvarchar(4000), NEW.[ReferenceNumber], 0), '[ReferenceNumber] Is Null') + ' AND ' + IsNull('[GuestNumber]='+CONVERT(nvarchar(4000), NEW.[GuestNumber], 0), '[GuestNumber] Is Null')),
		'PaysAdults',
		CONVERT(nvarchar(4000), NEW.[PaysAdults], 0),
		'A'
		, CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[ReferenceNumber], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[GuestNumber], 0))
	FROM inserted NEW
	
	  WHERE NEW.[PaysAdults] IS NOT NULL
	
    
	INSERT INTO dbo.AUDIT_LOG_DATA
	(
		AUDIT_LOG_TRANSACTION_ID,
		PRIMARY_KEY_DATA,
		COL_NAME,
		NEW_VALUE_LONG,
		DATA_TYPE
		, KEY1, KEY2
	)
	SELECT
		@AUDIT_LOG_TRANSACTION_ID,
		convert(nvarchar(1500), IsNull('[ReferenceNumber]='+CONVERT(nvarchar(4000), NEW.[ReferenceNumber], 0), '[ReferenceNumber] Is Null') + ' AND ' + IsNull('[GuestNumber]='+CONVERT(nvarchar(4000), NEW.[GuestNumber], 0), '[GuestNumber] Is Null')),
		'PaysChildren1',
		CONVERT(nvarchar(4000), NEW.[PaysChildren1], 0),
		'A'
		, CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[ReferenceNumber], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[GuestNumber], 0))
	FROM inserted NEW
	
	  WHERE NEW.[PaysChildren1] IS NOT NULL
	
    
	INSERT INTO dbo.AUDIT_LOG_DATA
	(
		AUDIT_LOG_TRANSACTION_ID,
		PRIMARY_KEY_DATA,
		COL_NAME,
		NEW_VALUE_LONG,
		DATA_TYPE
		, KEY1, KEY2
	)
	SELECT
		@AUDIT_LOG_TRANSACTION_ID,
		convert(nvarchar(1500), IsNull('[ReferenceNumber]='+CONVERT(nvarchar(4000), NEW.[ReferenceNumber], 0), '[ReferenceNumber] Is Null') + ' AND ' + IsNull('[GuestNumber]='+CONVERT(nvarchar(4000), NEW.[GuestNumber], 0), '[GuestNumber] Is Null')),
		'PaysChildren2',
		CONVERT(nvarchar(4000), NEW.[PaysChildren2], 0),
		'A'
		, CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[ReferenceNumber], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[GuestNumber], 0))
	FROM inserted NEW
	
	  WHERE NEW.[PaysChildren2] IS NOT NULL
	
    
	INSERT INTO dbo.AUDIT_LOG_DATA
	(
		AUDIT_LOG_TRANSACTION_ID,
		PRIMARY_KEY_DATA,
		COL_NAME,
		NEW_VALUE_LONG,
		DATA_TYPE
		, KEY1, KEY2
	)
	SELECT
		@AUDIT_LOG_TRANSACTION_ID,
		convert(nvarchar(1500), IsNull('[ReferenceNumber]='+CONVERT(nvarchar(4000), NEW.[ReferenceNumber], 0), '[ReferenceNumber] Is Null') + ' AND ' + IsNull('[GuestNumber]='+CONVERT(nvarchar(4000), NEW.[GuestNumber], 0), '[GuestNumber] Is Null')),
		'Day2',
		CONVERT(nvarchar(4000), NEW.[Day2], 0),
		'A'
		, CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[ReferenceNumber], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[GuestNumber], 0))
	FROM inserted NEW
	
	  WHERE NEW.[Day2] IS NOT NULL
	
    
	INSERT INTO dbo.AUDIT_LOG_DATA
	(
		AUDIT_LOG_TRANSACTION_ID,
		PRIMARY_KEY_DATA,
		COL_NAME,
		NEW_VALUE_LONG,
		DATA_TYPE
		, KEY1, KEY2
	)
	SELECT
		@AUDIT_LOG_TRANSACTION_ID,
		convert(nvarchar(1500), IsNull('[ReferenceNumber]='+CONVERT(nvarchar(4000), NEW.[ReferenceNumber], 0), '[ReferenceNumber] Is Null') + ' AND ' + IsNull('[GuestNumber]='+CONVERT(nvarchar(4000), NEW.[GuestNumber], 0), '[GuestNumber] Is Null')),
		'Day3',
		CONVERT(nvarchar(4000), NEW.[Day3], 0),
		'A'
		, CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[ReferenceNumber], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[GuestNumber], 0))
	FROM inserted NEW
	
	  WHERE NEW.[Day3] IS NOT NULL
	
    
	INSERT INTO dbo.AUDIT_LOG_DATA
	(
		AUDIT_LOG_TRANSACTION_ID,
		PRIMARY_KEY_DATA,
		COL_NAME,
		NEW_VALUE_LONG,
		DATA_TYPE
		, KEY1, KEY2
	)
	SELECT
		@AUDIT_LOG_TRANSACTION_ID,
		convert(nvarchar(1500), IsNull('[ReferenceNumber]='+CONVERT(nvarchar(4000), NEW.[ReferenceNumber], 0), '[ReferenceNumber] Is Null') + ' AND ' + IsNull('[GuestNumber]='+CONVERT(nvarchar(4000), NEW.[GuestNumber], 0), '[GuestNumber] Is Null')),
		'Day4',
		CONVERT(nvarchar(4000), NEW.[Day4], 0),
		'A'
		, CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[ReferenceNumber], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[GuestNumber], 0))
	FROM inserted NEW
	
	  WHERE NEW.[Day4] IS NOT NULL
	
    
	
	-- Lookup 
	

	-- 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
