Must declare the table variable

落花浮王杯 提交于 2021-01-28 08:27:01

问题


I come across an error while writing some SQL statements for data imports.

As I am doing data port, I need to declare some temporary table variables.

I declared a table variable at the beginning of the file and performed some manipulations (while loops, insertions and updates) on the table variable. Later in the middle of the scripts in another while loop if I access this table variable the script parsing giving below error

Must declare the table variable @temptable

Appreciate your help.


回答1:


If a previously declared Variable is no longer available in a block of executed SQL Code, it is likely that a GO statement has been called.

As per MSDN, "The scope of a local variable is the batch in which it is declared.".

A Go Statement "Signals the end of a batch of Transact-SQL statements to the SQL Server utilities."

It is recommended to double check your SQL code for any errant GO statements.

References:

DECLARE @local_variable (Transact-SQL) http://msdn.microsoft.com/en-us/library/ms188927.aspx

GO (Transact-SQL) http://msdn.microsoft.com/en-us/library/ms188037.aspx



来源:https://stackoverflow.com/questions/10539470/must-declare-the-table-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!