在PL \ SQL中设置SYS_REFCURSOR以返回数据集

时间:2016-06-16 22:02:14

标签: sql oracle plsql oracle-sqldeveloper sys-refcursor

我有一个PL \ SQL,它返回一个数据集,该数据集基本上按帐户|| product在两个不同的表中验证卷。

我的最终目标是通过传递日期参数(通过Excel提供)并调用存储过程,将此数据集直接拉入电子表格(使用Excel-VBA)。

在PL \ SQL上读了一下后,似乎我需要一个游标变量来存储提供的最终结果数据集,这样我就可以将它写入电子表格(作为记录集)。

以下在线示例,我已经编写了我的程序

CREATE OR REPLACE PROCEDURE PROC_REG_SPLIT_RECON (dStart IN Date, dEnd IN Date) 
-- procedure to check regional splits creation is okay
-- procedure checks volumes by account and product from FACT_TRADE_PRESPLIT_ROLLUP to FACT_TRADE_ROLLUP

-- SQLDeveloper doesn't like this section and I can't figure out how to set this up correctly, no matter what I do and research.

RETURN SYS_REFCURSOR 
AS 
l_return SYS_REFCURSOR; 
-- end of section not working

BEGIN

OPEN l_return FOR 

    SELECT OpStats.Account, OpStats.Platform, OpStats.Volume OpStatsVol, RegSplits.Volume RegSplitsVol, (OpStats.Volume-RegSplits.Volume) Difference FROM

   (a bunch of union queries) OpStats,

   (a bunch of other union queries) RegSplits

WHERE OpStats.Account = RegSplits.Account (+) And OpStats.Platform = RegSplits.Platform (+)
ORDER BY OpStats.Account ASC, OPStats.Platform DESC;

RETURN l_return;

END;

FWIW,查询工作正常并按预期返回结果。有谁知道为什么我的PL \ SQL没有编译?或者,如果我的方法偏离基础?

2 个答案:

答案 0 :(得分:2)

您正在创建Oracle过程,并且过程不能具有指定返回数据类型的RETURN关键字。创建一个Oracle函数来执行您想要执行的操作。

CREATE OR REPLACE FUNCTION FUN_REG_SPLIT_RECON 
...
RETURN SYS_REFCURSOR
....

答案 1 :(得分:1)

尝试创建Function,因为RETURN语句仅用于FUCNTION。 PROCEDURE中的RETURN语句仅用于在所需位置正常终止程序。希望下面的代码有帮助。

CREATE OR REPLACE FUNCTION PROC_REG_SPLIT_RECON( --Function has to be incorporated
    dStart IN DATE,
    dEnd   IN DATE)
  -- procedure to check regional splits creation is okay
  -- procedure checks volumes by account and product from FACT_TRADE_PRESPLIT_ROLLUP to FACT_TRADE_ROLLUP
  -- SQLDeveloper doesn't like this section and I can't figure out how to set this up correctly, no matter what I do and research.
  RETURN SYS_REFCURSOR
AS
  l_return SYS_REFCURSOR;
  -- end of section not working
BEGIN
  OPEN l_return FOR SELECT OpStats.Account, OpStats.Platform, OpStats.Volume OpStatsVol, RegSplits.Volume RegSplitsVol, (OpStats.Volume-RegSplits.Volume) Difference FROM (a bunch OF
  UNION queries) OpStats, (a bunch OF other
  UNION queries) RegSplits WHERE OpStats.Account = RegSplits.Account (+) AND OpStats.Platform = RegSplits.Platform (+) ORDER BY OpStats.Account ASC, OPStats.Platform DESC;
  RETURN l_return;
END;