SQL server stored procedure return a table

后端 未结 9 2182
情书的邮戳
情书的邮戳 2020-12-04 19:39

I have a stored procedure that takes in two parameters. I can execute it successfully in Server Management Studio. It shows me the results which are as I expect. However it

相关标签:
9条回答
  • 2020-12-04 20:12

    A procedure can't return a table as such. However you can select from a table in a procedure and direct it into a table (or table variable) like this:

    create procedure p_x
    as
    begin
    declare @t table(col1 varchar(10), col2 float, col3 float, col4 float)
    insert @t values('a', 1,1,1)
    insert @t values('b', 2,2,2)
    
    select * from @t
    end
    go
    
    declare @t table(col1 varchar(10), col2 float, col3 float, col4 float)
    insert @t
    exec p_x
    
    select * from @t
    
    0 讨论(0)
  • 2020-12-04 20:13

    I had a similar situation and solved by using a temp table inside the procedure, with the same fields being returned by the original Stored Procedure:

    CREATE PROCEDURE mynewstoredprocedure
    AS 
    BEGIN
    
    INSERT INTO temptable (field1, field2)
    EXEC mystoredprocedure @param1, @param2
    
    select field1, field2 from temptable
    
    -- (mystoredprocedure returns field1, field2)
    
    END
    
    0 讨论(0)
  • 2020-12-04 20:13

    Here's an example of a SP that both returns a table and a return value. I don't know if you need the return the "Return Value" and I have no idea about MATLAB and what it requires.

    CREATE PROCEDURE test
    AS 
    BEGIN
    
        SELECT * FROM sys.databases
    
        RETURN 27
    END
    
    --Use this to test
    DECLARE @returnval int
    
    EXEC @returnval = test 
    
    SELECT @returnval
    
    0 讨论(0)
  • 2020-12-04 20:15

    I do this frequently using Table Types to ensure more consistency and simplify code. You can't technically return "a table", but you can return a result set and using INSERT INTO .. EXEC ... syntax, you can clearly call a PROC and store the results into a table type. In the following example I'm actually passing a table into a PROC along with another param I need to add logic, then I'm effectively "returning a table" and can then work with that as a table variable.

    /****** Check if my table type and/or proc exists and drop them ******/
    IF EXISTS (SELECT * FROM sys.objects WHERE type = 'P' AND name = 'returnTableTypeData')
    DROP PROCEDURE returnTableTypeData
    GO
    IF EXISTS (SELECT * FROM sys.types WHERE is_table_type = 1 AND name = 'myTableType')
    DROP TYPE myTableType
    GO
    
    /****** Create the type that I'll pass into the proc and return from it ******/
    CREATE TYPE [dbo].[myTableType] AS TABLE(
        [someInt] [int] NULL,
        [somenVarChar] [nvarchar](100) NULL
    )
    GO
    
    CREATE PROC returnTableTypeData
        @someInputInt INT,
        @myInputTable myTableType READONLY --Must be readonly because
    AS
    BEGIN
    
        --Return the subset of data consistent with the type
        SELECT
            *
        FROM
            @myInputTable
        WHERE
            someInt < @someInputInt
    
    END
    GO
    
    
    DECLARE @myInputTableOrig myTableType
    DECLARE @myUpdatedTable myTableType
    
    INSERT INTO @myInputTableOrig ( someInt,somenVarChar )
    VALUES ( 0, N'Value 0' ), ( 1, N'Value 1' ), ( 2, N'Value 2' )
    
    INSERT INTO @myUpdatedTable EXEC returnTableTypeData @someInputInt=1, @myInputTable=@myInputTableOrig
    
    SELECT * FROM @myUpdatedTable
    
    
    DROP PROCEDURE returnTableTypeData
    GO
    DROP TYPE myTableType
    GO
    
    0 讨论(0)
  • 2020-12-04 20:20

    Though this question is very old but as a new in Software Development I can't stop my self to share what I have learnt :D

    Creation of Stored Procedure:

    CREAET PROC usp_ValidateUSer
    (
        @UserName nVARCHAR(50),
        @Password nVARCHAR(50)
    )
    AS
    
    BEGIN
        IF EXISTS(SELECT '#' FROM Users WHERE Username=@UserName AND Password=@Password)
        BEGIN
            SELECT u.UserId, u.Username, r.UserRole
            FROM Users u
            INNER JOIN UserRoles r
            ON u.UserRoleId=r.UserRoleId
        END
    END
    

    Execution of Stored Procedure:

    (If you want to test the execution of Stored Procedure in SQL)

    EXEC usp_ValidateUSer @UserName='admin', @Password='admin'
    

    Th Output:

    0 讨论(0)
  • The Status Value being returned by a Stored Procedure can only be an INT datatype. You cannot return other datatypes in the RETURN statement.

    From Lesson 2: Designing Stored Procedures:

    Every stored procedure can return an integer value known as the execution status value or return code.

    If you still want a table returned from the SP, you'll either have to work the record set returned from a SELECT within the SP or tie into an OUTPUT variable that passes an XML datatype.

    HTH,

    John

    0 讨论(0)
提交回复
热议问题