Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using a variable for a column name

Status
Not open for further replies.

JefB

Programmer
Dec 29, 2000
56
Is it possible to feed a column name to a query as an argument in a procedure?

My goal is a procedure to pull the value(s) from a single field, on the fly using the following code:

CREATE PROCEDURE [dbo].[Eval_Test_Proc]

@FldName text

AS

Select @FldName from

(SELECT Evaluee, Initiative, PosAttitudeCaller, PosAttitudeCoWork, GiveFeedback, Extreme
FROM dbo.TeamEval_Data) x
GO


However, when I execute the procedure:
EXEC dbo.Eval_Test_Proc 'Initiative'
All I get is the word "Initiative"

Any suggestions?

JefB
 
Try this

CREATE PROCEDURE [dbo].[Eval_Test_Proc]

@FldName text

AS
DECLARE @sql varchar(100)

select @sql='Select ' + @FldName + ' from dbo.TeamEval_Data'

exec (@sql)

GO
 
Thanks all. Now for the next part: How do I put this into a custom function?

For instance; I want to return the value IN @FldName:

CREATE PROCEDURE [dbo].[Eval_Test_Proc]

@FldName text

AS
DECLARE @sql varchar(100)

select @sql='Select ' + @FldName + ' from dbo.TeamEval_Data
Where Status = A'

exec (@sql)

GO


Evidently, I cannot execute a procedure within a custom function.

JefB
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top