在select查询中使用时,在输入过程参数中添加单引号 - Oracle

时间:2018-01-10 05:32:10

标签: oracle plsql

我需要在select select中使用的过程中为我的输入变量添加单引号。

 create or replace procedure listobj(objnam varchar)
    is
    cursor c1 is
    select object_id,object_name,tablespace_name,owner from dba_objects where object_name=objnam;
    rec c1%rowtype;
    begin
    for rec in c1 loop
    dbms_output.put_line(rec.object_id||'  '||rec.tablespace_name||'  '||rec.owner);
    end loop;
   end;
   /

1 个答案:

答案 0 :(得分:0)

执行以下操作:

CREATE OR REPLACE PROCEDURE LISTOBJ (
     OBJNAM   VARCHAR2)
IS
     CURSOR C1(OBJ_NM VARCHAR2)
     IS
          SELECT OBJECT_ID,
                 OBJECT_NAME,
                 --TABLESPACE_NAME,
                 OWNER
            FROM ALL_OBJECTS
           WHERE OBJECT_NAME = OBJ_NM ;

     REC                           C1%ROWTYPE;
BEGIN
     --Pass the input variable to cursor like a paramater. No need to add single quotes then
     FOR REC IN C1(OBJNAM)
     LOOP
          DBMS_OUTPUT.PUT_LINE (   REC.OBJECT_ID
                                || '  '                              
                               -- || REC.TABLESPACE_NAME
                                || '  '
                                || REC.OWNER);
     END LOOP;
END;

执行:

 SQL> Exec  LISTOBJ('DEPT');
      51149    SCOTT

      PL/SQL procedure successfully completed.