sql table record copy

Posted on 2011-02-25
Last Modified: 2012-05-11
MS SQL Server 2008

How do you script via a Stored Procedure the following:

DB 1 - Table 1A (server A)
DB 2 - Table 2A (server B)

if   DB 1 - Table 1A (column1) - unique value (uniqueidentity) doe not exist in
DB 2 - Table 2A (column1) - unique value (uniqueidentity) then create it in  DB 2 - Table 2A

then copy certain columns from DB 1 - Table 1A to DB 2 - Table 2A (insert a new entry).

(there could for example none, 1 or 1000's of new table entries to create/insert each time stored procedure is called)
Question by:amillyard
  • 3
  • 2
LVL 26

Accepted Solution

tigin44 earned 500 total points
ID: 34982914

first create a linked server to serverA from ServerB lest name the linked server as "lsserverA"
then run the following query... This will populate B with the values exists in A but not in B

FROM lsserverA.DB1..Table1A  A
      LEFT JOIN DB2..Table2A B ON A.column1 = B.column1

you can put this code in a procedure and call it again and again..

Author Comment

ID: 34983059
tigin44: thanks for the feedback.  how do you llink servers?

 (1 db is on a dedicated and other on a public shared cloud environment for example).
LVL 26

Expert Comment

ID: 34983083
you can create a linked server like the one below

EXEC sp_addlinkedserver  

Author Comment

ID: 34983129
tigin44:  does it matter which db server to run that script?   is this link preserved upon a physical reboot of server?
LVL 26

Expert Comment

ID: 34983150
I assume that you will run this code on ServerB ... you can run it vice versa but you should change the linked server and references to the new definitions to run it on ServerA...

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
hyperlink data type in SQL 3 26
SSIS with VPN COnnection 2 70
T-SQL: Do I need CLUSTERED here? 13 41
Query Help - MSSQL - Averages 5 25
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question