Help: Ways of persisting the resultset from SP?

  • is there anyway to insert the results from a stored procedure into a table/temptable

    without first knowing what's the columns returned.

    i know it works by first creating a temptable, but this only works if i know the columns.

    I am looking at something like

    Select (exec sp) into #temptable.

    i am actually trying to call a system stored procedure and i want to keep the results in a table from a stored procedure itself.

    Any help will be greatly appreciated

  • you could use a select into ...

    SELECT *

    INTO #WrkTable

    FROM OPENROWSET('SQLOLEDB','server';'user';'pass','master..sp_who')

    This would fail with sp's that return more than 1 result set as output

  • quote:


    This would fail with sp's that return more than 1 result set as output


    And those that use temp tables, e.g. sp_helpdb

    Far away is close at hand in the images of elsewhere.
    Anon.

  • Sorry ,

    yes that is correct , you should probably use "SET FMTONLY OFF EXEC master..sp_who" , this would avoid the temp table problem

  • if it's not too much i would like ur help again, the openrowset or openquery doesn't work properly on this system sp

    sp_sproc_columns, it keeps giving me error

    Syntax error converting the nvarchar value '%' to a column of data type int.

    PLS help me thanks!

  • try this

    SELECT *

    INTO #WrkTable

    FROM OPENROWSET('SQLOLEDB','server';'user';'pass','SET FMTONLY OFF EXEC master..sp_sproc_columns')

  • hi GCN, thanks for ur help, but this is not what i wanted, i want the recordset not the columns only

  • Hi , this should return the recordset not just columns

  • Try

    
    
    -- Generic example ...
    If Object_ID('TempDB..#Temp') Is Not NULL Drop Table #Temp

    select * Into #Temp from OpenRowset('SQLOLEDB',
    'Server=(local);Trusted_Connection=yes',
    'Exec Master.dbo.sp_help ')

    Select * from #Temp

    If Object_ID('TempDB..#Temp') Is Not NULL Drop Table #Temp

    This returns a record set. Some issues though:

    If SP deals with #Temp tables, results will not be returned.

    Also may try 'SET FMTONLY OFF EXEC theSP'.

    SP should have SET NOCOUNT ON, and no PRINTs ect. for best results.



    Once you understand the BITs, all the pieces come together

  • You can select the results of the procedure into a global temp table.

    create proc #temp as

    select *

    into ##so

    from sysobjects

    go

    I can then

    exec #temp

    select * from ##so

    drop table ##so

    Brian

  • this query doesn't work if i pass in the parameters(example a sp name).. any ideas? it just return no recordsets..

    but if i were to run just EXEC master..sp_sproc_columns spname

    it will return a recordset

    SELECT *

    INTO #WrkTable

    FROM OPENROWSET('SQLOLEDB','server';'user';'pass','SET FMTONLY OFF EXEC master..sp_sproc_columns spname')

  • try replacing <master> with whatever database that has sp_name...

    'SET FMTONLY OFF EXEC MyDatabase..sp_sproc_columns spname'

  • it works!!! thanks..

Viewing 13 posts - 1 through 12 (of 12 total)

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