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 8316487
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T21:22:30+00:00 2026-06-08T21:22:30+00:00

I’m writing a stored procedure to update multiple records based on a table variable

  • 0

I’m writing a stored procedure to update multiple records based on a table variable parameter.

The existing table is: Tb_Project_Image with relevant columns:

id PK (identity 1,1) 
cat_ord decimal(4,2)

The procedure will receive a temporary table variable (shown in the code below) containing the id as PI_ID, and the new value for cat_ord as newCatOrd. idx is a simple identity for each row containing 1…n where n is the rowcount of @tempTable.

For each row in @tempTable, I want to update Tb_Project_Image where id = PI_ID to the corresponding value.

    DECLARE @tempTable table (
    idx smallint Primary Key IDENTITY(1,1),
    PI_ID bigint, 
    newCatOrd decimal(4, 2) not null )

    INSERT INTO @tempTable values (3, 7.01)
    INSERT INTO @tempTable values (4, 7.02)
    INSERT INTO @tempTable values (5, 7.03)
    --etc...
    DECLARE @error int
    DECLARE @update int
    DECLARE @iter int
    SET @iter = 1

    BEGIN TRAN
WHILE @iter <= (select COUNT(*) from @tempTable)
    BEGIN
        UPDATE Tb_Project_Image
        SET cat_ord = (SELECT newCatOrd FROM @tempTable 
                        WHERE idx = @iter)
        WHERE id = (SELECT PI_ID FROM @tempTable
                        WHERE idx = @iter)  
        --error checking            
        set @error = @@ERROR
        set @update = @@ROWCOUNT
        IF ((@error = 0) AND (@update = 1))
            BEGIN
            SET @iter = @iter + 1
            CONTINUE
            END
        ELSE
            BREAK
    END

IF ((@error = 0) AND (@update = 1)) 
    COMMIT TRAN
ELSE
    ROLLBACK TRAN
GO

Now, the error checking is because, to ensure integrity, EACH row in the temporary table MUST make 1 update. (explanation omitted to save space) If a single iteration of the while loop threw an error, or didn’t effect exactly 1 row, I want to break the loop and rollback the transaction

THE PROBLEM I’m having is that this error checking is not working. I’m currently running it with 14 rows in @tempTable and the 11th uses a PI_ID not found in the Project_Image table. Therefore, @update = 0... but it continues the loop and commits the data.

I’d be doubly glad if someone had a method of doing this that only used a single update statement.

  • 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-06-08T21:22:32+00:00Added an answer on June 8, 2026 at 9:22 pm

    You cannot do it this way, because even SET resets the state of @@ERROR and @@ROWNUMBER variables. In this case @@ROWCOUNT is set to 1 after set @error = @@ERROR. If you do not assign the values to local variables, your code will work:

    IF ((@@error = 0) AND (@@rowcount = 1))
    

    But you might rather try try…catch error handling and test @@rowcount separately after update.

    UPDATE: doing it in single update:

    UPDATE t
       SET cat_ord = tt.newCatOrd
      FROM Tb_Project_Image t
     INNER JOIN @tempTable tt
        ON t.id = tt.PI_ID
    -- If there was PI_ID not found in Tb_Project_Image
    -- But I think that this should have been dealt with
    -- During the initial loading of temporary table
    IF @@ROWCOUNT <> (select count (*) from @tempTable)
    BEGIN
       -- Error reporting here
       ROLLBACK TRANSACTION
    END
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
Seemingly simple, but I cannot find anything relevant on the web. What is the
I am writing an app with both english and french support. The app requests
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

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.