Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server 2005 Table Variable Update Problem

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:

like image 713
DiningPhilanderer Avatar asked Nov 06 '08 16:11

DiningPhilanderer


People also ask

Can I update table variable in SQL Server?

It is true you can't update a table variable directly using its variable name - you have to use an alias. yes but you have created table t not @t.... I assume you didn't try my code.... If you had and had queried the table variable, you'd have seen it did work.

What is #temp table and @table variable in SQL Server?

Temporary Tables are physically created in the tempdb database. These tables act as the normal table and also can have constraints, index like normal tables. Table Variable acts like a variable and exists for a particular batch of query execution. It gets dropped once it comes out of batch.

Which is the correct way to DECLARE a table variable?

Syntax. If we want to declare a table variable, we have to start the DECLARE statement which is similar to local variables. The name of the local variable must start with at(@) sign. The TABLE keyword specifies that this variable is a table variable.


1 Answers

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'.

like image 167
Corbin March Avatar answered Oct 02 '22 07:10

Corbin March