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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Help with Script

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 Table
SET Column = 'Hello'
WHERE column2 = @Variable

Server: Msg 137, Level 15, State 2, Line 5
Must 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 help

G

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 int
set @testvariable = 5

GO

select @testvariable

--------------------------
http://connectsql.blogspot.com/
Go to Top of Page

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.
Go to Top of Page

Grifter
Constraint Violating Yak Guru

274 Posts

Posted - 2011-04-26 : 08:17:11
Hi

I am using

PRINT 'My comments'
GO

in some of the statements. I have to remove the GOs?
Go to Top of Page

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.
Go to Top of Page

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
Go to Top of Page

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/
Go to Top of Page

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.
Go to Top of Page
   

- Advertisement -