Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

trigger

Posted on 2011-09-07
4
294 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

809 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