0

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;    
 /
4

2 回答 2

0

改成

CURSOR tbsp 是选择 a.TABLESPACE_NAME 作为

于 2013-10-24T06:14:28.840 回答
0

顺便说一句,我假设你有 SQL Server 背景。我对您的代码进行了一些修复。请记住,即使您遍历光标,您仍然只返回一个最新的值。要更改它,您必须使用集合,即:嵌套表、关联数组。您的代码仍然无法工作,因为在光标中您选择了多个列,并且您尝试将其分配给包含单个值的变量。

尝试使用集合并发布您的答案。

编码:

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;
于 2013-10-24T08:31:06.493 回答