Click here to Skip to main content
15,896,111 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, All

I have 3 local database with a table name as "TransactionDetails" with some unique ID for 3 diff machines.
and I have one server database on dekstop, having same table structure and name "TransactionDetails."

I want to copy the contents of all the tables from all machines to one database server table, after certain time of interval, as an when new rows are added to local machine.

How to write sql querry for this and windows service for this? I know windows services, but how to implement overall.
Posted

1 solution

Hi,

There are two things that can be done.

1. Update the main table as soon as any row is inserted in any of the tables in local databases.

This can be done using after insert trigger.

2. Update the main table periodically.

This can be done if you create a proc to insert data in the main table from the local table. Create this proc on every local database as your server table can be called from local database.

Schedule this proc on local machines to be executed at whatever interval you want your table to be updated (daily, weekly, monthly).

While inserting you should keep in mind not to insert the data that is already there in the table. For this use:

--Use the code below.
insert into <server name="">.<database name="">.<schema name="">.[Transaction Table]
select * from [Transaction Table] where not exists (select * from <server name="">.<database name="">.<schema name="">.[Transaction Table] )

This will insert only the rows not there in the main table.


Happy Coding !!
 
Share this answer
 
Comments
Pdeveloper 29-Sep-15 0:54am    
Thanks Saksham, will try and implement this. Thanks !!! :)

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