Introducing DotNetFunda.com on mobile http://m.dotnetfunda.com ! Be with DotNetFunda.com on the go !
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 1995 |  Welcome, Guest!   Register  Login
Home > Articles > Sql Server > Triggers in SQL

Triggers in SQL

Article posted by Abhisek on 11/27/2009 | Views: 3037 | Category: Sql Server | Level: Beginner red flag


This article describes you how to use triggers in SQL.

Trigger


        A trigger is a special kind of stored procedure that executes automatically when a user attempts the specified data modification statement on the specified table. Miccrosoft SQl server allows the creation of triggers for any given INSERT, UPDATE or DELETE statement.

Trigger for INSERT


Syntax

CREATE TRIGGER trigger_name ON table_name FOR INSERT AS SQLStatement.

trigger_name:- It is the name of the trigger. A trigger name must confirm to the rules for identifiers and must be unique within the database.

table_name:- It is the name of the table on which the trigger is executed and is sometimes called trigger table.

SQLStatement:- The trigger action specified in the Transact-SQL statements go into effect when INSERT, DELETE or UPDATE operation is attempted.

Example1:-
CREATE TRIGGER ti1 ON Student FOR INSERT AS Print 'Record inserted successfully' // This will add trigger for insert operation in table student.

Firing a trigger
INSERT Student VALUES("Abhisek", 108)
The output will be,

Record inserted successfully
1 row(s) affected

Example2:-
CREATE TRIGGER ti2 ON Student FOR INSERT AS select*from inserted

        Inserted is a logical table and structure is similar to the table on which the trigger is defined. That is the table on which the user action is attempted, and new values of the rows that may be changed by the user action.

INSERT Student VALUES("Abinash", 107)

Name            Roll
Abinash         107
1 row(s) affected

Trigger for DELETE



Syntax

CREATE TRIGGER trigger_name ON table_name FOR DELETE AS SQLStatement.
Example1
CREATE TRIGGER td1 ON Student FOR DELETE AS 'Record deleted successfully' 
 This will add a trigger on table student for delete command
DELETE FROM Student WHERE Roll=107
Record inserted successfully
1 row(s) affected

Example2:-
CREATE TRIGGER td2 ON Student FOR DELETE AS select*from deleted

           Deleted
is a logical table and the structure is similar to the table on which the trigger is defined. That is, the table on which the user action is attempted, and the old values of the rows that may be changed by the user action.

DELETE FROM Student WHERE Roll=107

Name              Roll
Abinash           107
1 row(s) affected

Trigger for Update



Syntax:-

CREATE TRIGGER trigger_name ON table_name FOR UPDATE AS SQLStatement.
Example1:-
CREATE TRIGGER tu1 ON Student FOR UPDATE AS 'Record updated successfully' 

This will add a trigger on table student for delete command

UPDATE Student set Name='Abhisek Panda' whwre roll=108
Record updated successfully
1 row(s) affected

Example2:-
CREATE TRIGGER tu2 ON Student FOR UPDATE AS select*from deleted select*from inserted

         Deleted and inserted are logical tables and are structurally similar to the table on which the trigger is defined. That is, the table on which the user action is attempted, and hold the old values and new values of the rows that may be changed by the user action.

UPDATE Student set Name='Abhisek Panda' whwre roll=108
Name              Roll
Abhisek           108

Name                    Roll
Abhisek Panda     108
1 row(s) affected


NB:- First you have to create and execute that trigger and then you have to fire it.

If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Page copy protected against web site content infringement by Copyscape
Found interesting? Add this to:



Please Sign In to vote for this post.

Experience:0 year(s)
Home page:
Member since:Sunday, October 11, 2009
Level:Bronze
Status: [Member]
Biography:Thanks and Regards
Abhisek Panda
Go Green And Save Your Future
 Responses
Posted by: Raja | Posted on: 13 Feb 2011 10:57:02 PM | Points: 25

Short and sweet article, for in depth knowledge on triggers people can visit http://msdn.microsoft.com/en-us/library/ms189799.aspx.

Thanks!

>> Write Response - Respond to this post and get points
Related Posts

This is part 1 of series of article on SSRS and we shall know basics on how to create a report in SSRS.

DataBase Transaction

This article enable you to learn how to use Column Index Number instead of Column Name in the order by clause and where it should be used.

To make SQL Queries Case Sensitive using Collation. You can make SQL Queries Case sensitive at the column level or Table level or DataBase level

Were you ever in need of debugging a stored procedure in SQL Server 2005. Any .NET Developer is well versed with debugging .NET applications. The .pdb files will be used internally to serve the purpose. Pdb files will have the mapping information from Native to MSIL and ultimately to the .NET code. I will not get into the internals of how this happens as this article falls under the SQL Server category.

More ...
About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you found plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/21/2012 7:23:33 AM