Search code examples
t-sqlstored-procedures

T-Sql How to return a table from a storedproc in another stored proc


I would like to do the following. Basically have a stored procedure call another stored procedure that returns a table. How is this done?

    ALTER PROC [GETSomeStuff]
    AS
    BEGIN
    
    @table = EXEC CB_GetLedgerView @accountId, @fromDate, @toDate, @pageSize, @pageNumber, @filter, @status, @sortExpression, @sortOrder, @virtualCount OUTPUT

    SELECT * FROM @table
    --Do some other stuff here        
    END

Solution

  • The target of a stored procedure has to be a temp or actual table so you can

        Insert into #table exec CB_GetLedgerView @accountId, @fromDate, 
    @toDate, @pageSize, @pageNumber, 
    @filter, @status, @sortExpression, 
    @sortOrder, @virtualCount OUTPUT
    

    If the output result set of the stored procedure does not match the ordinal positions and count of the rows in the target table, specify a column list.