I want to create a T-SQL function, send table name and column name to it, and get the max value of this column.
I wrote a function like this:
CREATE FUNCTION getMaxValue
(@TableName nvarchar(30),@FieldName nvarchar(30))
RETURNS nvarchar(max)
AS
BEGIN
DECLARE @SqlString nvarchar(MAX)
--declare @Result nvarchar(MAX)
SET @SqlString = ' select max( ' + @FieldName + ') from ' + @TableName
RETURN EXEC(@SqlString)
END
but I can't use EXEC
in SET
, SELECT
or RETURN
within this function.
Please help me to solve this problem.