Just return a TABLE VARIABLE in either a FUNCTION
or a STORED PROCEDURE
and then use that TABLE VARIABLE
in your caller Stored Procedure for further processing or to pass on to another Stored Procedure as a TABLE VARIABLE Parameter.
i.e. Create a Function as follows that returns a TABLE VARIABLE.
CREATE PROCEDURE MyProcedureThatReturnsATable()
RETURNS @ReturnTable TABLE
(
-- specify columns returned by the proc here
ID INT NOT NULL,
Name nvarchar(255) NOT NULL
)
AS
BEGIN
.
..
...
--This select returns data
INSERT INTO @ReturnTable
SELECT ID, Name FROM SOME_TABLES
RETURN
END
Then in the parent Stored Procedure, you would execute above stored procedure and populate a TABLE Variable in that parent Stored Procedure as follows.
DECLARE @MyParentTableVariable as TABLE (ID INT, Name nvarchar(255))
INSERT INTO @MyParentTableVariable
EXECUTE MyProcedureThatReturnsATable
So now, in the parent Stored Procedure, you have the data from the MyProcedureThatReturnsATable
in a TABLE VARIABLE.