I would like to create a function in T-SQL which both inputs and outputs instances of a user-defined table type. However, on the line of the RETURN
statement, it is throwing an error:
Must declare the scalar variable @output_table
My code:
CREATE FUNCTION FUNC_NAME
(@input_table TableType READONLY,
@other_param VARCHAR(255))
RETURNS TableType
AS
BEGIN
DECLARE @output_table TableType;
INSERT INTO @output_table (Col1, Col2, Col3)
SELECT column1, column2, column3
FROM SOME_TABLE
WHERE column1 = @other_param;
RETURN @output_table;
END
Can someone please explain to me what I am doing wrong? Is it even possible to create a function that both inputs and outputs a table type variable? If not, are there any alternative approaches that I may use? I am using SQL Server Management Studio 2008 R2.