SQL Server Function Dynamic Column
-
I am trying to creat a function in SQL Server where it returns a the value of a column in a table where the column is passed as a parameter. Below is the function where I want to return the value in the user table for column @field. Thanks for your help. CREATE FUNCTION [dbo].[USER_RETURNCOLUMN] (@userid decimal, @field varchar(200)) RETURNS varchar(2000) AS BEGIN DECLARE @RETURNVALUE varchar(100) SELECT @RETURNVALUE = '' SELECT @RETURNVALUE = @field FROM USER_TABLE WHERE USER_ID = @userid RETURN @RETURNVALUE END
-
I am trying to creat a function in SQL Server where it returns a the value of a column in a table where the column is passed as a parameter. Below is the function where I want to return the value in the user table for column @field. Thanks for your help. CREATE FUNCTION [dbo].[USER_RETURNCOLUMN] (@userid decimal, @field varchar(200)) RETURNS varchar(2000) AS BEGIN DECLARE @RETURNVALUE varchar(100) SELECT @RETURNVALUE = '' SELECT @RETURNVALUE = @field FROM USER_TABLE WHERE USER_ID = @userid RETURN @RETURNVALUE END
If you ask me no chance to do this in a user defined function. You would need dynamic SQL with
exec( @stmt )
and that is not allowed in UDFs. Can't you use a stored proc? You can use dynamic SQL there. Regards, Rainer. Rainer Stropek Visit my blog at http://www.cubido.at/rainers