How can I get data from a stored procedure into a temp table?

前端 未结 4 2047
孤独总比滥情好
孤独总比滥情好 2020-12-15 22:24

Am working on sybase ASE 15. Looking for something like this

Select * into #tmp exec my_stp;

my_stp returns 10 data rows with two columns i

4条回答
  •  不思量自难忘°
    2020-12-15 22:39

    I've just faced this problem, and better late than never...

    It's doable, but a monstrous pain in the butt, involving a Sybase "proxy table" which is a standin for another local or remote object (table, procedure, view). The following works in 12.5, newer versions hopefully have a better way of doing it.

    Let's say you have a stored proc defined as:

    create procedure mydb.mylogin.sp_extractSomething (
    @timestamp datetime) as
    select column_a, column_b
        from sometable
        where timestamp = @timestamp
    

    First switch to the tempdb:

    use tempdb
    

    Then create a proxy table where the columns match the result set:

    create existing table myproxy_extractSomething (
    column_a int not null, -- make sure that the types match up exactly!
    column_b varchar(20) not null,
    _timestamp datetime null,
    primary key (column_a)) external procedure at "loopback.mydb.mylogin.sp_extractSomething"
    

    Points of note:

    • "loopback" is the Sybase equivalent of localhost, but you can substitute it for any server registered in the server's sysservers table.
    • The _timestamp parameter gets translated to @timestamp when Sybase executes the stored proc, and all parameter columns declared like this must be defined as null.

    You can then select from the table like this from your own db:

    declare @myTimestamp datetime
    set @myTimestamp = getdate()
    
    select * 
    from tempdb..myproxy_extractSomething
    where _timestamp = @myTimestamp
    

    Which is straightforward enough. To then insert into a temporary table, create it first:

    create table #myTempExtract (
        column_a int not null, -- again, make sure that the types match up exactly
        column_b varchar(20) not null,
        primary key (column_a)
    )
    

    and combine:

    insert into #myTempExtract (column_a, column_b)
    select column_a, column_b
        from tempdb..myproxy_extractSomething
        where _timestamp = @myTimestamp
    

提交回复
热议问题