0

我有一个这样的 PL/SQL 语句(它不是存储过程或函数):

DECLARE
  curstatus VARCHAR(2);
  person_id varchar(6) := 100;
BEGIN
   SELECT cur_status INTO curstatus FROM Persons WHERE PID=cast(person_id as number(6));
   IF curstatus='F' THEN
    SELECT PID FROM Reports WHERE Report_Type='F' AND PID = person_id;
   END IF;
END;

我想从报告中返回所有 PID(将它们显示到屏幕上),但是我收到一个错误:an INTO clause is expected.

请注意,这不是存储过程或函数。我怎样才能退回桌子?

4

4 回答 4

4

像下面这样的纯 SQL 解决方案肯定会起作用吗?

   SELECT r.PID
   FROM Reports r, Persons p
   WHERE r.Report_Type = 'F' AND r.PID = p.PID
   AND p.cur_status = 'F'
   AND p.PID = 100;
于 2013-09-24T12:23:34.823 回答
1

错误消息的原因是into第二个 select 语句中没有。

DECLARE
  curstatus VARCHAR(16);
  person_id varchar(6) := 100;
  p_pid VARCHAR(32);
BEGIN
   SELECT cur_status INTO curstatus FROM Persons WHERE PID=cast(person_id as number(6));
   IF curstatus='F' THEN
    SELECT PID into p_pid FROM Reports WHERE Report_Type='F' AND PID = person_id;
   END IF;
END;

您可以使用返回的函数sys_refcursor

例如

CREATE OR REPLACE FUNCTION testfunc
    RETURN SYS_REFCURSOR
AS
    curstatus   persons.cur_status%TYPE;
    person_id   VARCHAR (6) := 100;
    r_cursor    SYS_REFCURSOR;
BEGIN
    SELECT  cur_status
      INTO  curstatus
      FROM  persons
     WHERE  pid = CAST (person_id AS NUMBER (6));

    IF curstatus = 'F'
    THEN
        OPEN r_cursor FOR
            SELECT  pid
              INTO  p_pid
              FROM  reports
             WHERE  report_type = 'F' AND pid = person_id;
    END IF;

    RETURN r_cursor;
END;
于 2013-09-24T12:05:10.917 回答
0

尝试这个:

DECLARE
    CURSTATUS VARCHAR ( 2 );
    PERSON_ID VARCHAR ( 6 ) := 100;
    RESULTS REPORTS.PID%TYPE;
BEGIN
    SELECT
          CUR_STATUS
    INTO
          CURSTATUS
    FROM
          PERSONS
    WHERE
          PID = CAST ( PERSON_ID AS NUMBER ( 6 ) );

    IF CURSTATUS = 'F'
    THEN
        SELECT
              PID
        INTO
              RESULTS
        FROM
              REPORTS
        WHERE
              REPORT_TYPE = 'F'
              AND PID = PERSON_ID;
        DBMS_OUTPUT.PUT_LINE  ( 'Result is:'||RESULTS );
    END IF;

END;
于 2013-09-24T12:07:22.860 回答
0

您不能直接从 PL/SQL 块调用查询,因为 oracle 对 SQL 和 PL/SQL 使用两种不同的引擎。

所有 SQL 查询都在 SQL Engine 中执行,所有 PL/SQL 操作都在 PL/SQL Engine 中处理。当您在 PL/SQL 块中运行查询时,数据集必须从 sql 传递到 pl/sql 引擎。而在 PL/SQL 引擎中,它需要作为变量或集合绑定到内存区域。对于这个绑定过程,我们需要 INTO 子句。一旦您声明了一个变量并使用 INTO 子句绑定数据,它就可供 PL/SQL 引擎使用数据。

于 2013-09-24T13:09:40.327 回答