where子句中的临时表

时间:2014-08-14 13:47:04

标签: sql sql-server

我需要来自临时表的一些记录,为此我尝试运行以下查询:

DECLARE @temp_table TABLE (Id uniqueidentifier, Dates nvarchar(10))

INSERT @temp_table
SELECT ID,Right(Companies.UserDefined4, 10)
FROM   Companies

Select *
From  Companies,@temp_table
Where Companies.ID = @temp_table.Id

但是在Where子句中我收到了这个错误:

  

必须声明标量变量@temp_table。

2 个答案:

答案 0 :(得分:1)

尝试

SELECT *
FROM Companies c
INNER JOIN @temp_table t ON c.ID = t.ID

答案 1 :(得分:1)

正确的代码应该是:

DECLARE @temp_table TABLE (Id uniqueidentifier, Dates nvarchar(10))

INSERT @temp_table
SELECT ID,Right(Companies.UserDefined4, 10)
FROM   Companies

Select *
From  Companies c
join @temp_table t
  on c.ID = t.Id