如何在 PL/SQL 块中打印光标?

How to print a cursor in a PL/SQL block?

我似乎无法打印光标,我做错了什么?

DECLARE
  LEADEMAIL VARCHAR2(200);
  CLIENTID NUMBER;
  v_Return ON24MASTER.WEBCAST_REPORTS.ResultSetCursor;
BEGIN
  LEADEMAIL := 'nunyo@business.com';
  CLIENTID := 22921;

  v_Return := WEBCAST_REPORTS.LEAD_BASIC_INFO(
    LEADEMAIL => LEADEMAIL,
    CLIENTID => CLIENTID
  );

    DBMS_OUTPUT.PUT_LINE('v_Return = ' || v_Return);
--    :v_Return := v_Return;

END;

我收到以下错误:

Error report -
ORA-06550: line 14, column 26:
PLS-00306: wrong number or types of arguments in call to '||'
ORA-06550: line 14, column 5:
PL/SQL: Statement ignored
06550. 00000 -  "line %s, column %s:\n%s"
*Cause:    Usually a PL/SQL compilation error.
*Action:

大部分代码直接取自 运行 来自 SQL 开发人员的函数。

这是封装函数:

  FUNCTION LEAD_BASIC_INFO(
    leadEmail VARCHAR2,
    clientId NUMBER
  ) RETURN ResultSetCursor IS
    resultSet ResultSetCursor;
    email VARCHAR2(1000);
    webcastEngagement NUMBER(10,1);
    videoEngagement NUMBER(10,1);
    documentEngagement NUMBER(10,1);
    totalEngagement NUMBER(10,1);
    --averageEngagement NUMBER(4,1);
    totalWebcastSeconds NUMBER(10);
    engagementMinutes NUMBER(10, 1);
    last30DaysEM NUMBER(10, 1);
    last60DaysEM NUMBER(10, 1);
    fromDate DATE;
    engagementPrediction NUMBER(10);
  BEGIN...

此外,我无法使用 select 语句打印结果,因为函数也有 DML。

你不能打印那样的光标;它必须隐式地将行和列转换为字符串,这太过分了。 dbms_output.put_line() 过程只接受字符串参数 - 或任何可以隐式转换为字符串的参数。光标不能。

您将不得不遍历游标结果集,获取合适的记录类型;然后在该循​​环中进行 dbms_output 调用,将结果集中的所有列值(如果您尝试模拟 select 进行格式化和填充)连接成单个字符串。

不知道 ON24MASTER.WEBCAST_REPORTS.ResultSetCursor 是如何定义的(大概是 TYPE ResultSetCursor IS REF CURSOR),或者在您的过程中填充它的查询正在返回什么 - 哪些列名 - 很难更具体。

但是因为您已经为 SQL 开发人员标记了它,您可以使用它的内置处理来处理 ref 游标变量,这很方便:

variable rc refcursor;

DECLARE
  LEADEMAIL VARCHAR2(200);
  CLIENTID NUMBER;
BEGIN
  LEADEMAIL := 'nunyo@business.com';
  CLIENTID := 22921;

  :rc := WEBCAST_REPORTS.LEAD_BASIC_INFO(
    LEADEMAIL => LEADEMAIL,
    CLIENTID => CLIENTID
  );

END;
/

print rc

在块之前用 the variable command. Inside the block that is used instead of a local v_Return, so that doesn't even need to be declared locally. (Note the colon before :rc in the assignment from the function - that denotes a bind variable). And then after the block the client lets you print the ref cursor. (Those doc links are for SQL*Plus, but they are among the the many commands SQL Developer supports 声明绑定变量 rc。)

使用虚拟包:

create or replace package WEBCAST_REPORTS AS
  TYPE ResultSetCursor IS ref cursor;
  FUNCTION LEAD_BASIC_INFO(
    leadEmail VARCHAR2,
    clientId NUMBER
  ) RETURN ResultSetCursor;
end WEBCAST_REPORTS;
/

create or replace package body WEBCAST_REPORTS AS
  FUNCTION LEAD_BASIC_INFO(
    leadEmail VARCHAR2,
    clientId NUMBER
  ) RETURN ResultSetCursor IS
    resultSet ResultSetCursor;
  BEGIN
    OPEN resultSet FOR select * from dual;
    RETURN resultSet;
  END LEAD_BASIC_INFO;
end WEBCAST_REPORTS;
/

然后我在上面显示的代码,运行 作为脚本,在脚本输出中显示了这个 window:

PL/SQL procedure successfully completed.


D
-
X

在 Oracle 12c 中,您可以使用 DBMS_SQL.RETURN_RESULT。即,

DECLARE
  LEADEMAIL VARCHAR2(200);
  CLIENTID NUMBER;
  v_Return ON24MASTER.WEBCAST_REPORTS.ResultSetCursor;
BEGIN
  LEADEMAIL := 'nunyo@business.com';
  CLIENTID := 22921;

  v_Return := WEBCAST_REPORTS.LEAD_BASIC_INFO(
    LEADEMAIL => LEADEMAIL,
    CLIENTID => CLIENTID
  );

    DBMS_SQL.RETURN_RESULT(v_Return); 

END;

SQL*开发者将打印结果。