SQL DECLARE not retaining scope when running multiple statement
Hi, I have an issue when running multiple SQL statements through IDEA:
DECLARE @x int
SELECT * FROM MyTable WHERE id = @x
go
DECLARE @y varchar(max)
SELECT * FROM MyTable WHERE name = @y
go
...in this situation, the console spits out an error - @x is not declared. But if i run the scripts (between the go's) one by one it works fine.
How can I get this to work for the whole script?
~gilbert
请先登录再写评论。
I currently do not know the other way but runing both statements together using JDBC.
Declared variables are accessible within the same batch block.
See my test output:
My question is more IntelliJ (and maybe JDBC) than SQL - I want to run the entire script (in my example) in one batch (ctrl-shift-f10). Problem is, when i have multiple statemt's with "go" the variable decalrations seem to get separated from the usages (and I do ensure the declarations are in the same block as their usages) - this happens only in IntelliJ, and works fine in SSMS.
~gilbert
Now I see. This is related to "Run Script" functionality that doesn't handle T-SQL correctly. Please file a ticket at http://youtrack.jetbrains.com .
Logged - thanks: http://youtrack.jetbrains.com/issue/IDEA-107916