Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi..Friends,
I have two database with same definition. If any change happens in first database tables's Records, I also want to update second database table's Records. I want to write a query for this. Please help me..
Posted
Comments
Surendra Adhikari SA 22-May-13 2:05am    
please clarify your question . which database server do you using ??
Maciej Los 22-May-13 2:06am    
WHat have you done till now?
Where are you stuck?

Hi,

You can do this using Trigger on the particular table..


ex:

SQL
CREATE TRIGGER trg_YourTrigger ON dbo.AA
FOR UPDATE
AS
UPDATE  B.dbo.BB
SET ColumnY = I.ColumnX
FROM    inserted I
INNER JOIN
    deleted D
ON  I.PrimaryKeyInTableAA = D.PrimaryKeyInTableAA
WHERE   B.dbo.BB.ColumnY = D.ColumnX

SQL
<pre lang="SQL">
 
Share this answer
 
The simplest way is to use (for the first time):
SQL
INSERT INTO [destination database].[dbo].[destination table] ([destination field1], [destination field12], [destination fieldN])
SELECT ([source field1], [source field12], [source fieldN])
FROM [source database].[dbo].[source table]

Next time, you'll need to use UPDATE[^] statement.

More: INSERT Examples (Transact-SQL)[^]
SELECT statement (T-SQL)[^]
 
Share this answer
 
you can use trigger...
to update second database record
SQL
Update Databasename.Schemaname.tablename set ...

e.g.
SQL
Update database2.dbo.table1 set...

Happy Coding!
:)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900