Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
Grifter
Constraint Violating Yak Guru
274 Posts |
Posted - 2011-04-26 : 06:07:32
|
Hi I have a script with a variable that I am assigning a value to so that it is usedd atthe end of the script. I have the declaration at the start of the script as follows, some other staements then the use of the variable value at the end and the get the error msg as shown:DECLARE @Variable CHAR(13)SELECT @Variable = '1234567890123'...other update statements....Update TableSET Column = 'Hello'WHERE column2 = @VariableServer: Msg 137, Level 15, State 2, Line 5Must declare the variable '@Variable'. I have used other scripts like this and have no problems. I have checked case and that all appears to match.Thanks for any helpG |
|
|
lionofdezert
Aged Yak Warrior
885 Posts |
Posted - 2011-04-26 : 06:33:00
|
are you using GO between some where declaration and update query.declare @testvariable intset @testvariable = 5GOselect @testvariable--------------------------http://connectsql.blogspot.com/ |
 |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2011-04-26 : 07:28:27
|
| Or are you executing the statements seperately.You have to execute the declare in the same batch as it is used.==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
Grifter
Constraint Violating Yak Guru
274 Posts |
Posted - 2011-04-26 : 08:17:11
|
| Hi I am using PRINT 'My comments'GOin some of the statements. I have to remove the GOs? |
 |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2011-04-26 : 08:19:56
|
| Yes - that terminates a batch.If you want to output the print data immediately you need to flush the buffer.Try adding a print space(8000) after the print.==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
Grifter
Constraint Violating Yak Guru
274 Posts |
Posted - 2011-04-26 : 08:20:40
|
| Have removed and seem to work. Why was I taught to put GO after the end of comments when A) it appears I don't need it and B) it causes problems like this?do I ever need to use the GO statement?G |
 |
|
|
lionofdezert
Aged Yak Warrior
885 Posts |
Posted - 2011-04-26 : 09:00:00
|
| GO command is a signal so to send the current batch (T-SQL statements between two GO commands) of T-SQL statements to Sql Server instance. You need not to place GO after any comments.--------------------------http://connectsql.blogspot.com/ |
 |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2011-04-26 : 09:28:47
|
quote: Originally posted by Grifter Have removed and seem to work. Why was I taught to put GO after the end of comments when A) it appears I don't need it and B) it causes problems like this?do I ever need to use the GO statement?G
Good question - you should ask whoever taught you.==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
|
|
|