Click here to Skip to main content
15,885,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I would like to seek advice, if you can create sql trigger, which get data from table A, into trigger table, if there has been insert created into table B.

if so, is there example or tutorial I can follow, which cater to this task?

Many thanks
Posted
Comments
Thanks7872 29-Aug-14 6:02am    
Search google for Triggers in SQL, thats it. Tons of Tutorials available out there.

Google is your friend here: a simple search "SQL INSERT TRIGGER" found it very, very quickly:
https://www.google.co.uk/search?q=sql+insert+trigger&oq=sql+insert+trigger&aqs=chrome..69i57j0l5.4881j0j7&sourceid=chrome&es_sm=0&ie=UTF-8[^]

Right at the top of the list:
http://msdn.microsoft.com/en-gb/library/ms189799.aspx[^]

In future, please try to do at least basic research yourself, and not waste your time or ours.
 
Share this answer
 
You can write a trigger is fired after an INSERT on the table. Let’s create the trigger as:
CREATE TRIGGER trgAfterInsert ON [dbo].[Employee_Test] 
FOR INSERT
AS
	declare @empid int;
	declare @empname varchar(100);
	declare @empsal decimal(10,2);
	declare @audit_action varchar(100);

        -- Collect data from Table A
	select @empid=i.Emp_ID from inserted i;	
	select @empname=i.Emp_Name from inserted i;	
	select @empsal=i.Emp_Sal from inserted i;	
	set @audit_action='Inserted Record -- After Insert Trigger.';

        -- Insert to second table b by declared variable
	insert into Employee_Test_Audit
           (Emp_ID,Emp_Name,Emp_Sal,Audit_Action,Audit_Timestamp) 
	values(@empid,@empname,@empsal,@audit_action,getdate());

	PRINT 'AFTER INSERT trigger fired.'

Get More info msdn[^]
 
Share this answer
 
Comments
miss786 29-Aug-14 8:09am    
Thanks this is great help. Many thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900