-1

In my database I have one table which when there is a record inserted into it a trigger will use information from the data inserted, run a select statement on the rest of the database, and insert the result into another database on the same server.

Ive never written a trigger before and Im not entirely sure that the examples out there help a lot.

Thanks in advance

Brownd92
  • 75
  • 1
  • 11
  • 1
    This does not show any research effort or provide any details about what is expected. – Sean Lange Feb 17 '16 at 16:39
  • 1
    Try this. https://msdn.microsoft.com/en-us/library/ms189799.aspx Make sure you don't add scalar variables in your trigger. It should be set based and it should utilize at least one of the two virtual tables (inserted and deleted) – Sean Lange Feb 17 '16 at 16:40
  • Thanks Sean, apologies for the vagueness – Brownd92 Feb 17 '16 at 16:54

1 Answers1

2

its always good to start learning ...here is some sample code and some links to start ..

create trigger <<give your triggername>>
on 
<<give your tablename>>
after insert
as 
begin

select * from inserted--gives me data inserted

--run on rest of database--your logic goes here
select * from inserted i
join
some table tbl
on 
tbl.id=i.id

insert into anotherdatabase.dbo.anotherdatabasetable
select * from inserted

end

Also please be aware that ,if the above trigger fails due to some reason ,your insert also will be rolled back.. some good links:
http://www.sqlteam.com/article/an-introduction-to-triggers-part-i
Insert Update trigger how to determine if insert or update
sql server trigger

Community
  • 1
  • 1
TheGameiswar
  • 27,855
  • 8
  • 56
  • 94