Solved

trigger

Posted on 2011-09-07
4
293 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
ID: 36500906
0
 

Author Comment

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

Expert Comment

by:mimran18
ID: 36501021
0
 
LVL 9

Accepted Solution

by:
mimran18 earned 500 total points
ID: 36501039
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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

773 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