I have a stored procedure that returns a result set and that I would like to convert into a user-defined function so that I can call specific fields from it.
I know that a UDF can return a table by using:
CREATE FUNCTION fn_FunctionName
RETURNS @table TABLE (col1 int, col2 varchar...)
The problem is that my procedure creates the columns for the return table dynamically, so there is no way to know the structure of the table before it is called.
Is there any way to create a function this way? I have tried writing it as:
CREATE FUNCTION [SB].[fn_FunctionName] (@var varchar(max) = NULL)
RETURNS TABLE
AS
declare @var1 ....
but I get Incorrect syntax near the keyword 'declare'
Alternatively, is there any way to call specific columns from a stored procedure that returns a table?