dbTalk Databases Forums  

Create One Trigger For Both Update and Delete

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss Create One Trigger For Both Update and Delete in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
satish
 
Posts: n/a

Default Create One Trigger For Both Update and Delete - 04-30-2007 , 08:02 AM






hi,
CAn i have one trigger for both Update and Delete
Delete Trigger
---------------------
create Trigger [tr_delete_user_log]
on [dbo].[user_log] for delete
as
begin
insert into z_user_log select * from deleted
end

Trigger Update
---------------------
CREATE Trigger [tr_update_user_log]
on [dbo].[user_log] for update
as
begin
insert into z_user_log select * from deleted
end

Can i have one trigger instead of these Triggers ..


Reply With Quote
  #2  
Old   
Hugo Kornelis
 
Posts: n/a

Default Re: Create One Trigger For Both Update and Delete - 04-30-2007 , 03:10 PM






On 30 Apr 2007 06:02:57 -0700, satish wrote:

Quote:
hi,
CAn i have one trigger for both Update and Delete
(snip)
Can i have one trigger instead of these Triggers ..
Hi satish,

Yes.

CREATE Trigger [tr_update_delete_user_log]
on [dbo].[user_log] for update, delete
as
begin
insert into z_user_log select * from deleted
end


--
Hugo Kornelis, SQL Server MVP
My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis


Reply With Quote
  #3  
Old   
Erland Sommarskog
 
Posts: n/a

Default Re: Create One Trigger For Both Update and Delete - 04-30-2007 , 04:31 PM



satish (satishkumar.gourabathina (AT) gmail (DOT) com) writes:
Quote:
CAn i have one trigger for both Update and Delete
Delete Trigger
---------------------
create Trigger [tr_delete_user_log]
on [dbo].[user_log] for delete
as
begin
insert into z_user_log select * from deleted
end
As Hugo said, you can. Permit me to point that your example exhibits
two cases of bad practice:

o INSERT without a values list. If someone adds a column to user_log,
the INSERT statement will fail.
o SELECT *. While convenient for ad hoc queries, it's bad in production
code. In this example - if someone adds or removes a column - or
just changes the column order, the INSERT statement will fail. SELECT *
also make it more difficult to find where different columns are
actually used.


--
Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx


Reply With Quote
  #4  
Old   
Hugo Kornelis
 
Posts: n/a

Default Re: Create One Trigger For Both Update and Delete - 05-01-2007 , 04:02 PM



On Mon, 30 Apr 2007 21:31:28 +0000 (UTC), Erland Sommarskog wrote:

Quote:
Permit me to point that your example exhibits
two cases of bad practice:
(snip)

Hi Erland,

Thanks for stepping in. I new feel so bad for not mentioning that
myself. Please remind me not to reply when tired in the future :-)

--
Hugo Kornelis, SQL Server MVP
My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.