Solved

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

Posted on 2008-06-25
5
4,014 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Help with SQL Query 23 53
MySQL left join performance 4 30
SQL Query 34 82
grouping logic 6 50
In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

863 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now