SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 

How to insert and update from remote linked server

By Chris Morton, 2007/10/30

Total article views: 2298 | Views in the last 30 days: 20

UPDATED : 2008-10-22 BY CHRIS MORTON

 

-------------------


Unfortunately at the time I published this article the data was truncated when I submitted it. Unfortunately that stuff is long gone.

Basically it worked like this:

I had a table that mapped the remote sql server (linked server) to the district id that it was associated with.

Then I had a bunch of stored procedures, one for each table that needed an import or update, in all of these stored procedures I had a districtid input parameter, which was resolved to the linked server name it belonged to by a user defined function.

From there the dynamic sql was constructed for example:

If district id was 1 then

Insert into localtable (columnlist plus the districtid) select (exact same column list plus the districtid) from [(linkedserverremotetable(fully qualified name))] where [(remotecriteria)] not in (sub query localtable).

The update works similarly.

Now I had something like 200 stored procedures doing this.

I wrapped them all up in a stored procedure that used a cursor to go through all the districtid's in the linkedsever table and execute them in order.

When I tested it on a local environment it worked very well and it to approximately 2 minutes to import about 2 million records.

This was my first piece of 'advanced' sql.

If I had to do it again I would change some things slightly, and probably use other technology.

However this logic did work, and if the technology is not available (i.e. you don't have enterprise licenses or have older technology)

It is a real shame that my article was truncated. It was eleven word pages long, originally. I have lost it long ago.

Thanks

Chris

 

-----------------------------

 

 

The Problem:


i need to join 19 databases on remote linked servers that do not support replication. the net effect must be the same as replication but without GUID's. i have to maintain all relationships and data integrity. the 19 databases are in different regions of the country, each on a different server and therefore can be identified by districtid and alias (the server name). the connections are not necessarily always connected and failures need to be handled. i have read only access to the databases. Only data that has changed must be updated. Only new data must be inserted. Errors must be traceable and failures must be able to be audited.

The Solution:

Set up your linked servers.

Create a table in the 'subscription' database (in this case [DIMSCONSOLIDATEDData]) that maps the district id's against an alais and an installation.
Use this script for example:

USE [DIMSCONSOLIDATEDData]
GO
/****** Object: Table [dbo].[General_District] Script Date: 08/07/2007 08:38:22 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[General_District](
[DistrictID] [int] IDENTITY(1,1) NOT FOR REPLICATION NOT NULL,
[District] [varchar](255) NOT NULL,
[ProvinceID] [int] NOT NULL,
[Enabled] [bit] NOT NULL,
[Alias] [varchar](90) NULL,
CONSTRAINT [PK_District] PRIMARY KEY CLUSTERED
(
[DistrictID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF
GO
ALTER TABLE [dbo].[General_District] WITH CHECK ADD CONSTRAINT [FK_General_District_General_Province] FOREIGN KEY([ProvinceID])
REFERENCES [dbo].[General_Province] ([ProvinceID])
GO
ALTER TABLE [dbo].[General_District] CHECK CONSTRAINT [FK_General_District_General_Province]

Create a districtid column in the 'replication tables' and make it a composite key.
for example:

USE [DIMSCONSOLIDATEDData]
G

By Chris Morton, 2007/10/30

Total article views: 2298 | Views in the last 30 days: 20
Your response
 
 
Related Articles
FORUM

dynamic database generation

dynamic database generation

FORUM

how get generate script for database user with their database and server roles

how get generate script for database user with their database and server roles

FORUM

How to generate email from database( MS SQL SERVER 2000 or 2005)

How to generate email from database( MS SQL SERVER 2000 or 2005)

FORUM

How can i generate backups sqlserver7.0 database

How can i generate backups sqlserver7.0 database

FORUM

Generating SQL Logins\users and permissions for a database

Generating SQL Logins\users and permissions for a database

 
Contribute

Free registration required...

To read the rest of this article, and access thousands of other articles, we ask you to register on the site and subscribe to our newsletters.

Login (existing users)

Login

Email:   Password:   Remember me: Forgotten your password?

Register (new users)

Register

Email:   Password:
Confirm:

Subscribing to our newsletters gets you:

  • ALL of our content (thousands of articles, scripts, and forum postings)
  • A daily newsletter (example)
  • A weekly news round up (example)
  • The opportunity to ask and answer questions in our forums
  • A daily Question of the Day to test and help you increase your knowledge of SQL Server.

Steve Jones
Editor, SQLServerCentral.com