Groups | Blog | Home
all groups > sql server new users > november 2005 >

sql server new users : Triggers in 2005, they changed it <GGGG>


Bob
11/17/2005 6:13:57 PM
Been doing triggers for years in sql 2000 now I find I got triggers that =
worked in a previous versions but I can no longer just pick up and =
paste my code in sql 2005 triggers. One sample.
I'm in mydb on myserver and have a table mytable with two fields =
LastupdateOn and LastUpdateBy, I want to update these witha trigger. So =
I right click on triggers in my database, select new trigger, now I get =
this template that I fill out.=20

-- =
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D

-- Template generated from Template Explorer using:

-- Create Trigger (New Menu).SQL

--

-- Use the Specify Values for Template Parameters=20

-- command (Ctrl-Shift-M) to fill in the parameter=20

-- values below.

--

-- See additional Create Trigger templates for more

-- examples of different Trigger statements.

--

-- This block of comments will not be included in

-- the definition of the function.

-- =
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D

SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO

-- =
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D

-- Author: Robert Dufour

-- Create date: Nov 17 2005

-- Description: Used to set the lastupdate fields

-- =
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D

CREATE TRIGGER trg_Mytable_ins_upd=20

ON Mytable

AFTER INSERT,UPDATE

AS=20

BEGIN

-- SET NOCOUNT ON added to prevent extra result sets from

-- interfering with SELECT statements.

SET NOCOUNT ON;

-- Insert statements for trigger here

Update Mytable

Set LastUpdateBy =3D Suser_sname(),

LastUpdateOn =3D GetDate()

Where Exists (select * from inserted where inserted.Id=3D Mytable.Id)

END

GO

This worked fine in 2000, in 2005 now I find I have to execute this =
query to create the trigger and when I do I get following error message

Msg 8197, Level 16, State 4, Procedure trg_Mytable_ins_upd , Line 6

Object 'Mytable' does not exist or is invalid for this operation.





What gives here? Why does Mytable that I have already created and is =
correct not get recognized. What do I need to do to create a trigger =
now. Any help appreciated.



Mike Hodgson
11/18/2005 12:00:00 AM
Works fine for me, although I would probably have written the trigger
like this myself:

create trigger trg_Mytable_ins_upd on Mytable
for insert, update
as
set nocount on;

update mytable
set LastUpdateBy = SYSTEM_USER,
LastUpdateOn = CURRENT_TIMESTAMP
from mytable t
inner join inserted i on i.ID = t.ID;

set nocount off;
go

It may be a dumb question, but are you sure the mytable table exists
before you run the create trigger statement? Is mytable actually a
table or is it a view? Can you post the schema for mytable? Are you in
the correct DB at the time you try to execute the create trigger statement?

--
*mike hodgson*
blog: http://sqlnerd.blogspot.com



[quoted text, click to view]
GregO
11/18/2005 12:00:00 AM
Hi Bob,
The syntax seems find. =20

Could you be in the wrong database? Please post he DDL and I can test =
some more.

kind regards
Greg O
-------
Looking to use CLR in SQL 2005. Try some pre-build CLR Functions and SP
AGS SQL 2005 Utilities, over 20+ functions
http://www.ag-software.com/?tabid=3D38

[quoted text, click to view]
Been doing triggers for years in sql 2000 now I find I got triggers =
that worked in a previous versions but I can no longer just pick up and =
paste my code in sql 2005 triggers. One sample.
I'm in mydb on myserver and have a table mytable with two fields =
LastupdateOn and LastUpdateBy, I want to update these witha trigger. So =
I right click on triggers in my database, select new trigger, now I get =
this template that I fill out.=20

-- =
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D

-- Template generated from Template Explorer using:

-- Create Trigger (New Menu).SQL

--

-- Use the Specify Values for Template Parameters=20

-- command (Ctrl-Shift-M) to fill in the parameter=20

-- values below.

--

-- See additional Create Trigger templates for more

-- examples of different Trigger statements.

--

-- This block of comments will not be included in

-- the definition of the function.

-- =
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D

SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO

-- =
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D

-- Author: Robert Dufour

-- Create date: Nov 17 2005

-- Description: Used to set the lastupdate fields

-- =
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D

CREATE TRIGGER trg_Mytable_ins_upd=20

ON Mytable

AFTER INSERT,UPDATE

AS=20

BEGIN

-- SET NOCOUNT ON added to prevent extra result sets from

-- interfering with SELECT statements.

SET NOCOUNT ON;

-- Insert statements for trigger here

Update Mytable

Set LastUpdateBy =3D Suser_sname(),

LastUpdateOn =3D GetDate()

Where Exists (select * from inserted where inserted.Id=3D Mytable.Id)

END

GO

This worked fine in 2000, in 2005 now I find I have to execute this =
query to create the trigger and when I do I get following error message

Msg 8197, Level 16, State 4, Procedure trg_Mytable_ins_upd , Line 6

Object 'Mytable' does not exist or is invalid for this operation.





What gives here? Why does Mytable that I have already created and is =
correct not get recognized. What do I need to do to create a trigger =
now. Any help appreciated.



Bob
11/28/2005 11:16:41 AM
Thanks to you both, I tried again and now it seems to work OK. NO =
change, weird. I must have done something wrong but still don't know =
what.
Oh well. Thanks=20
Bob

[quoted text, click to view]
Been doing triggers for years in sql 2000 now I find I got triggers =
that worked in a previous versions but I can no longer just pick up and =
paste my code in sql 2005 triggers. One sample.
I'm in mydb on myserver and have a table mytable with two fields =
LastupdateOn and LastUpdateBy, I want to update these witha trigger. So =
I right click on triggers in my database, select new trigger, now I get =
this template that I fill out.=20

-- =
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D

-- Template generated from Template Explorer using:

-- Create Trigger (New Menu).SQL

--

-- Use the Specify Values for Template Parameters=20

-- command (Ctrl-Shift-M) to fill in the parameter=20

-- values below.

--

-- See additional Create Trigger templates for more

-- examples of different Trigger statements.

--

-- This block of comments will not be included in

-- the definition of the function.

-- =
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D

SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO

-- =
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D

-- Author: Robert Dufour

-- Create date: Nov 17 2005

-- Description: Used to set the lastupdate fields

-- =
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D

CREATE TRIGGER trg_Mytable_ins_upd=20

ON Mytable

AFTER INSERT,UPDATE

AS=20

BEGIN

-- SET NOCOUNT ON added to prevent extra result sets from

-- interfering with SELECT statements.

SET NOCOUNT ON;

-- Insert statements for trigger here

Update Mytable

Set LastUpdateBy =3D Suser_sname(),

LastUpdateOn =3D GetDate()

Where Exists (select * from inserted where inserted.Id=3D Mytable.Id)

END

GO

This worked fine in 2000, in 2005 now I find I have to execute this =
query to create the trigger and when I do I get following error message

Msg 8197, Level 16, State 4, Procedure trg_Mytable_ins_upd , Line 6

Object 'Mytable' does not exist or is invalid for this operation.





What gives here? Why does Mytable that I have already created and is =
correct not get recognized. What do I need to do to create a trigger =
now. Any help appreciated.



AddThis Social Bookmark Button