Monday, March 19, 2012

Get Value of Parameter whos name is in a variable

If I have a varialbe that contains the name of a Parameter in my stored
procedure is it possible to get the value of that parameter
Example:
CREATE PROCEDURE test
@.myParam1 varchar(50)
AS
DECLARE @.ParamName varchar(50)
@.ParamName = '@.myParam1'
EXEC ('SELECT ' + @.ParamName)
the above code doesn't work but is there a way to do this?Don't enclose variables in quotes. Also, you need to use SET to assign a
value.
SET @.ParamName = @.myParam1
Select @.ParamName
will give you the result for debugging purposes.
If you plan on using dynamic SQL - i.e. EXEC(SQLStatement)
then check out Erland's article on it...
http://www.sommarskog.se/dynamic_sql.html
<regmellon@.gmail.com> wrote in message
news:1148326918.904046.55660@.g10g2000cwb.googlegroups.com...
> If I have a varialbe that contains the name of a Parameter in my stored
> procedure is it possible to get the value of that parameter
> Example:
> CREATE PROCEDURE test
> @.myParam1 varchar(50)
> AS
> DECLARE @.ParamName varchar(50)
> @.ParamName = '@.myParam1'
> EXEC ('SELECT ' + @.ParamName)
>
> the above code doesn't work but is there a way to do this?
>|||well that was a simplified example of what I am trying to do I am
actually looping through a record set of all the parameters of a stored
proc and then trying to store the values that were passed into the
stored proc in a table to make debuging easyer. So the parameter name
is stored in @.ParamName not the value of that parameter.
A better example would be :
CREATE PROCEDURE test
@.myParam1 varchar(50) ,
@.myParam2 varchar(50)
AS
DECLARE @.ParamName varchar(50)
@.ParamName = '@.myParam' + cast(1, varchar(10))
EXEC ('SELECT ' + @.ParamName)
@.ParamName = '@.myParam' + cast(2, varchar(10))
EXEC ('SELECT ' + @.ParamName)|||well that was a simplified example of what I am trying to do I am
actually looping through a record set of all the parameters of a stored
proc and then trying to store the values that were passed into the
stored proc in a table to make debuging easyer. So the parameter name
is stored in @.ParamName not the value of that parameter.
A better example would be :
CREATE PROCEDURE test
@.myParam1 varchar(50) ,
@.myParam2 varchar(50)
AS
DECLARE @.ParamName varchar(50)
@.ParamName = '@.myParam' + cast(1, varchar(10))
EXEC ('SELECT ' + @.ParamName)
@.ParamName = '@.myParam' + cast(2, varchar(10))
EXEC ('SELECT ' + @.ParamName)|||TRY THIS,IT WILL SURELY WORK and can u plz specify wat type of parameter.Plz
give an example.
CREATE PROCEDURE test
@.myParam1 varchar(50)
AS
DECLARE @.ParamName varchar(50)
SET @.ParamName = @.myParam1
EXEC ('SELECT ' + @.ParamName)
--
MEHAK
"regmellon@.gmail.com" wrote:

> If I have a varialbe that contains the name of a Parameter in my stored
> procedure is it possible to get the value of that parameter
> Example:
> CREATE PROCEDURE test
> @.myParam1 varchar(50)
> AS
> DECLARE @.ParamName varchar(50)
> @.ParamName = '@.myParam1'
> EXEC ('SELECT ' + @.ParamName)
>
> the above code doesn't work but is there a way to do this?
>

No comments:

Post a Comment