简体   繁体   中英

how to write pl/sql procedure package body with more cursors and out them as varchar2

I'm new to oracle pl/sql. I want to write code to get table space info and database lock status and i want to output them as varchar2. My code gives me error

** Errors for PACKAGE BODY FINAL_PACKAGE: i run this as **SYS AS SYSDBA

LINE/COL ERROR
11/13    PLS-00103: Encountered the symbol "SELECT" when expecting one of
         the following:
         ( ; is return 

line 11 is

11 CURSOR tbsp select a.TABLESPACE_NAME as

code is,

CREATE OR REPLACE PACKAGE final_package as
     PROCEDURE final_procedure(var1 in varchar2, dbinfo out varchar2);
 END final_package;                                         
/


CREATE OR REPLACE PACKAGE BODY final_package IS

       PROCEDURE final_procedure(var1 in varchar2, dbinfo out varchar2) IS

        BEGIN
          IF var1 = 'a'
         ------  /* get tablespaces name, percentage */ ----
         THEN 
         DECLARE
            tsinfo varchar(500); ---- /* i want to put tablespaces result to this tsinfo */----

            CURSOR tbsp select a.TABLESPACE_NAME as
              Tablespace,round((1-((a.BYTES-nvl(b.BYTES,0))/a.BYTES))*100,2)
              AS Percentages from (select TABLESPACE_NAME, sum(BYTES) BYTES from
              sys.dba_data_files group by TABLESPACE_NAME) a,
              (select TABLESPACE_NAME, sum(BYTES) BYTES from sys.dba_free_space
                group by TABLESPACE_NAME) b
               where a.TABLESPACE_NAME = b.TABLESPACE_NAME (+)
              order by ((a.BYTES-b.BYTES)/a.BYTES) desc;

        BEGIN

            FOR each_data in tbsp
             LOOP
                 FETCH tbsp INTO tsinfo; --- /* i want to put tablespaces result to this tsinfo */ ---
             END LOOP;
            CLOSE tbsp;

          ----   /* get database lock status */ ----
         ELSIF var1 = 'b' THEN
             DECLARE lockinfo varchar(1500);
             CURSOR lock_info  SELECT vh.sid locking_sid,
                                vw.sid waiter_sid,
                                vs.status status,
                                vs.program program_holding,
                                vsw.program program_waiting
                               FROM v$lock vh, v$lock vw,
                                    v$session vs, v$session vsw
                               WHERE(vh.id1, vh.id2) IN 
                                    (SELECT id1, id2
                                       FROM v$lock
                                      WHERE request = 0
                                    INTERSECT 
                                     SELECT id1, id2
                                      FROM v$lock
                                      WHERE lmode = 0)
                                     AND vh.id1 = vw.id1
                                     AND vh.id2 = vw.id2
                                     AND vh.request = 0
                                     AND vw.lmode = 0
                                     AND vh.sid = vs.sid
                                     AND vw.sid = vsw.sid;

        BEGIN

            FOR each_data in lockinfo
                LOOP
             FETCH lock_info INTO lockinfo; -- i want to put database lock  result to this tsinfo
                END LOOP;
             CLOSE lock_info;

     END IF;   
  END;
 END;    
 /

change to

CURSOR tbsp is select a.TABLESPACE_NAME as

By the way that you write I assume you have SQL Server background. I made some fixes to your code. Have in mind that even though you loop through cursor you still return only a one, latest value. To change it you have to use collections ie: nested tables, associative arrays. Your code will still not work because in the cursor you select more than one column and you try to assign it to variable that holds a single value.

Try to use collections and post your answer.

The code:

CREATE OR REPLACE PACKAGE final_package IS

     PROCEDURE final_procedure(var1 IN VARCHAR2, dbinfo OUT VARCHAR2);

END final_package;



CREATE OR REPLACE PACKAGE BODY final_package IS

    PROCEDURE final_procedure(var1 IN VARCHAR2, dbinfo OUT VARCHAR2)
    IS
        -- declare variables
        tsinfo   varchar(500);
        lockinfo varchar(1500);

        -- declare cursors
        CURSOR tbsp IS
            select a.TABLESPACE_NAME as
            "TABLESPACE",round((1-((a.BYTES-nvl(b.BYTES,0))/a.BYTES))*100,2)
            AS Percentages from (select TABLESPACE_NAME, sum(BYTES) BYTES from
            sys.dba_data_files group by TABLESPACE_NAME) a,
            (select TABLESPACE_NAME, sum(BYTES) BYTES from sys.dba_free_space
            group by TABLESPACE_NAME) b
            where a.TABLESPACE_NAME = b.TABLESPACE_NAME (+)
            order by ((a.BYTES-b.BYTES)/a.BYTES) desc;

        CURSOR lock_info IS
            SELECT vh.sid locking_sid,
            vw.sid waiter_sid,
            vs.status status,
            vs.program program_holding,
            vsw.program program_waiting
            FROM v$lock vh, v$lock vw,
            v$session vs, v$session vsw
            WHERE(vh.id1, vh.id2) IN 
            (SELECT id1, id2
            FROM v$lock
            WHERE request = 0
            INTERSECT 
            SELECT id1, id2
            FROM v$lock
            WHERE lmode = 0)
            AND vh.id1 = vw.id1
            AND vh.id2 = vw.id2
            AND vh.request = 0
            AND vw.lmode = 0
            AND vh.sid = vs.sid
            AND vw.sid = vsw.sid;
    BEGIN
        IF var1 = 'a' THEN
            -- open, loop and close
            FOR each_data in tbsp
            LOOP
                FETCH tbsp INTO tsinfo;
            END LOOP;
        ELSIF var1 = 'b' THEN
            -- open, loop and close
            FOR each_data in lockinfo
            LOOP
                FETCH lock_info INTO lockinfo;
            END LOOP;
        END IF;
    END final_procedure;

END final_package;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM