Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Variable in Dynamic T-SQL Query Out of Scope

Status
Not open for further replies.

YaBabyYa

Programmer
Jul 26, 2007
21
CA
Hello,

I am constructing a dynamic T-SQL query.





code snippet
----------------------------------------------------------

set @value_string = ' Select @temp_val = isNull( ' + @curr_char + ' , 0) from ##' +@input_table_name


EXEC(@value_string)

------------------------------------------------------------



Output T-SQL Query Which is Called by EXEC
*********************************************************

Select @temp_val = isNull( t , 0) from ##Test_word28July20075231713524



PROBLEM
*********************************************************
It is throwing an error at EXEC(@value_string)

Server: Msg 137, Level 15, State 1, Line 1
Must declare the variable '@temp_val'.


Debugging Notes
*******************************************************
1) I have ensured that @temp_val is declared as a float but it's not finding the variable I have declared in the body of the stored procedure.

2) Is the variable out of scope?

3) Can I declare named global variables .. like the global
temporary tables ## ?



 
Did you see my answer in the other thread you started? Would you answer my comments there, please, instead of starting a new thread?

Using global temporary tables is NOT the way to solve the problem, other advice given you fully considered.

[COLOR=black #d0d0d0]When I walk, I sometimes bump into things. I am closing my eyes so that the room will be empty.[/color]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top