PLSQL返回表集

时间:2013-09-24 11:59:25

标签: sql oracle plsql

我有这样的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 个答案:

答案 0 :(得分: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;

答案 1 :(得分:1)

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

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

的函数

E.g。

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;

答案 2 :(得分: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;

答案 3 :(得分: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引擎就可以使用该数据了。