drop temp table的问题

时间:2011-07-19 08:03:08

标签: sql-server-2005

我有一个没有11行的脚本

Declare c Cursor For Select Distinct InvestmentId From dbo.Investment
Open c
DECLARE @InvestmentIdTemp INT
Fetch next From c into @InvestmentIdTemp

While @@Fetch_Status=0 Begin
    Declare @tablevar table(col1 DATETIME)
    insert into @tablevar(col1) EXEC dbo.CalculateActualUpdateDate @InvestmentIdTemp
    UPDATE dbo.InvestmentData SET ActualUpdateDate =(SELECT TOP 1 col1 FROM @tablevar) WHERE InvestmentID=@InvestmentIdTemp

    DROP table @tablevar

   Fetch next From c into @InvestmentIdTemp
End

Close c
Deallocate c

我有错误:

Msg 102, Level 15, State 1, Line 11
Incorrect syntax near '@tablevar'.

如何删除此表?

1 个答案:

答案 0 :(得分:2)

正如我所说,表变量在数据库中不是真正的表,这意味着你不能“删除”它们。你可以在循环时清空你的表。你不需要处理变量。

Declare c Cursor For Select Distinct InvestmentId From dbo.Investment
Open c

DECLARE @InvestmentIdTemp INT
Declare @tablevar table(col1 DATETIME)

Fetch next From c into @InvestmentIdTemp

While @@Fetch_Status=0 Begin
    insert into @tablevar(col1) EXEC dbo.CalculateActualUpdateDate @InvestmentIdTemp
    UPDATE dbo.InvestmentData SET ActualUpdateDate =(SELECT TOP 1 col1 FROM @tablevar) WHERE InvestmentID=@InvestmentIdTemp

    delete @tablevar

    Fetch next From c into @InvestmentIdTemp
End

Close c
Deallocate c