PL / SQL - dbms输出执行立即的结果

时间:2011-05-29 07:38:06

标签: sql database oracle plsql cursor

我希望能够打印出查询的所有结果(应该由TableA中的PK过滤)并为TABLEA中的每个PK执行此操作。 这就是我到目前为止所做的:

 DECLARE
    CURSOR Curs IS SELECT DISTINCT PKID FROM TABLEA;
    BEGIN
    FOR rec IN Curs 
          LOOP
              EXECUTE IMMEDIATE 
              'SELECT * FROM (
              SELECT cola,
              FKTABLEA,
              colc,
              lag (cold,1) OVER (ORDER BY cold) AS cold
              FROM tableB
              WHERE FKTABLEA = :1)
              WHERE colc != cold
              order by cola' using Curs.PKID;

              DBMS_OUTPUT.PUT_LINE('OUTPUT ALL RESULTS FROM THE QUERY HERE');
          END LOOP;     
    END;

1 个答案:

答案 0 :(得分:4)

无需使用EXECUTE IMMEDIATE。并且只有完全手动的方式来打印所有结果:

DECLARE
    CURSOR Curs IS SELECT DISTINCT PKID FROM TABLEA;
BEGIN
    FOR rec IN Curs LOOP
        FOR r IN (
            SELECT * FROM (
                SELECT cola,
                    FKTABLEA,
                    colc,
                    lag (cold,1) OVER (ORDER BY cold) AS cold
                FROM tableB
                WHERE FKTABLEA = rec.PKID)
            WHERE colc != cold
            order by cola )
        LOOP
            DBMS_OUTPUT.PUT_LINE(r.cola || ',' || r.colb || ',' || r.colc || ',' || r.cold);
        END LOOP;
    END LOOP;     
END;