Solved

sql table record copy

Posted on 2011-02-25
5
289 Views
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)
0
Comment
Question by:amillyard
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 26

Accepted Solution

by:
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

INSERT INTO DB2..Table2A
SELECT *
FROM lsserverA.DB1..Table1A  A
      LEFT JOIN DB2..Table2A B ON A.column1 = B.column1
WHERE B.column1 IS NULL

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

Author Comment

by:amillyard
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).
0
 
LVL 26

Expert Comment

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

EXEC sp_addlinkedserver  
   @server='lsserverA',
   @srvproduct='',
   @provider='SQLNCLI',
   @datasrc='Server1.abc.com\DB1'
0
 

Author Comment

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

Expert Comment

by:tigin44
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...
0

Featured Post

Backup Solution for AWS

Read about how CloudBerry Backup fully integrates your backups with Amazon S3 and Amazon Glacier to provide military-grade encryption and dramatically cut storage costs on any platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MS SQL Server connect issues 4 37
Find unused columns in a table 12 67
Upgrading to SQL Server 2015 Express 2 29
Need to trim my database size 9 16
Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

740 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