Updating data in a remote table by using a linked server

Posted on 2014-08-19
Last Modified: 2014-08-19
I am trying to update a column in one database from a select statement on another database.
lets call database1 = weis1 and is on server1
and database2 = weis2 and is on server2

EXEC sp_addlinkedsrvlogin 'Accounts', 'true'

update server1.weis1 .[dbo].[CAT_RootOrg]
      set CAT_RootOrg.SKUCode=999999
      From server1.weis1.[dbo].[CAT_RootOrg] t1
            join server2.weis2 .[dbo].[ITEM_MASTER] t2
                  on t1.upc = t2.upc
         where server2.weis2 .[dbo].[ITEM_MASTER].upc=null

Sorry i am just a novice at best when comes to sql. Any help on this would be great.
Question by:jacobJL
    LVL 39

    Expert Comment

    Are you getting any error or what is the issue? The only problem I see assuming the user running it has all necessary rights in place is that it will be very slow on large volumes. Can you create a view on Server2 and filter that data at least right at the source?
    LVL 39

    Accepted Solution

    --assuming this "where server2.weis2 .[dbo].[ITEM_MASTER].upc=null"
    --will reduce a lot the server2 recordset
    use weis2
    create view v_items2join as
    select upc from [dbo].[ITEM_MASTER]
    where [dbo].[ITEM_MASTER].upc=null

    --on Server1:
    update server1.weis1.[dbo].[CAT_RootOrg]
          set CAT_RootOrg.SKUCode=999999
    from server1.weis1.[dbo].[CAT_RootOrg] t1
          inner join server2.weis2.[dbo].v_items2join t2 on t1.upc = t2.upc

    Author Closing Comment

    Thanks icohan. The problem is more of permissions thing. My DBA will have to run it. but your query above helps a lot. Thanks so much

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    This is basically a blog post I wrote recently. I've found that SARGability is poorly understood, and since many people don't read blogs, I figured I'd post it here as an article. SARGable is an adjective in SQL that means that an item can be fou…
    Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    761 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

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now