|
database
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Simple trigger questionGiven the table.. CREATE TABLE [dbo].[tblTest] ( [KeyCol] [char] (10) COLLATE Latin1_General_BIN NULL , [Test1] [char] (10) COLLATE Latin1_General_BIN NULL , [Test2] [char] (10) COLLATE Latin1_General_BIN NULL ) ON [PRIMARY] GO If a change is made to column Test1... then update column Test2 with the value in Test1 Sql server will not allow me to even save the following trigger due to the error below... what am I doing wrong ? The error message is - Error 107: The column prefix "Inserted" does not match with a table name or alias name use in the query. CREATE TRIGGER trg ON [dbo].[tblTest] FOR INSERT, UPDATE AS if Update(Test1) begin update tblTest Set Test2 = Test1 where tblTest.KeyCol = Inserted.KeyCol end Thanks ! Try this:
CREATE TRIGGER trg ON [dbo].[tblTest] FOR INSERT, UPDATE AS BEGIN IF UPDATE(Test1) BEGIN UPDATE tblTest SET Test2 = Test1 FROM inserted i INNER JOIN tblTest t ON t.KeyCol = i.KeyCol END END Thanks Chris...
But now the error reads Error 209: Ambiguous column name "Test1" Show quote "Chris Lim" <blackca***@hotmail.com> wrote in message news:1156377123.195247.166880@i42g2000cwa.googlegroups.com... > Try this: > > CREATE TRIGGER trg ON [dbo].[tblTest] > FOR INSERT, UPDATE > AS > BEGIN > IF UPDATE(Test1) > BEGIN > UPDATE tblTest > SET Test2 = Test1 > FROM inserted i > INNER JOIN tblTest t > ON t.KeyCol = i.KeyCol > END > END > Either change Chris's solution to:
CREATE TRIGGER trg ON [dbo].[tblTest] FOR INSERT, UPDATE AS BEGIN IF UPDATE(Test1) BEGIN UPDATE tblTest SET Test2 = i.Test1 FROM inserted i INNER JOIN tblTest t ON t.KeyCol = i.KeyCol END END Or use the following which uses the ANSI standard for UPDATE instead of the SQL Server extentions CREATE TRIGGER trg ON [dbo].[tblTest] FOR INSERT, UPDATE AS If Update(Test1) Begin Update tblTest Set Test2 = (Select i.Test1 From inserted i Where tblTest.KeyCol = i.KeyCol) End Tom Show quote "Rob" <rwch***@comcast.net> wrote in message news:aoOdnadOT4hNZnHZnZ2dnUVZ_qOdnZ2d@comcast.com... > Thanks Chris... > But now the error reads Error 209: Ambiguous column name "Test1" > > "Chris Lim" <blackca***@hotmail.com> wrote in message > news:1156377123.195247.166880@i42g2000cwa.googlegroups.com... >> Try this: >> >> CREATE TRIGGER trg ON [dbo].[tblTest] >> FOR INSERT, UPDATE >> AS >> BEGIN >> IF UPDATE(Test1) >> BEGIN >> UPDATE tblTest >> SET Test2 = Test1 >> FROM inserted i >> INNER JOIN tblTest t >> ON t.KeyCol = i.KeyCol >> END >> END >> > > On Wed, 23 Aug 2006 21:48:59 -0400, "Tom Cooper"
<tomcooper@comcast.no.spam.please.net> wrote: >Or use the following which uses the ANSI standard for UPDATE instead of the NOT good. This version will update every row that has no match in>SQL Server extentions > >CREATE TRIGGER trg ON [dbo].[tblTest] >FOR INSERT, UPDATE >AS >If Update(Test1) >Begin > Update tblTest Set Test2 = (Select i.Test1 From inserted i Where >tblTest.KeyCol = i.KeyCol) >End INSERTED to NULL. UPDATE tblTest SET Test2 = (Select i.Test1 From inserted i Where tblTest.KeyCol = i.KeyCol) WHERE EXISTS (Select * From inserted i2 Where tblTest.KeyCol = i2.KeyCol) Roy Harvey Beacon Falls, CT You're right. Thanks.
Tom Show quote "Roy Harvey" <roy_har***@snet.net> wrote in message news:272qe2dq92jutrg1j2qlbj3n40o5g8he69@4ax.com... > On Wed, 23 Aug 2006 21:48:59 -0400, "Tom Cooper" > <tomcooper@comcast.no.spam.please.net> wrote: > >>Or use the following which uses the ANSI standard for UPDATE instead of >>the >>SQL Server extentions >> >>CREATE TRIGGER trg ON [dbo].[tblTest] >>FOR INSERT, UPDATE >>AS >>If Update(Test1) >>Begin >> Update tblTest Set Test2 = (Select i.Test1 From inserted i Where >>tblTest.KeyCol = i.KeyCol) >>End > > NOT good. This version will update every row that has no match in > INSERTED to NULL. > > UPDATE tblTest > SET Test2 = > (Select i.Test1 From inserted i > Where tblTest.KeyCol = i.KeyCol) > WHERE EXISTS > (Select * From inserted i2 > Where tblTest.KeyCol = i2.KeyCol) > > Roy Harvey > Beacon Falls, CT |
|||||||||||||||||||||||