Insert data from one Database into another Database

  • Hi All,

    I have a table which contains a master list of stock codes (stoItemMaster) which lives on Server05.

    I have another table which contains a list of stock codes (Products) which lives on Server03.

    My task is to get all Master stock codes which do not exist in Products to be added to the products tables.

    Here is my sql which works if the tables exists on the same server...

    ALTER PROCEDURE dbo.JanetsTest

    AS

    Insert into Products(ProductCode, ProductDescription)

    SELECT StoItemMaster.Code, StoItemMaster.Name

    FROM StoItemMaster

    where not exists(select Products.ProductCode, Products.ProductDescription from Products where Products.ProductCode = StoItemMaster.Code)

    GO

    Can someone help me with the syntax of getting the information to Server03

    Also if need to know how to change the 'where' so that it will only select stoItemMaster record where the productGroup = 'NPD1' or 'NPD2'

    Many thanks in advance 🙂

    Janet

  • You can setup a linked server (see BOL) and then you can do the query like this:

    ALTER PROCEDURE dbo.JanetsTest

    AS

    Insert into Products

    (

    ProductCode,

    ProductDescription

    )

    SELECT

    SIM.StoItemMaster.Code,

    SIM.StoItemMaster.Name

    FROM

    linkedservername.database.schema.StoItemMaster as SIM

    where

    not exists(select 1 from Products P

    where P.ProductCode = SIM.Code)

    GO

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply