AutomaticSlim
asked on
Having 2 Databases in Sync
I have 2 separate databases in SQL server, Database_A and Database_B.
In Databse_A I have a table called Transactions with the following fields:
OrderId_A
OrderDate_A
ShipDate_A
InvoiceDatePoNumber_A
In Databse_B I have a table called Orders with the following fields:
OrderId_B
OrderDate_B
ShipDate_B
InvoiceDatePoNumber_B
Database_A gets updated through a web interface and Database_B gets updated through an Access Front-end.
Now at the end of the day I want Both Database_A and Database_B to have the same exact info, meaning all the orders that have been placed through the web get updated to Database_B and vice versa. I wanted a query or a stored proc that I can schedule to do this once a day to Sync this to tables together.
Any suggestions???
In Databse_A I have a table called Transactions with the following fields:
OrderId_A
OrderDate_A
ShipDate_A
InvoiceDatePoNumber_A
In Databse_B I have a table called Orders with the following fields:
OrderId_B
OrderDate_B
ShipDate_B
InvoiceDatePoNumber_B
Database_A gets updated through a web interface and Database_B gets updated through an Access Front-end.
Now at the end of the day I want Both Database_A and Database_B to have the same exact info, meaning all the orders that have been placed through the web get updated to Database_B and vice versa. I wanted a query or a stored proc that I can schedule to do this once a day to Sync this to tables together.
Any suggestions???
or you may use triggers:
define this trigger on Databse_A
create trigger tr_I_TransactionsA
on Transactions
for insert,update
as
/*Check if operation was an update*/
if update(OrderDate_A)
update B
set B.OrderDate_B=A.OrderDate_ A
from Database_B..Transactions B
join inserted A
on B.Orderid_B=A.Orderid_A
else if update(ShipDate_A)
update B
set B.ShipDate_B=A.ShipDate_A
from Database_B..Transactions B
join inserted A
on B.Orderid_B=A.Orderid_A
else if update(InvoiceDatePoNumber _A)
update B
set B.InvoiceDatePoNumber_B=A. InvoiceDat ePoNumber_ A
from Database_B..Transactions B
join inserted A
on B.Orderid_B=A.Orderid_A
else/*Operation was an insert*/
insert into Database_B..Transactions
select * from inserted
go
Then define this trigger on Database_B
create trigger tr_I_TransactionsB
on Transactions
for insert,update
as
/*Check if operation was an update*/
if update(OrderDate_B)
update A
set A.OrderDate_A=B.OrderDate_ B
from Database_A..Transactions A
join inserted B
on A.Orderid_A=B.Orderid_B
else if update(ShipDate_B)
update A
set A.ShipDate_A=B.ShipDate_B
from Database_A..Transactions A
join inserted B
on A.Orderid_A=B.Orderid_B
else if update(InvoiceDatePoNumber _B)
update A
set A.InvoiceDatePoNumber_A=B. InvoiceDat ePoNumber_ B
from Database_A..Transactions A
join inserted B
on A.Orderid_A=B.Orderid_B
else/*Operation was an insert*/
insert into Database_A..Transactions
select * from inserted
go
using these triggers both tables from both databases are in sync in real time.
hope this helps.
xnmb
define this trigger on Databse_A
create trigger tr_I_TransactionsA
on Transactions
for insert,update
as
/*Check if operation was an update*/
if update(OrderDate_A)
update B
set B.OrderDate_B=A.OrderDate_
from Database_B..Transactions B
join inserted A
on B.Orderid_B=A.Orderid_A
else if update(ShipDate_A)
update B
set B.ShipDate_B=A.ShipDate_A
from Database_B..Transactions B
join inserted A
on B.Orderid_B=A.Orderid_A
else if update(InvoiceDatePoNumber
update B
set B.InvoiceDatePoNumber_B=A.
from Database_B..Transactions B
join inserted A
on B.Orderid_B=A.Orderid_A
else/*Operation was an insert*/
insert into Database_B..Transactions
select * from inserted
go
Then define this trigger on Database_B
create trigger tr_I_TransactionsB
on Transactions
for insert,update
as
/*Check if operation was an update*/
if update(OrderDate_B)
update A
set A.OrderDate_A=B.OrderDate_
from Database_A..Transactions A
join inserted B
on A.Orderid_A=B.Orderid_B
else if update(ShipDate_B)
update A
set A.ShipDate_A=B.ShipDate_B
from Database_A..Transactions A
join inserted B
on A.Orderid_A=B.Orderid_B
else if update(InvoiceDatePoNumber
update A
set A.InvoiceDatePoNumber_A=B.
from Database_A..Transactions A
join inserted B
on A.Orderid_A=B.Orderid_B
else/*Operation was an insert*/
insert into Database_A..Transactions
select * from inserted
go
using these triggers both tables from both databases are in sync in real time.
hope this helps.
xnmb
BTW, if you're only synchronizing updates, then remove the for insert definition and leave only the (for) update then
ommit the insert operations.
xnmb
ommit the insert operations.
xnmb
Or better yet, why not have a single database for both front-ends?
xnmb
xnmb
ASKER
xnmb-
I have experience with SQL server but I have never don trigers. Can you guide me a little beat on that. Do I write it in a Stored Proc or is it a different thing??
Thanks
I have experience with SQL server but I have never don trigers. Can you guide me a little beat on that. Do I write it in a Stored Proc or is it a different thing??
Thanks
ASKER
xnmb -
To answer your last comment, we have to use two databases, since one of them is created by another company and we can not do any changes on it. When they have updates on the database everything gets over writen.
To answer your last comment, we have to use two databases, since one of them is created by another company and we can not do any changes on it. When they have updates on the database everything gets over writen.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
xnmb -
Ok....
So triggers are not part of Stored Procs.....
So when you say "run this in database A" where do you exactly mean? Should I run it in Query Analyzer??
Thanks again
Ok....
So triggers are not part of Stored Procs.....
So when you say "run this in database A" where do you exactly mean? Should I run it in Query Analyzer??
Thanks again
ASKER
I figured out where to do the triggers.
I Opend the database clicked on my table and then manage triggers and I pasted your code there. Then I Inserted a value in one of the tables and nothing happend....
so how do I execute the triggers???
I Opend the database clicked on my table and then manage triggers and I pasted your code there. Then I Inserted a value in one of the tables and nothing happend....
so how do I execute the triggers???
you dont invoke triggers manually. triggers "fire" when certain operations are performed against the table on which you defined your trigger(s).
create trigger tr_I_TransactionsA
on Transactions <--This is the table on which the trigger is defined(Transactions table)
for update <--This is the operation, when performed against the table, will fire the trigger
as
...
xnmb
create trigger tr_I_TransactionsA
on Transactions <--This is the table on which the trigger is defined(Transactions table)
for update <--This is the operation, when performed against the table, will fire the trigger
as
...
xnmb
ASKER
Just one last question.....
How do I get this to work with INSERTS and UPDATES together instead of UPDATE only?
Also would this work if I have my key field in my table as Auto Increment Identity?
Thanks
How do I get this to work with INSERTS and UPDATES together instead of UPDATE only?
Also would this work if I have my key field in my table as Auto Increment Identity?
Thanks
AutomaticSlim:
This old question needs to be finalized -- accept an answer, split points, or get a refund. For information on your options, please click here-> http:/help/closing.jsp#1
EXPERTS:
Post your closing recommendations! No comment means you don't care.
This old question needs to be finalized -- accept an answer, split points, or get a refund. For information on your options, please click here-> http:/help/closing.jsp#1
EXPERTS:
Post your closing recommendations! No comment means you don't care.
OrderId
OrderDate
ShipDate
InvoiceDatePoNumber
Source
Copied
Where the field SOURCE is either "A" or "B" (or whatever combination you want), indicating where the rows come from, and the field "copied" indicates if the row has been syncronized to the other database.
Now, when Database_A is syncronized with Database_B, all records with Source="A" and copied=FALSE need to be transferred to Database_B, and as second step, all these records need also to be updated so the value Copied get's set to TRUE
You can also try to rely on replication (which is build-in in SQL Server), and schedule the replication to occur only once.
CHeers