Hi,
I am having problems setting the value of a variable in a SQL String
that I have to create dynamically in my procedure. The code that I
currently have is as follows:
set @sqlStatement='Set @compare_string=' + '(Select ' +
@group_column_list_mod + ' from ' + @Tbl_Name + '_Sorted' + ' where
Identity_Column=' + ltrim(rtrim(str(@loop_counter))) + ')'
exec(@sqlStatement)
The error message that I get is as follows:
Must declare the variable '@compare_string'.
Here @compare_string has already been declared in the procedure and I
don't have a problem using the variable anywhere else but this SQL
Statement (when called using the EXEC function).
I am not sure why SQL Server can't see the variable declared when used
in a string in conjunction with EXEC. Is this a syntax issue? Any help
on this issue would be greatly appreciated!
Thanks in advance. 5 13355
You need a parms string and an exec string, like this:
SET @Parms = `@compare_string`
set @sqlStatement='Set @compare_string=' + '(Select ' +
@group_column_list_mod + ' from ' + @Tbl_Name + '_Sorted' + ' where
Identity_Column=' + ltrim(rtrim(str(@loop_counter))) + ')'
EXECUTE sp_executesql @sqlStatement, @Parms, @compare_string
SET @Error = COALESCE ( NULLIF ( @Error, 0 ), @@ERROR ) exec(@sqlStatement)
The error message that I get is as follows:
Must declare the variable '@compare_string'.
Here @compare_string has already been declared in the procedure and I don't have a problem using the variable anywhere else but this SQL Statement (when called using the EXEC function).
I am not sure why SQL Server can't see the variable declared when used in a string in conjunction with EXEC. Is this a syntax issue? Any help on this issue would be greatly appreciated!
Thanks in advance.
Thanks for your reply. The sp_executesql procedure still doesn't give
the desired results. I am posting the updated piece of code and sample
output from the Query Analyzer.
------------------
set @parameter_String=N'@compare_string nvarchar(4000)'
set @sqlStatement='Set @compare_string=(Select ' +
@group_column_list_mod + ' from ' + @Tbl_Name + '_Sorted' + ' where
Identity_Column=' + ltrim(rtrim(str(@loop_counter))) + ')'
Print @sqlStatement
EXECUTE sp_executesql @sqlStatement,@parameter_String,@compare_string
Print @compare_String
------------------
When I print the value of @compare_String in the end its a NULL.
However, if I run the same query without the set @compare_string
clause, it does work perfectly and returns the values of two columns
concatenated together. Any clues as to where I might be going wrong?
Thanks,
"Robin Tucker" <id*************************@reallyidont.com> wrote in message news:<bs*******************@news.demon.co.uk>... You need a parms string and an exec string, like this:
SET @Parms = `@compare_string`
set @sqlStatement='Set @compare_string=' + '(Select ' + @group_column_list_mod + ' from ' + @Tbl_Name + '_Sorted' + ' where Identity_Column=' + ltrim(rtrim(str(@loop_counter))) + ')'
EXECUTE sp_executesql @sqlStatement, @Parms, @compare_string
SET @Error = COALESCE ( NULLIF ( @Error, 0 ), @@ERROR )
exec(@sqlStatement)
The error message that I get is as follows:
Must declare the variable '@compare_string'.
Here @compare_string has already been declared in the procedure and I don't have a problem using the variable anywhere else but this SQL Statement (when called using the EXEC function).
I am not sure why SQL Server can't see the variable declared when used in a string in conjunction with EXEC. Is this a syntax issue? Any help on this issue would be greatly appreciated!
Thanks in advance.
[posted and mailed, please reply in news]
Aamer Nazir (aa***********@hotmail.com) writes: I am having problems setting the value of a variable in a SQL String that I have to create dynamically in my procedure. The code that I currently have is as follows:
set @sqlStatement='Set @compare_string=' + '(Select ' + @group_column_list_mod + ' from ' + @Tbl_Name + '_Sorted' + ' where Identity_Column=' + ltrim(rtrim(str(@loop_counter))) + ')'
exec(@sqlStatement)
The error message that I get is as follows:
Must declare the variable '@compare_string'.
Here @compare_string has already been declared in the procedure and I don't have a problem using the variable anywhere else but this SQL Statement (when called using the EXEC function).
The EXEC() statement is another scope which is not part of your procedure.
Thus, @compare_string is not defined in that example.
For better examples than the one posted, see http://support.microsoft.com/?id=262499 and http://www.sommarskog.se/dynamic_sql.html#sp_executesql.
--
Erland Sommarskog, SQL Server MVP, so****@algonet.se
Books Online for SQL Server SP3 at http://www.microsoft.com/sql/techinf...2000/books.asp
Yes, that won't work. Sorry, I just focused on the parameter part.
You can just do this:
select @compare_string = mytable.myfield FROM mytable where Identity_Column
= myvalue
or, in your specific case:
'Select @compare_string=' + @group_column_list_mod + ' from ' + @Tbl_Name +
'_Sorted' + ' where Identity_Column=' + ltrim(rtrim(str @loop_counter))'
At least this is the syntax you should use in this case. Otherwise, you are
effectively trying to bind @compare_string to a recordset result, which
doesn't work.
Make sure you add in the error checking afterwards!! :)
"Aamer Nazir" <aa***********@hotmail.com> wrote in message
news:60**************************@posting.google.c om... Thanks for your reply. The sp_executesql procedure still doesn't give the desired results. I am posting the updated piece of code and sample output from the Query Analyzer.
------------------ set @parameter_String=N'@compare_string nvarchar(4000)'
set @sqlStatement='Set @compare_string=(Select ' + @group_column_list_mod + ' from ' + @Tbl_Name + '_Sorted' + ' where Identity_Column=' + ltrim(rtrim(str(@loop_counter))) + ')'
Print @sqlStatement EXECUTE sp_executesql @sqlStatement,@parameter_String,@compare_string
Print @compare_String ------------------
When I print the value of @compare_String in the end its a NULL. However, if I run the same query without the set @compare_string clause, it does work perfectly and returns the values of two columns concatenated together. Any clues as to where I might be going wrong?
Thanks,
"Robin Tucker" <id*************************@reallyidont.com> wrote in
message news:<bs*******************@news.demon.co.uk>... You need a parms string and an exec string, like this:
SET @Parms = `@compare_string`
set @sqlStatement='Set @compare_string=' + '(Select ' + @group_column_list_mod + ' from ' + @Tbl_Name + '_Sorted' + ' where Identity_Column=' + ltrim(rtrim(str(@loop_counter))) + ')'
EXECUTE sp_executesql @sqlStatement, @Parms, @compare_string
SET @Error = COALESCE ( NULLIF ( @Error, 0 ), @@ERROR )
exec(@sqlStatement)
The error message that I get is as follows:
Must declare the variable '@compare_string'.
Here @compare_string has already been declared in the procedure and I don't have a problem using the variable anywhere else but this SQL Statement (when called using the EXEC function).
I am not sure why SQL Server can't see the variable declared when used in a string in conjunction with EXEC. Is this a syntax issue? Any help on this issue would be greatly appreciated!
Thanks in advance.
Thanks for pointing me to the right direction. The code works
perfectly fine now. The problem was with the syntax that Erland
Sommarskog mentioned in his posting. You have to specify the parameter
type (input or output) in the parameter specification string (the
second argument to sp_executesql).
Best Regards,
"Robin Tucker" <id*************************@reallyidont.com> wrote in message news:<bs*******************@news.demon.co.uk>... Yes, that won't work. Sorry, I just focused on the parameter part.
You can just do this:
select @compare_string = mytable.myfield FROM mytable where Identity_Column = myvalue
or, in your specific case:
'Select @compare_string=' + @group_column_list_mod + ' from ' + @Tbl_Name + '_Sorted' + ' where Identity_Column=' + ltrim(rtrim(str @loop_counter))'
At least this is the syntax you should use in this case. Otherwise, you are effectively trying to bind @compare_string to a recordset result, which doesn't work.
Make sure you add in the error checking afterwards!! :)
"Aamer Nazir" <aa***********@hotmail.com> wrote in message news:60**************************@posting.google.c om... Thanks for your reply. The sp_executesql procedure still doesn't give the desired results. I am posting the updated piece of code and sample output from the Query Analyzer.
------------------ set @parameter_String=N'@compare_string nvarchar(4000)'
set @sqlStatement='Set @compare_string=(Select ' + @group_column_list_mod + ' from ' + @Tbl_Name + '_Sorted' + ' where Identity_Column=' + ltrim(rtrim(str(@loop_counter))) + ')'
Print @sqlStatement EXECUTE sp_executesql @sqlStatement,@parameter_String,@compare_string
Print @compare_String ------------------
When I print the value of @compare_String in the end its a NULL. However, if I run the same query without the set @compare_string clause, it does work perfectly and returns the values of two columns concatenated together. Any clues as to where I might be going wrong?
Thanks,
"Robin Tucker" <id*************************@reallyidont.com> wrote in message news:<bs*******************@news.demon.co.uk>... You need a parms string and an exec string, like this:
SET @Parms = `@compare_string`
set @sqlStatement='Set @compare_string=' + '(Select ' + @group_column_list_mod + ' from ' + @Tbl_Name + '_Sorted' + ' where Identity_Column=' + ltrim(rtrim(str(@loop_counter))) + ')'
EXECUTE sp_executesql @sqlStatement, @Parms, @compare_string
SET @Error = COALESCE ( NULLIF ( @Error, 0 ), @@ERROR )
> > exec(@sqlStatement) > > The error message that I get is as follows: > > Must declare the variable '@compare_string'. > > Here @compare_string has already been declared in the procedure and I > don't have a problem using the variable anywhere else but this SQL > Statement (when called using the EXEC function). > > I am not sure why SQL Server can't see the variable declared when used > in a string in conjunction with EXEC. Is this a syntax issue? Any help > on this issue would be greatly appreciated! > > Thanks in advance.
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: |
last post by:
Hi,
I'm trying to use the cookie munging session handling behaviour of asp.net
instead of cookies themselves as I'm finding quite a few people are barring
cookies (especially AOL users). If I...
|
by: R Duke |
last post by:
I have tried everything I can think of to change the visible property
of a design time created control from a dynamically created control's
command event handler.
Here is the scenario.
I have...
|
by: Andrew |
last post by:
Hey all,
Working on revamping our Intranet here and making use of the LDPA, Active
Directory, Directory Services, etc. that .Net provides. I am still fairly
new on this subject, so the problem...
|
by: Ron L |
last post by:
We are working on a distributed VB.Net application which will access a SQL
database located on a known server. Each client will run on the user's
local machine. To implement this, we are trying...
|
by: David Hearn |
last post by:
I have a property in a user control that I am setting:
Private strPageName as String
Public Property PageName() as String
Get
Return strPageName
End Get
Set(byVal Value as String)...
|
by: Toni |
last post by:
Hello! I'm building an application where the user can update his own
personal information in a database using a form. The program fetches the
user's information from the database, fills the form...
|
by: sara |
last post by:
I have a report that runs fine with data. If there is no data, I have
its NO Data event sending a MsgBox and cancelling the report.
Then it seems I still get the 2501 message on the Open Report...
|
by: laredotornado |
last post by:
Hi,
I'm using PHP 4.4.4 on Apache 2 on Fedora Core 5. PHP was installed
using Apache's apxs and the php library was installed to
/usr/local/php. However, when I set my "error_reporting"...
|
by: tshad |
last post by:
In VS 2003, I am setting up an abstract class that is setting up classes for
each datatype of VB.Net (as well as C#).
I am trying to set it up so that most of the work is done in the Abstract...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |