How to declare a cursor after BEGIN?

I want to know if the cursor can be declared after BEGIN.

And how can I export the result of plsql to an Excel worksheet because I need to run this procedure as a job.

CREATE OR REPLACE PROCEDURE masc(v_amsprogramid VARCHAR2) AS

  v_mid VARCHAR2(50);
  v_sid VARCHAR2(50);

  CURSOR c1 IS
    SELECT DISTINCT mid
    FROM table_a WHERE aid = v_aid
    ORDER BY mid;

  BEGIN

    OPEN c1;

    LOOP
      FETCH c1 INTO v_mid;
      EXIT WHEN c1%NOTFOUND;

      DECLARE
        CURSOR c2 IS
          SELECT DISTINCT sid INTO v_sid
          FROM table_b WHERE mid = v_mid;

      BEGIN
        OPEN c2;
        LOOP

          FETCH c1 INTO v_mid;
          EXIT WHEN c1%NOTFOUND;

          dbms_output.PUT_LINE('MID : ' || v_mid);
          dbms_output.PUT_LINE('Sid : ' || v_sid);

        END LOOP;

        CLOSE c2;
      END LOOP;

      CLOSE c1;
  END masc;
+4
source share
4 answers

Perhaps you are looking for this:

create or replace PROCEDURE MASC (V_AMSPROGRAMID VARCHAR2) AS

V_MID VARCHAR2(50);
V_SID VARCHAR2(50);

CURSOR C1 IS
SELECT distinct MID from table_a WHERE AID = V_AID
ORDER BY MID;

CURSOR C2 IS
SELECT DISTINCT SID INTO V_SID FROM table_b WHERE MID = V_MID
ORDER BY MID;

BEGIN    
...

or

create or replace PROCEDURE MASC (V_AMSPROGRAMID VARCHAR2) AS

V_MID VARCHAR2(50);
V_SID VARCHAR2(50);

CURSOR C1 IS
SELECT distinct MID from table_a WHERE AID = V_AID
ORDER BY MID;

CURSOR C2(v in NUMBER) IS
SELECT DISTINCT SID INTO V_SID FROM table_b WHERE MID = v
ORDER BY MID;

BEGIN

OPEN C1;
...
OPEN C2(V_MID);
...
+3
source

I want to know if I can declare a cursor after the start

Not really. But you can use the cursor for the loop instead of declaring an explicit pointer.

For example,

FOR i IN (SELECT distinct MID from table_a WHERE AID = V_AID ORDER BY MID)
LOOP
   <do something>
END LOOP;

, . . PL/SQL, BULK COLLECT.

plsql excel, .

PL/SQL. SPOOL SQL * Plus.

,

sqlplus user/pass@service_name
<required formatting options>

SPOOL /location/myfile.csv
SELECT distinct MID from table_a WHERE AID = V_AID ORDER BY MID;
SPOOL OFF
+2

U

            create or replace PROCEDURE MASC (V_AMSPROGRAMID VARCHAR2) AS

            V_MID VARCHAR2(50);
            V_SID VARCHAR2(50);

            C1 sys_refcursor ;
            c2 sys_refcursor ;

            BEGIN

            OPEN C1 for SELECT distinct MID from table_a WHERE AID = V_AID
            ORDER BY MID;

            LOOP

            FETCH C1 INTO V_MID;
            EXIT WHEN C1%NOTFOUND;


            open C2 for SELECT DISTINCT SID INTO V_SID FROM table_b WHERE MID = V_MID;

            LOOP

            FETCH C1 INTO V_MID;
            EXIT WHEN C1%NOTFOUND;

            DBMS_OUTPUT.PUT_LINE('MID : ' || V_MID);
            DBMS_OUTPUT.PUT_LINE('Sid : ' || V_SID);



            END LOOP;

            CLOSE C2;
            CLOSE C1;
            END LOOP;
0

pl/sql. , !

- :

create or replace procedure masc (p_amsprogramid varchar2)
as
  v_mid varchar2(50);
  v_sid varchar2(50);

  cursor c1 
  is
    select   distinct mid
    from     table_a
    where    aid = p_amsprogramid
    order by mid;

  cursor c2
  is
    select distinct sid
    from   table_b
    where  mid = v_mid;

begin
  open c1;
  loop
    fetch c1 into v_mid;
    exit when c1%notfound;

    open c2;
    loop
      fetch c1 into v_mid;
      exit when c1%notfound;

      dbms_output.put_line('mid : ' || v_mid);
      dbms_output.put_line('sid : ' || v_sid);
    end loop;

    close c2;
  end loop;

  close c1;
end masc;
/

, - , :

create or replace procedure masc (p_amsprogramid varchar2)
as
  cursor c1 
  is
    select   distinct mid
    from     table_a
    where    aid = p_amsprogramid
    order by mid;

  cursor c2
  is
    select distinct sid
    from   table_b
    where  mid = v_mid;

begin
  for rec1 in c1
  loop
    for rec2 in c2
    loop
      dbms_output.put_line('mid : ' || rec1.mid);
      dbms_output.put_line('sid : ' || rec2.sid);
    end loop;
  end loop;
end masc;
/

, . , , , (.. . SQL PL/SQL, , SQL , PL/SQL-).

, NESTED LOOP - -, SQL- , ( , , SQL- !). , , , select. (, , , , , , - , , select , , . !)

, :

create or replace procedure masc (p_amsprogramid varchar2)
as
  cursor c1 
  is
    select   distinct a.mid,
                      b.sid
    from     table_a a
             inner join table_b b on (a.mid = b.mid)
    where    a.aid = p_amsprogramid
    order by mid;

begin
  for rec1 in c1
  loop
    dbms_output.put_line('mid : ' || rec1.mid);
    dbms_output.put_line('sid : ' || rec1.sid);
  end loop;
end masc;
/

, , , !

If you want to write sql query results as a file, you need to use UTL_FILEinstead DBMS_OUTPUT. Keep in mind that the directory on which the file is written must be something that is mounted / displayed on the server on which the database is located. If you write the results as separated by characters, you can easily import this file into Excel.

You may find "> .

0
source

Source: https://habr.com/ru/post/1613456/


All Articles