Link to home
Create AccountLog in
Avatar of Ricky11
Ricky11

asked on

How to Sync Microsoft SQL Databas


 Scenario:
I have of off-site SQL 2005 EXPRESS Database Located in Remote Site, We also have a client .NET application that sits on the same server.  We choose to put the database and the one of the client apps on the same server as the speed of connectivity from the .net application to the database is very quick since its a local connection.

We have several Client Apps in different countries that connects to the remote SQL Database using a connection string as such :
<ConnectionStr>Provider=SQLOLEDB;Data Source=xxx.xxx.xxx.xxx,1433\SQLEXPRESS;uid=xxx;pwd=password;Initial Catalog=ParcelManagement;</ConnectionStr>

The problem is the Remote location where the database is stored has a slow internet connection to other countries, and it take a while for other client app to connect and download data. What’s more the remote site powers off after office hours.  So our client apps cannot connect after certain times.

We  do not want to change the location of the remote sql database because that office uses it the most and needs a local connection to the database.

What are my options? Would it be possible to sync the database, so overseas users can connect to a local database that is sync with the remote one? Any changes made on one would reflect in another.  (without doing any .net coding) I would like sql management studio to do all the syncing?

IF SYNCing is not possible, then creating daily backups (changes only, and not entire database) could also be a solution.. Our users could connect to the remotely backup database. Our users would have to know that this is a backup only and not to make any changes. Most of the time our users need to review but not edit.

Any ideas?

THANKS.
Avatar of NajamG
NajamG
Flag of Pakistan image

ASKER CERTIFIED SOLUTION
Avatar of Andrei Fomitchev
Andrei Fomitchev
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
You can also use MERGE statement (see Code).

And for 2 instances of SQL Server you can use Linked Server and OPENQUERY (or 4-part queries - if it works faster).


USE tempdb
GO

IF(Exists(SELECT 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'MergeSourceTest'))
DROP TABLE MergeSourceTest

CREATE TABLE MergeSourceTest(ParcelID int, ParcelAddress varchar(100),
     CONSTRAINT MergeSourceTest_PK PRIMARY KEY(ParcelID));

IF(Exists(SELECT 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'MergeTargetTest'))
DROP TABLE MergeTargetTest

CREATE TABLE MergeTargetTest(ParcelID int, ParcelAddress varchar(100),
     CONSTRAINT MergeTargetTest_PK PRIMARY KEY(ParcelID));
GO

INSERT MergeTargetTest (ParcelID, ParcelAddress) VALUES
(1, 'Address1'),
(2, 'Address2'),
(3, 'Address3')

INSERT MergeSourceTest(ParcelID, ParcelAddress) VALUES
(1, 'Address1'),
(2, 'Address2-new'),
(4, 'Address4')
GO

SELECT * FROM MergeSourceTest
SELECT * FROM MergeTargetTest
GO

BEGIN TRAN;

----------------------------- MERGE QUERY
MERGE MergeTargetTest AS t
USING MergeSourceTest AS s
ON (t.ParcelID = s.ParcelID)
WHEN NOT MATCHED BY TARGET
    THEN INSERT(ParcelID, ParcelAddress) VALUES(s.ParcelID, s.ParcelAddress)
WHEN MATCHED
    THEN UPDATE SET t.ParcelAddress = s.ParcelAddress
WHEN NOT MATCHED BY SOURCE
    THEN DELETE
OUTPUT $action AS [Action], inserted.*, deleted.*;
---------------------------
COMMIT TRAN;
GO

SELECT * FROM MergeSourceTest
SELECT * FROM MergeTargetTest
GO

------- Returns before MERGE ----------

MergeTargetTestSource:
ParcelID	ParcelAddress
1	Address1
2	Address2-new
4	Address4

MergeTargetTest:
1	Address1
2	Address2
3	Address3

------- Returns from MERGE ----------

Action	ParcelID	ParcelAddress	ParcelID	ParcelAddress
UPDATE	1	Address1	1	Address1
UPDATE	2	Address2-new	2	Address2
DELETE	NULL	NULL	3	Address3
INSERT	4	Address4	NULL	NULL

------- Returns after MERGE ----------

MergeSourceTest:
ParcelID	ParcelAddress
1	Address1
2	Address2-new
4	Address4

MergeTargetTest:
1	Address1
2	Address2-new
4	Address4

Open in new window

Avatar of Ricky11
Ricky11

ASKER

Thanks, I mentioned that we didn't want to change of our our coding in our application, or set any trigger events.  All this must be done by DB replication,backups etc, using Management Studio Express