I am creating a versioning scheme using a trigger and I do not want to insert a new row into the audit table unless the article is live. I have a column called live which is a bit field. So, if LIVE is true I want the row inserted. I tried putting an if statement around the Insert statement but I'm getting this error message back.
Msg 207, Level 16, State 1, Procedure Tier1_ChangeTracking, Line 40
Invalid column name 'Live'.
Msg 207, Level 16, State 1, Procedure Tier1_ChangeTracking, Line 40
Invalid column name 'true'.
Thank you for your help. Here is my code.
USE [GSIGHub]
GO
/****** Object: Trigger [dbo].[Tier1_ChangeTracking] Script Date: 03/11/2010 09:15:40 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER trigger [dbo].[Tier1_ChangeTracking] on [dbo].[Tier1] for insert, update, delete
as
declare @AuditType char(1), @PKCols VARCHAR(MAX), @SQL VARCHAR(MAX)
--Find the Primary keys to be used in the inserted and deleted outer join
select @PKCols = coalesce(@PKCols + ' and', ' on') + ' i.' + c.COLUMN_NAME + ' = d.' + c.COLUMN_NAME
from INFORMATION_SCHEMA.TABLE_CONSTRAINTS pk ,
INFORMATION_SCHEMA.KEY_COLUMN_USAGE c
where pk.TABLE_NAME = 'Tier1'
and CONSTRAINT_TYPE = 'PRIMARY KEY'
and c.TABLE_NAME = pk.TABLE_NAME
and c.CONSTRAINT_NAME = pk.CONSTRAINT_NAME
select * into #ins from inserted
select * into #del from deleted
if exists (select * from inserted)
if exists (select * from deleted)
SET @AuditType = 'U'
else
SET @AuditType = 'I'
else
SET @AuditType = 'D'
IF (Live = true)
BEGIN
EXEC('INSERT INTO Tier1_AUDIT (T1ID, T1Name, althline, ShortName, class, Alt_text, header, Description, Content, BypassUrl, ButtonLoc, FrontPage, Live, allowcontent, datecreated, dateupdated, groupmember, headerpic, keywords, tier, oldT1ID, oldT1Name, oldalthline, oldShortName, oldclass, oldAlt_text, oldheader, oldDescription, oldContent, oldBypassUrl, oldButtonLoc, oldFrontPage, oldLive, oldallowcontent, olddatecreated, olddateupdated, oldgroupmember, oldheaderpic, oldkeywords, oldtier, AuditType)
SELECT i.*, d.*, ''' + @AuditType + ''' FROM #ins i full outer join #del d ' + @PKCols )
END
Msg 207, Level 16, State 1, Procedure Tier1_ChangeTracking, Line 40
Invalid column name 'Live'.
Msg 207, Level 16, State 1, Procedure Tier1_ChangeTracking, Line 40
Invalid column name 'true'.
Thank you for your help. Here is my code.
USE [GSIGHub]
GO
/****** Object: Trigger [dbo].[Tier1_ChangeTracking] Script Date: 03/11/2010 09:15:40 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER trigger [dbo].[Tier1_ChangeTracking] on [dbo].[Tier1] for insert, update, delete
as
declare @AuditType char(1), @PKCols VARCHAR(MAX), @SQL VARCHAR(MAX)
--Find the Primary keys to be used in the inserted and deleted outer join
select @PKCols = coalesce(@PKCols + ' and', ' on') + ' i.' + c.COLUMN_NAME + ' = d.' + c.COLUMN_NAME
from INFORMATION_SCHEMA.TABLE_CONSTRAINTS pk ,
INFORMATION_SCHEMA.KEY_COLUMN_USAGE c
where pk.TABLE_NAME = 'Tier1'
and CONSTRAINT_TYPE = 'PRIMARY KEY'
and c.TABLE_NAME = pk.TABLE_NAME
and c.CONSTRAINT_NAME = pk.CONSTRAINT_NAME
select * into #ins from inserted
select * into #del from deleted
if exists (select * from inserted)
if exists (select * from deleted)
SET @AuditType = 'U'
else
SET @AuditType = 'I'
else
SET @AuditType = 'D'
IF (Live = true)
BEGIN
EXEC('INSERT INTO Tier1_AUDIT (T1ID, T1Name, althline, ShortName, class, Alt_text, header, Description, Content, BypassUrl, ButtonLoc, FrontPage, Live, allowcontent, datecreated, dateupdated, groupmember, headerpic, keywords, tier, oldT1ID, oldT1Name, oldalthline, oldShortName, oldclass, oldAlt_text, oldheader, oldDescription, oldContent, oldBypassUrl, oldButtonLoc, oldFrontPage, oldLive, oldallowcontent, olddatecreated, olddateupdated, oldgroupmember, oldheaderpic, oldkeywords, oldtier, AuditType)
SELECT i.*, d.*, ''' + @AuditType + ''' FROM #ins i full outer join #del d ' + @PKCols )
END