Question

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.

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top