Solved

SQL Trigger on insert into an other table in a different database

Posted on 2008-06-25
5
4,017 Views
Last Modified: 2012-06-21
We have  a jobs table. On insert of any data, we want the data copied to the similar table in a different database in the same server. what is the syntax for writing insert trigger?
0
Comment
Question by:newoffice
  • 3
  • 2
5 Comments
 
LVL 60

Expert Comment

by:chapmandew
ID: 21868287
create trigger tr_triggername on tablename
for insert
as
begin
print 'do something here...put code in'
end
0
 
LVL 1

Author Comment

by:newoffice
ID: 21868482
how to copy all the fields including id filed and insert in the same table in different database?
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 21868511
here you go.  just swap in your field names where I have fieldlist mentioned.

create trigger tr_triggername on tablename
for insert
as
begin
insert into otherdbname..tablename(fieldlist)
select fieldlist
from inserted
end
0
 
LVL 1

Author Comment

by:newoffice
ID: 21868551
is there a way to get a copy of the field names? it has around 100 fields
0
 
LVL 60

Accepted Solution

by:
chapmandew earned 500 total points
ID: 21868576
this should do it:

select column_name + ',' from information_schema.columns
where table_name = 'yourtablename'
for xml path('')
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Addition to SQL for dynamic fields 6 48
What is the proper way to use for criteria in left join? 7 32
SQL Query assistance 16 36
IIF in access query 19 26
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…
I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …

830 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