【问题标题】:How do I insert the result set of a stored procedure into a temp table AND get the output parameters in Sybase?如何将存储过程的结果集插入临时表并在 Sybase 中获取输出参数?
【发布时间】:2016-05-13 03:46:06
【问题描述】:

我目前正在使用 Sybase ASE 15.7 并编写一个使用另一个 SP 的结果的存储过程。我想调用它并将结果插入到临时表中,因此不需要对原始 SP 进行修改。

参考此帖:How can I get data from a stored procedure into a temp table?

Jakub 使用代理表的答案与示例 SP 定义完美配合:

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

但是,还缺少最后一件!你如何获得输出参数和结果集?在我的例子中,SP 的定义如下:

create procedure mydb.mylogin.sp_extractSomething 
(
    @timestamp datetime, 
    @errcode    char(10) output
) as
    select @errcode='NOERR'
    select column_a, column_b
        from sometable
        where timestamp = @timestamp    
    if (@@rowcount = 0)
    begin
        select @errcode = 'ERR001'
    end

我定义并使用了如下的代理表:

--create proxy table
create existing table myproxy_extractSomething (
column_a int not null, 
column_b varchar(20) not null,
_timestamp datetime null,
_errcode char(10) null) external procedure at "loopback.mydb.mylogin.sp_extractSomething"

--calling sp
declare @errcode Char
declare @myTimestamp datetime
set @myTimestamp = getdate()

select * 
from myproxy_extractSomething
where _timestamp = @myTimestamp
and _errcode = @errcode
select @errcode

虽然可以成功返回结果集,但@errcode/_errcode始终为null。如何在代理表中定义输出参数?

【问题讨论】:

    标签: sql stored-procedures sybase


    【解决方案1】:

    事实证明,即使在存储过程中也不允许创建代理表。会弹出如下错误:

    Statement with location clause must be the only statement in a query batch
    

    所以我猜在 Sybase sp 中没有办法在不修改原始数据集的情况下使用另一个 sp 的结果数据集。不幸的是,剩下的唯一方法似乎是复制原始 sp(有 1.4k 行)。

    【讨论】:

      猜你喜欢
      • 2020-12-05
      • 2020-01-26
      • 2018-07-05
      • 2023-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-10
      • 2018-11-16
      相关资源
      最近更新 更多