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:
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.
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.
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.
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'.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With