Skip to content
 

Insert Stored Procedure Results Into Table

You are currently browsing comments. If you would like to return to the full story, you can read the full entry here: “Insert Stored Procedure Results Into Table”.

16 Comments

  1. Pathan says:

    Hi,
    i have created following storedProc
    How can i Execute that and how can i insert the values into table
    ALTER proc insert_details(@UserId int ,@UserName varchar(50),@MobileNo varchar(50),@UserRole int)
    AS
    BEGIN
    INSERT INTO Registation
    (UserId,UserName,MobileNo,UserRole)
    values(@UserId ,@UserName ,@MobileNo ,@UserRole )
    RETURN 0
    END

    Thanks a lot in advance

  2. URVISH SUTHAR says:

    DECLARE @EmpGroup INT =3 ,
    @IsActive BIT=1

    DECLARE @tblEmpMaster AS TABLE
    (EmpCode VARCHAR(20),EmpName VARCHAR(50),EmpAddress VARCHAR(500))

    INSERT INTO @tblEmpMaster EXECUTE SPGetEmpList @EmpGroup,@IsActive

    SELECT * FROM @tblEmpMaster

  3. joy says:

    how make a procedure to insert the values where one column is the foregin key of the primary key in a single table
    like

    create table test1(id int identity constraint mno1 primary key ,name varchar(100), sno int constraint constraint_test1_id_fk references test1(id))

  4. farid says:

    How produce present store procedure from tables?

  5. rmotiani says:

    How to pass a table variable in OpenRowSet?

  6. Vince says:

    “Retrieving the result set from a stored procedure has historically proven to be a difficult task.”

    Uhh… why not just:

    INSERT INTO #TABLE
    EXEC sp_name

    or if you need specific columns
    INSERT INTO #TABLE
    (ColumnName1, ColumnName2)
    EXEC sp_name

    Am I missing the true point of this post? It doesn’t seem very a very difficult task to me.

    • Derek Dieter says:

      Hi Vince,

      The problem I’ve always faced is not knowing what the column names or data types is of the procedure you are executing in advance. These are needed in order to create the temp table definition. The ultimate solution is to create the temp table dynamically based on the result set of the procedure at execution time. That’s the scenario this article addresses. Does that make sense or am I missing something also?

  7. Sandeep says:

    Hi,

    Everything works fine. But I am facing problem when Sproc outputs a XML result set.

    For Example,
    Output of
    EXEC DB.schema.ProcName

    Result set is

    4F7135AF-03C2-4166-9645-A81EED3656AC
    false
    0

    {NONE}

    However when i tried

    SELECT *
    FROM OPENQUERY([LOCALSERVER], ‘EXEC DB.schema.ProcName’)

    Result set is something like this below. which is in binary or image format.

    0x44065400690063006B006500740044085400690063006B006500740049004400440E53006F00750072006300650045006E00640070006F0069006E0074004413440065007300740069006E006100740069006F006E0045006E00640070006F0069006E0074004409500072006F0063006500730073004900440044114F0072006900670069006E0061006C004D00650073007300610067006500490040000040074727565810C010D410310E4040000050066616C7365810D010E410310E4040000050066616C7365810E010F410310E4040000010030810F0110410111410112410310E404000006007B4E4F4E457D8112811181108101

    How do I proceed from here.?

  8. Stephen says:

    So, how do I take a stored procedure that has multiple selects and take the results of those selects and put them into different tables? Any help would be greatly appreciated. Thanks!

    • Derek Dieter says:

      Hi Stephen,

      You can’t do that unfortunately. While this article does show how to take the result of a stored procedure and capture the result, it’s really a frowned upon way to do things. Ultimately, procedures should not return multiple result sets. When creating a procedure, the goal should be to make it as atomic as possible. Meaning it should only serve one purpose.

  9. unruledboy says:

    how to pass a temp table from one sp to another sp without the table name and exec sql method?

    I mean can we pass the temp table like:

    CREATE PROCEDURE TestTablePara2
    @TempTable TABLE
    AS

  10. Coco says:

    Sick man! I have to document a full db of sprocs (fun) and this’ll do the trick nicely for getting some things that’ll me go

  11. [...] For that example, follow this link: (Insert Results of Stored Procedure Into Table) [...]

post a comment OR Post Your Question on our ASK! Community!