C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




INSERT and UPDATE using Triggers in SQL Server


Posted Date: 20 Jun 2008    Resource Type: Code Snippets    Category: SQL

Posted By: Sabarees       Member Level: Gold
Rating:     Points: 8



This code sample explains how to create triggers when a particular table, "employee" in this example, has insert and update operations done on it. This example uses SQL Server database.


Create Trigger UpdateEmployee
On Employee
For Insert,Update
As
Declare @Emp_no As Int;
Declare @Emp_name As Varchar(50);
Declare @Emp_age As Int;
Declare @Dept_no As Int;

Select @Emp_no = i.empno From inserted i;
Select @Emp_name = i.emp_name From inserted i;
Select @Emp_age = i.emp_age From inserted i;
Select @Dept_no = i.Dept_no From inserted i;

If Exists(Select empno From employee1 Where empno = @Emp_no)
Begin
Update employee1 Set emp_name=@Emp_name,emp_age=@Emp_age,Dept_no=@Dept_no Where empno= @Emp_no
End
Else
Begin
IF (@Emp_no Is NULL And @Emp_name Is NULL And @Emp_age Is NULL)
Begin
Print 'Empty Values Not Inserted'
End
Else
Begin
Insert Into employee1(empno,emp_name,emp_age,Dept_no) Values (@Emp_no,@Emp_name,@Emp_age,@Dept_no)
End
End
Go




Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Using Triggers in SQL Server  .  Triggers on Insert and Update  .  Triggers in SQL Server  .  Trigger on Update  .  Trigger on Insert  .  Creating Triggers in SQL Server  .  Creating Triggers  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Rolling back Delete and Truncate commands
Previous Resource: How to insert values from existing table into new table with same table structure?
Return to Discussion Resource Index
Post New Resource
Category: SQL


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

email fax service

Contact Us    Privacy Policy    Terms Of Use