PL/SQL - dbms output the result of an execute immediate

≯℡__Kan透↙ 提交于 2019-12-24 07:18:56

问题


I would like to be able to print out all results for a query (that should be filtered by the PK from TableA) and do this for each PK in TABLEA. This is what i have so far:

 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:


There is no need to use EXECUTE IMMEDIATE. And there's only the fully manual way of printing all the results:

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;


来源:https://stackoverflow.com/questions/6166513/pl-sql-dbms-output-the-result-of-an-execute-immediate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!