Solved

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

Posted on 2008-06-25
5
4,019 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

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

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

623 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