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

  • Home
  • SEARCH
  • 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 66923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T19:10:46+00:00 2026-05-10T19:10:46+00:00

I have been reading about the differences between Table Variables and Temp Tables and

  • 0

I have been reading about the differences between Table Variables and Temp Tables and stumbled upon the following issue with the Table Variable. I did not see this issue mentioned in the articles I pursued.

I pass in a series of PKs via a XML data type and successfully create the records in both temp table structures. When I attempt to update further fields in the temp tables the Table Variable fails but the Temp Table has no problem with the Update Statement. What do need to do different? I would like to take advantage of the speed boost that Table Variables promise…

Here are the SP snippets and Results:

CREATE PROCEDURE ExpenseReport_AssignApprover (     @ExpenseReportIDs       XML ) AS   DECLARE     @ERTableVariable        TABLE   (   ExpenseReportID             INT,                                                 ExpenseReportProjectID      INT,                                                 ApproverID                  INT)   CREATE TABLE #ERTempTable (     ExpenseReportID             INT,     ExpenseReportProjectID      INT,     ApproverID                  INT )  INSERT INTO @ERTableVariable (ExpenseReportID) SELECT ParamValues.ID.value('.','VARCHAR(20)') FROM @ExpenseReportIDs.nodes('/Root/ExpenseReportID') as ParamValues(ID)  INSERT INTO #ERTempTable (ExpenseReportID) SELECT ParamValues.ID.value('.','VARCHAR(20)') FROM @ExpenseReportIDs.nodes('/Root/ExpenseReportID') as ParamValues(ID)  UPDATE          #ERTempTable SET             ExpenseReportProjectID = (  SELECT TOP 1 ExpenseReportProjectID                                                  FROM ExpenseReportItem                                                  WHERE(ExpenseReportID = #ERTempTable.ExpenseReportID))  UPDATE          @ERTableVariable SET             ExpenseReportProjectID = (  SELECT TOP 1 ExpenseReportProjectID                                                  FROM ExpenseReportItem                                                  WHERE(ExpenseReportID = @ERTableVariable.ExpenseReportID)) 

Error when last update statement in there : Must declare the scalar variable ‘@ERTableVariable’.

ExpenseReportProjectID is updated in #ERTempTable when the last update is commented out:

  • 1 1 Answer
  • 1 View
  • 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. 2026-05-10T19:10:46+00:00Added an answer on May 10, 2026 at 7:10 pm

    A quick test works when I literalize the table var reference in the last update:

    UPDATE @ERTableVariable     SET ExpenseReportProjectID = (               SELECT TOP 1 ExpenseReportProjectID         FROM ExpenseReportItem          WHERE ExpenseReportID = [@ERTableVariable].ExpenseReportID     ) 

    You could also use an ‘update from’:

    UPDATE er SET      ExpenseReportProjectID = ExpenseReportItem.ExpenseReportProjectID FROM @ERTableVariable er INNER JOIN ExpenseReportItem ON      ExpenseReportItem.ExpenseReportID = er.ExpenseReportID 

    The join might return multiple rows but only one will ‘stick’. Kind of a non-deterministic update like ‘TOP 1’.

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

Sidebar

Ask A Question

Stats

  • Questions 206k
  • Answers 206k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Well you could BEGIN a transaction, do the two queries… May 12, 2026 at 9:07 pm
  • Editorial Team
    Editorial Team added an answer If I'm understanding your question correctly you would need to… May 12, 2026 at 9:07 pm
  • Editorial Team
    Editorial Team added an answer The issue is that you are releasing img when you… May 12, 2026 at 9:07 pm

Related Questions

I think I'm just cloudy on how debugging works on a real device -
I've been using javascript for a while, but have never learned the language past
I am currently reading Code Complete where McConnell strongly encourages making all variables private.
I have created a dynamic typing system in C in order to create a

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.