Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6175907
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T00:00:20+00:00 2026-05-24T00:00:20+00:00

I have this sql update statement which updates a remote table. Is there any

  • 0

I have this sql update statement which updates a remote table. Is there any way I can optimize/speed up this code? It is running as part of a stored procedure.

DECLARE @WIP Table(Item varchar(25), WIP int)

--Get Work In Progress Numbers
INSERT INTO @WIP
select (UPC.ItemPrefix + '-' + UPC.ItemCode) As Item, SUM(PO.Quantity) As WIP
--into #WIP
from [NCLGS].[dbo].[AL_ItemUPCs] UPC
INNER JOIN al_PO PO ON PO.UPCID = UPC.UPCID
where PO.status in ('Assigned', 'New', 'UnAssigned', 'WaitingForFile')
Group by  (UPC.ItemPrefix + '-' + UPC.ItemCode)

--SLOW PART, takes over 17 minutes
UPDATE [Server].[Database].[dbo].[Item]  
SET     QtyOnHand = IH.QtyOnHand,
        QtyWorkInProgress = W.WIP   
FROM Avanti_InventoryHeader IH
INNER JOIN [Server].[Database].[dbo].[Item] I ON I.ItemNumber = IH.ItemNumber
LEFT JOIN @WIP W ON IH.ItemNumber = W.Item
WHERE isnumeric(left(IH.ItemNumber, 2)) = 0
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T00:00:22+00:00Added an answer on May 24, 2026 at 12:00 am

    I figured out the real problem with the query, it was updating thousands of records, even if those records didn’t change. So I queried for the changed records, saved that in a table variable, and only updated the changed records. Entire procedure (not just this part) went from 16min 44sec to 1min 26 sec.

    --BEGIN EXPERIMENTAL ITEM UPDATE SECTION
        DECLARE @WIP Table(Item varchar(25), WIP int)
    
        --Get Work In Progress Numbers
        INSERT INTO @WIP
        select (UPC.ItemPrefix + '-' + UPC.ItemCode) As Item, SUM(PO.Quantity) As WIP
        --into #WIP
        from [NCLGS].[dbo].[AL_ItemUPCs] UPC
        INNER JOIN al_PO PO ON PO.UPCID = UPC.UPCID
        where PO.status in ('Assigned', 'New', 'UnAssigned', 'WaitingForFile')
        Group by  (UPC.ItemPrefix + '-' + UPC.ItemCode)
    
        Declare @Remote Table(Item varchar(25), QtyOnHand int, WIP int)
    
        INSERT INTO @REMOTE
        Select ItemNumber, QtyOnHand, QtyWorkInProgress
        from [BMPR.COM].[NCL-CustomerPortal].[dbo].[Item]
    
        DECLARE @ItemsToUpdate Table (Item varchar(50))
    
        INSERT INTO @ItemsToUpdate
        Select R.Item
        From @Remote R
        Inner join Avanti_InventoryHeader IH ON R.Item = IH.ItemNumber
        LEFT JOIN @WIP W ON R.Item = W.Item
        Where R.QtyOnHand <> IH.QtyOnHand 
        OR R.WIP <> W.WIP
    
        --Select * from @ItemsToUpdate
    
        UPDATE [BMPR.COM].[NCL-CustomerPortal].[dbo].[Item]  
        SET     QtyOnHand = IH.QtyOnHand,
                QtyWorkInProgress = W.WIP   
        FROM Avanti_InventoryHeader IH
        INNER JOIN [BMPR.COM].[NCL-CustomerPortal].[dbo].[Item] I ON I.ItemNumber = IH.ItemNumber
        LEFT JOIN @WIP W ON IH.ItemNumber = W.Item
        WHERE I.ItemNumber in ( Select * from @ItemsToUpdate )
    --END EXPERIMENTAL ITEM UPDATE SECTION
    

    Any comments on this method?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this sql statement: CREATE TABLE [dbo].[User]( [UserId] [int] IDENTITY(1,1) NOT NULL, [FirstName]
I have a SQL statement in which I do this ... group by date
I have this SQL update query: UPDATE table1 SET table1.field1 = 1 WHERE table1.id
I have this sql: ALTER TABLE dbo.ChannelPlayerSkins DROP CONSTRAINT FK_ChannelPlayerSkins_Channels but apparently, on some
Right now I have this SQL query which is valid but always times out:
I have this query in sql server 2000: select pwdencrypt('AAAA') which outputs an encrypted
I need help re-factoring this legacy LINQ-SQL code which is generating around 100 update
(Note: this is for MS SQL Server) Say you have a table ABC with
I have the following Execution statement which creates a table (using data from another
Using a stored procedure i have a fairly complex SQL statement which returns a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.