Solved

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

Posted on 2008-06-25
5
4,011 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
this should do it:

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

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
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.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

763 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

12 Experts available now in Live!

Get 1:1 Help Now