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.
Microsoft Server AppsMicrosoft SQL ServerMicrosoft SQL Server 2005

Avatar of undefined
Last Comment
Ricky11

8/22/2022 - Mon
NajamG

ASKER CERTIFIED SOLUTION
Andrei Fomitchev

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Andrei Fomitchev

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

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
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck