Solved

trigger

Posted on 2011-09-07
4
291 Views
Last Modified: 2012-05-12
I have two tables, table A and table B. When ever row is updated , inserted or deleted in table A, same row should be updated , inserted and deleted in table B. Want to write insert , update and delete after trigger on table A and in trigger update data in table b.

thanks,
Ravi
0
Comment
Question by:asrithap
  • 2
4 Comments
 
LVL 39

Expert Comment

by:Pratima Pharande
Comment Utility
0
 

Author Comment

by:asrithap
Comment Utility
Can you give me examples for update, insert and delete trigger.
0
 
LVL 9

Expert Comment

by:mimran18
Comment Utility
0
 
LVL 9

Accepted Solution

by:
mimran18 earned 500 total points
Comment Utility
Here is the sample.

 
Drop table test1
Go
Create table test1
(ID int,
[SName] nvarchar(50),
)
Go

Drop table test2
Go
Create table test2
(ID int,
[SName] nvarchar(50),
)
Go

Open in new window

Drop TRIGGER test_INSERT 
GO
CREATE TRIGGER test_INSERT 
ON test1
FOR INSERT, UPDATE, DELETE AS 
 
SET NOCOUNT ON; 

-- 
-- Variables Needed for this Trigger 
-- 
DECLARE @ID int

DECLARE @SName varchar(50) 

-- -- Determine if this is an INSERT,UPDATE, or DELETE Action --

DECLARE @Action as char(1) 
DECLARE @Count as int 
SET @Action = 'I' 
-- Set Action to 'I'nsert by default. 
SELECT @Count = COUNT(*) FROM DELETED 
if @Count > 0     
	BEGIN         
		SET @Action = 'D' -- Set Action to 'D'eleted.         
		SELECT @Count = COUNT(*) FROM INSERTED         
		IF @Count > 0 SET @Action = 'U' -- Set Action to 'U'pdated.     
	END

if @Action = 'D'     
-- This is a DELETE Record Action     
--     
BEGIN         
	SELECT @ID =[ID]                     
    FROM DELETED  
        
	DELETE [dbo].test2         
	WHERE [ID]    =@ID 

	END  
Else     
BEGIN             
		--             
		-- Table INSERTED is common to both the INSERT, UPDATE trigger             
		--             
		SELECT @ID =[ID]                 
		,@Sname = [Sname] FROM INSERTED      
      
		if @Action = 'I'-- This is an Insert Record Action             
		--             
		BEGIN                 
				insert test2 SELECT  [ID],[SName] FROM inserted           
		END         
		else             
		-- This is an Update Record Action             
		--             
		BEGIN                 
				UPDATE test2            
				SET [Sname] = @Sname 
				WHERE [ID]=@ID 
    	END 
END

Open in new window


 
Insert  into test1 values (1,'John')
Insert  into test1 values (2,'Robert')
Insert  into test1 values (3,'William')
GO
Select * from test1
Select * from test2
GO
Update [test1] set [Sname]='Robinson' where [ID]=2
GO
Select * from test1
Select * from test2
GO
Delete from test1 Where [ID]=1
GO
Select * from test1
Select * from test2

Open in new window

0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now