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
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Field name is a variable

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2001-06-19 : 16:28:25
Narayan writes "
I've a stored procedure which takes field name in one parameter. How do I execute the query & store the result in variable by avoiding temporary table?

-Stored proc does not work
CREATE PROCEDURE g_GetResult(@FldNam varchar(20), @Code char(3))
BEGIN
DECLARE @FldVal varchar(15)
--Query will always return one row
SELECT @FldVal = @FldNam
FROM MyTable
WHERE PrimaryCode = @Code

--I'm doing a lot of stuff here with @FldVal
...
END

If in the above stored proc, I use the field name, it works.
SELECT @FldVal = MyFldNam
FROM MyTable
WHERE PrimaryCode = @Code
Where "MyFldNam" is the field name in "MyTable"

But if the field name is in variable. It assigns the @FldVal with the value of @FldNam
SELECT @FldVal = @FldNam
FROM MyTable
WHERE PrimaryCode = @Code

How can I use Exec with result returning to variable?
"
   

- Advertisement -