SQL Server: table variable used in a inner join SQL Server: table variable used in a inner join sql sql

SQL Server: table variable used in a inner join


Change your last statement to:

UPDATE t1, tempSET t1.SportName = temp._SportNameFROM tblSport AS t1INNER JOIN @t AS temp    ON t1.Lang = temp._Lang

(need to check exact syntax)


Apart from the t1 alias being in the wrong place, nobody else mentioned using square brackets around the table variable, instead of an alias. Changing the update statement to the following will work too:

UPDATE t1SET    t1.SportName = [@t]._SportNameFROM    @t INNER JOIN tblSport t1 ON t1.Lang = [@t]._Lang

[Tested on SQL Server 2005.]


Justin's answer is correct syntactically - you need to assign an alias to the temp table (same for table type variables in 2008).

However, be aware that neither table variables nor table-type variables have any statistics associated with them, and therefore can lead the query optimiser to make very dubious choices with regard to execution plans (because it will always estimate that the table variable contains 1 row - and therefore usually chooses nested loops as a join operator).