Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi all,

When and why we use trigger in SQL Server ?Can anybody explain it with giving suitable examples.


Thanks
Posted

Why not use Google[^]?
 
Share this answer
 
Comments
Abhinav S 22-Aug-11 1:27am    
My 5.
walterhevedeich 22-Aug-11 2:00am    
Thanks Abhinav.
Sergey Alexandrovich Kryukov 22-Aug-11 1:58am    
Why not, indeed? Not suitable for test/interview question? My 5.
--SA
walterhevedeich 22-Aug-11 2:01am    
Thanks SA.
You might want to pick up a book on SQL and read it.
It would help you understand the basics of triggers.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 22-Aug-11 1:58am    
Education needed :-) My 5.
--SA
Abhinav S 22-Aug-11 1:59am    
Thank you SA.
If u have dependency in 2 table and Change in I table requires a Changes in other Apply trigger to table I which will make Changes in II one
SQL
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go

-- =============================================
-- Author:      <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER TRIGGER [<Trigger Name>]
   ON  [dbo].[<Table name where Change Occurs>]
   FOR INSERT
AS
DECLARE @Inserted_ID INT
BEGIN
    SET @InsertedID  = (SELECT FK_ID FROM INSERTED)
    -- Target Table for Trigger Will Update Some Count in Other Table
   Update  TrnTarget -- Table Name
     SET testColName= testColName+1
    WHERE FK_ID = @Inserted_ID

END
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900