如何将存储过程中的数据导入临时表?

use*_*246 14 sybase-ase

我正在研究sybase ASE 15.寻找类似这样的东西

Select * into #tmp exec my_stp;
Run Code Online (Sandbox Code Playgroud)

my_stp返回10个数据行,每行有两列.

Ada*_*amH 6

在ASE 15中,我相信你可以使用函数,但它们不会帮助多行数据集.

如果您的存储过程返回带有"select col1,col2 from somewhere"的数据,那么就无法获取该数据,它只会流回客户端.

您可以做的是将数据直接插入临时表.这可能有点棘手,就好像你在sproc中创建临时表一旦sproc完成运行就被删除而你没有看到内容.这方面的技巧是在sproc之外创建临时表,但是从sproc引用它.这里的难点是每次重新创建sproc时都必须创建临时表,否则你会得到"table not found"错误.


    --You must use this whole script to recreate the sproc    
    create table #mine
    (col1 varchar(3),
    col2 varchar(3))
    go
    create procedure my_stp
    as
    insert into #mine values("aaa","aaa")
    insert into #mine values("bbb","bbb")
    insert into #mine values("ccc","ccc")
    insert into #mine values("ccc","ccc")
    go
    drop table #mine
    go
Run Code Online (Sandbox Code Playgroud)

运行代码:


create table #mine
(col1 varchar(3),
col2 varchar(3))
go

exec my_stp
go

select * from #mine
drop table #mine
go
Run Code Online (Sandbox Code Playgroud)


Jak*_*rab 5

我刚遇到这个问题,迟到总比没有好......

这是可行的,但是在对接中有一个巨大的痛苦,涉及一个Sybase" 代理表 ",它是另一个本地或远程对象(表,过程,视图)的代表.以下工作在12.5中,较新的版本希望有更好的方法.

假设您将存储过程定义为:

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

首先切换到tempdb:

use tempdb
Run Code Online (Sandbox Code Playgroud)

然后创建一个代理表,其中列与结果集匹配:

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"
Run Code Online (Sandbox Code Playgroud)

注意事项:

  • "loopback"是Sybase等效的localhost,但您可以将其替换为在服务器的sysservers表中注册的任何服务器.
  • 当Sybase执行存储过程时,_timestamp参数将转换为@timestamp,并且必须将声明为此类的所有参数列定义为null.

然后,您可以从您自己的数据库中选择这样的表:

declare @myTimestamp datetime
set @myTimestamp = getdate()

select * 
from tempdb..myproxy_extractSomething
where _timestamp = @myTimestamp
Run Code Online (Sandbox Code Playgroud)

这很简单.然后插入临时表,首先创建它:

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)
)
Run Code Online (Sandbox Code Playgroud)

并结合:

insert into #myTempExtract (column_a, column_b)
select column_a, column_b
    from tempdb..myproxy_extractSomething
    where _timestamp = @myTimestamp
Run Code Online (Sandbox Code Playgroud)


Val*_*ion -1

不确定 Sybase,但在 SQL Server 中,以下内容应该有效:

插入 #tmp (col1,col2,col3...) exec my_stp