I have code like this:
DECLARE @.CurrentValue nvarchar(1000)
EXEC('SELECT @.CurrentValue = min(Column) FROM Table')
PRINT @.CurrentValue
When I execute this code, I get an error message that I have to declare
@.CurrentValue. Apparently the code in the EXEC statement runs in it's own
scope and it doesn't see variables outside that scope.
But if I try to declare the variable inside the EXEC string, I can't access
it outside the EXEC statement.
I tried creating a global variable (@.@.CurrentValue), but that doesn't seem
to work either."MatthewR" <MatthewR@.discussions.microsoft.com> schrieb im Newsbeitrag
news:D5D08C86-E390-45C0-8D92-A341D457E50B@.microsoft.com...
>I have code like this:
> DECLARE @.CurrentValue nvarchar(1000)
> EXEC('SELECT @.CurrentValue = min(Column) FROM Table')
> PRINT @.CurrentValue
> When I execute this code, I get an error message that I have to declare
> @.CurrentValue. Apparently the code in the EXEC statement runs in it's own
> scope and it doesn't see variables outside that scope.
> But if I try to declare the variable inside the EXEC string, I can't
> access
> it outside the EXEC statement.
> I tried creating a global variable (@.@.CurrentValue), but that doesn't seem
> to work either.
>|||Hi,
try the following
DECLARE @.CurrentValue nvarchar(1000)
set @.CurrentValue = (Select min(Column) FROM Table)
Hope it helps
Regards
Alex|||Have a look here:
http://www.support.microsoft.com/?id=262499 Using OutPut Params &
sp_executeSql
Andrew J. Kelly SQL MVP
"MatthewR" <MatthewR@.discussions.microsoft.com> wrote in message
news:D5D08C86-E390-45C0-8D92-A341D457E50B@.microsoft.com...
>I have code like this:
> DECLARE @.CurrentValue nvarchar(1000)
> EXEC('SELECT @.CurrentValue = min(Column) FROM Table')
> PRINT @.CurrentValue
> When I execute this code, I get an error message that I have to declare
> @.CurrentValue. Apparently the code in the EXEC statement runs in it's own
> scope and it doesn't see variables outside that scope.
> But if I try to declare the variable inside the EXEC string, I can't
> access
> it outside the EXEC statement.
> I tried creating a global variable (@.@.CurrentValue), but that doesn't seem
> to work either.
>|||http://www.aspfaq.com/2492
This is my signature. It is a general reminder.
Please post DDL, sample data and desired results.
See http://www.aspfaq.com/5006 for info.
"MatthewR" <MatthewR@.discussions.microsoft.com> wrote in message
news:D5D08C86-E390-45C0-8D92-A341D457E50B@.microsoft.com...
>I have code like this:
> DECLARE @.CurrentValue nvarchar(1000)
> EXEC('SELECT @.CurrentValue = min(Column) FROM Table')
> PRINT @.CurrentValue
> When I execute this code, I get an error message that I have to declare
> @.CurrentValue. Apparently the code in the EXEC statement runs in it's own
> scope and it doesn't see variables outside that scope.
> But if I try to declare the variable inside the EXEC string, I can't
> access
> it outside the EXEC statement.
> I tried creating a global variable (@.@.CurrentValue), but that doesn't seem
> to work either.
>|||Look in the posting from ED and today (15.04.2005 20:47) (for Google
searcher the article
http://support.microsoft.com/defaul...kb;en-us;262499)
It describes using the Return value from an Execute. The problem is that in
the context of executing a new session is established and closed when the
statement was executed. So in the next line the value doesnt exists anymore
(in your query)
HTH, Jens Smeyer
http://www.sqlserver2005.de
--
"MatthewR" <MatthewR@.discussions.microsoft.com> schrieb im Newsbeitrag
news:D5D08C86-E390-45C0-8D92-A341D457E50B@.microsoft.com...
>I have code like this:
> DECLARE @.CurrentValue nvarchar(1000)
> EXEC('SELECT @.CurrentValue = min(Column) FROM Table')
> PRINT @.CurrentValue
> When I execute this code, I get an error message that I have to declare
> @.CurrentValue. Apparently the code in the EXEC statement runs in it's own
> scope and it doesn't see variables outside that scope.
> But if I try to declare the variable inside the EXEC string, I can't
> access
> it outside the EXEC statement.
> I tried creating a global variable (@.@.CurrentValue), but that doesn't seem
> to work either.
>|||> I tried creating a global variable (@.@.CurrentValue),
Where did you get the idea of a "global variable"? This is merely a
variable called @.CurrentValue, with a @. prefix to indicate it is a variable.
There is absolutely no difference between @.@.CurrentValue and @.CurrentValue.
A|||Thanks for the link. sp_executeSQL with OUTPUT parameters worked great!
"Andrew J. Kelly" wrote:
> Have a look here:
> http://www.support.microsoft.com/?id=262499 Using OutPut Params &
> sp_executeSql
>
> --
> Andrew J. Kelly SQL MVP
>
> "MatthewR" <MatthewR@.discussions.microsoft.com> wrote in message
> news:D5D08C86-E390-45C0-8D92-A341D457E50B@.microsoft.com...
>
>