我的那段存储过程已经编译成功的,就是到下面execute时,说我没有声明变量

解决方案 »

  1.   

    create or replace procedure getclassname(id in varchar2) as 
      classname1 varchar2(20);
    begin
      select classname into classname1 from jxl.class
      where classid = id;
    exception
    when no_data_found then
      raise_application_error(-20100, 'cannot find the id');
    end;
      

  2.   

    楼上所说的可以执行成功,但是classname1的值如何取出呀
    variable classname1 varchar2;
    execute getclassname('1');
    print classname1;
    打印出来的是空值,没有取出来
      

  3.   

    楼主犯了一个微细的错误,变里与字段名不同名,这样oracle只能认作表的字段
    create procedure getclassname(p_id in varchar2, p_classname out varchar2) as 
    begin
      select classname into p_classname from jxl.class
    where classid = p_id;
    dbms_output.put_line(p_classname);
    exception
    when no_data_found then
      raise_application_error(-20100, 'cannot find the id');
    end;
    /
      

  4.   

    to beckhambobo:
    我已经把我的procedure改成和你的一样了,但是还是出现我以前的错误,说必须声明
    variable p_classname varchar2;
    execute getclassid('1', p_classname);
    出错如下:
    ERROR at line: 1:
    ORA-06550: line 1, column 23:
    PLS-00201: identifier 'P_CLASSNAME' must be declared
    ORA-06550: line 1, column7:
    PL/SQL: Statement ignored
      

  5.   

    说了一大堆,就是为了这个绑定变量(bind variable)。
    先看看 Oracle 自带的帮助吧.SQL> help variable VARIABLE
     -------- Declares a bind variable that can then be referenced in PL/SQL.
     VARIABLE with no arguments displays a list of all variables declared
     in the session. VARIABLE followed by a name lists that variable. VAR[IABLE] [variable [NUMBER | CHAR | CHAR (n) | NCHAR | NCHAR (n) |
       VARCHAR2 (n) | NVARCHAR2 (n) | CLOB | NCLOB | REFCURSOR]]
    =========================================================================
    以上是定义的方式.
    至于调用嘛,这样写一个简单的例子你就明白了...SQL>variable strOut varchar2(20);  -- 定义 strOut 变量
    SQL>CREATE OR REPLACE PROCEDURE sp_TestVar(str_id OUT VARCHAR2)
    AS
    BEGIN
      str_id := 'This is Test.';
    end; -- 建立过程SQL>exec sp_TestVar(:strOut); -- 调用过程,内容会输出到 strOut.
    SQL>print strOut; -- 看看会是什么内容.(还是空的话,找我要 :-),呵呵...  )
      

  6.   

    建义楼主查看定义变量语法,variable p_classname varchar2; --这句话没有定义长度(字符类型)
    declare
    v_classname varchar2(10);
    begin
    getclassid('1', v_classname);
    dbms_output.put_line(v_classname);
    end;
    /
      

  7.   

    create or replace procedure getclassname(id in varchar2) as 
      classname1 varchar2(20);
    begin
      select classname into classname1 from jxl.class
      where classid = id;
      dbms_output.put_line(classname1);      此句可在屏幕中打印出来。
    exception
    when no_data_found then
      raise_application_error(-20100, 'cannot find the id');
    end;