Solved

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

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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
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 video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

710 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