| Author |
Topic |
|
ureyes84
Starting Member
4 Posts |
Posted - 2006-05-29 : 19:02:25
|
| HI, please help needed !!!Can somebody tell me why the next lines:declare @MONTH_QTY varchar (300)set @vSQL = 'select @MONTH_QTY = '+ char(39)+ 'Value'+ char(39)print @vSQLExec (@vSQL) give the next error:select @MONTH_QTY = 'Value'Server: Msg 137, Level 15, State 1, Line 1Must declare the variable '@MONTH_QTY'.I need dynamic sql statements, that's why I'm using EXEC (@var) instead of a regular statement.THANKS !! |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-05-29 : 19:15:07
|
@MONTH_QTY is not declare within exec() scope. If you need to use dynamic SQL with variable, use sp_executesql insteaddeclare @MONTH_QTY varchar (300)set @vSQL = 'select @MONTH_QTY = '+ char(39)+ 'Value'+ char(39)print @vSQLExec (@vSQL)exec sp_executesql @vSQL, N'@MONTH_QTY varchar (300) OUTPUT', @MONTH_QTY OUTPUT KH |
 |
|
|
ureyes84
Starting Member
4 Posts |
Posted - 2006-05-29 : 19:25:04
|
| Hey thank you so much for your quick reponse, but when I ran:DECLARE @vSQL varchar (300)declare @MONTH_QTY varchar (300)set @vSQL = 'select @MONTH_QTY = '+ char(39)+ 'Value'+ char(39)print @vSQLexec sp_executesql @vSQL, N'@MONTH_QTY varchar (300) OUTPUT', @MONTH_QTY OUTPUTI Got:select @MONTH_QTY = 'Value'Server: Msg 214, Level 16, State 2, Procedure sp_executesql, Line 5Procedure expects parameter '@statement' of type 'ntext/nchar/nvarchar'.I'm sorry I'm new and kind of stressed I need helpTHANKS !!!!! |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-05-29 : 19:43:31
|
define the @vSQL as nvarchar(1000) KH |
 |
|
|
ureyes84
Starting Member
4 Posts |
Posted - 2006-05-29 : 19:47:00
|
| It's working, Khtan you rock !!!!I just would like to know why nvarchar instead of varchar?Why 1000?Thank you so much !!!!!!!! |
 |
|
|
ureyes84
Starting Member
4 Posts |
Posted - 2006-05-29 : 20:00:59
|
| Well I read the documentation, I think I got it.Thanks Khtan |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-05-29 : 20:37:46
|
"Why 1000?"No particular reason. Just use a value that is large enough for your dynamic sql statement. Anyway nvarchar max is 4000. KH |
 |
|
|
jen
Master Smack Fu Yak Hacker
4110 Posts |
Posted - 2006-05-30 : 01:45:56
|
why nvarcharquote: from bolThe string must be either a Unicode constant or a variable that can be implicitly converted to ntext.
--------------------keeping it simple... |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-05-30 : 06:15:48
|
| http://www.nigelrivett.net/SQLTsql/sp_executesql.htmlMadhivananFailing to plan is Planning to fail |
 |
|
|
|