SELECT Query on Stored Procedure Output

  • I am trying to query the output of a stored procedure. However I am getting a syntax error.

    E.x:

    SELECT * FROM (EXEC proc_A(1,2,'Test'))

    Please suggest the correct syntax to achive the above result.

    Thanks,

    -NS

  • insert into tablename exec proc_A(1,2,'Test), then query the tablename

    or

    convert proc_A to table function and query directly from the function.

  • Hope this helps

    declare @procOut int

    exec @procOut = proc_A(1,2,'Test)

    select @procOut

  • Search the script library or google. Pretty likely someone else has done this before.

    --
    Frank Kalis
    Microsoft SQL Server MVP
    Webmaster: http://www.insidesql.org/blogs
    My blog: http://www.insidesql.org/blogs/frankkalis/[/url]

  • its the same every stored proc. the procOut returns integer,zero meaning successful,anything > 0 some error.

  • Up to you but here is a simple example to demonstrate how it works.

    SET NOCOUNT ON

    CREATE TABLE #tblWho (

     spid int primary key,

     ecid int,

     status varchar(30),

     loginame varchar(128),

     hostname varchar(128),

     blk int,

     dbname varchar(128),

     cmd varchar(16)

    )

    INSERT INTO #tblWho EXEC sp_who

    SELECT * FROM #tblWho

    SELECT * FROM #tblWho WHERE blk != 0 OR spid IN (SELECT blk FROM #tblWho WHERE blk != 0)

    DROP TABLE #tblWho

  • Afraid not at least not that way. You could create a linked server to itself and try

     

    SELECT INTO #tblWho FROM OPENQUERY(MYSELF,'EXEC sp_who')

     

    But I don't like that method because of how it does connections and the time they take to timeout.

  • Not with stored procedures. Unfortanely, you will have to create manually the table. The columns doesn't have to have the same name as the recordset that the sp returns, but yes the same data types.

     

Viewing 8 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic. Login to reply