在plsql中新建了一个type 里面有一个只有一个参数的构造函数 外面还声明了一个变量,编译成功,但是在存储过程中调用的时候却报00307错误type代码如下:
create or replace type TYP_LEAD as object
  (
    -- Author  : ADMINISTRATOR
    -- Created : 2011-10-10 下午 10:04:01
    -- Purpose : 
    
    -- Attributes
    m_strLead varchar2(16),
    -- m_strLeadName varchar2(10),
    
    constructor function TYP_LD(p_strLeadCd in varchar2) return self as result,
    
    member function IsExist return boolean  )create or replace type body TYP_LEAD is
  
constructor function TYP_LD(p_strLeadCd in varchar2) return self as result is
begin
  begin
    select sb.物件cd
    into m_strLead
    from SB物件 sb
    where sb.物件cd = p_strLeadCd;
    exception 
      when no_data_found then
        m_strLead := '';
    end;
    return;
end;member function IsExist return boolean is
begin
  if m_strLead is null then
    return false;
  else 
    return true;
  end if;
  end;
  
end;
调用的存储过程
create or replace procedure TEST_TYP_LEAD is
  targetPLSQL TYP_LEAD;
  bolIsExist boolean;
  
  begin
    targetPLSQL := TYP_LEAD('001');    bolIsExist := targetPLSQL.IsExist();
    
    if bolIsExist = true then
      dbms_output.put_line('1');
    else
        dbms_output.put_line('0');
    end if;    
    -- dbms_output.put_line(bolIsExist);
end TEST_TYP_LEAD;type编译成功,但是用上面的存储过程调用type的时候,总是报一个pls0307:有太多声明和“TYP_LD”相同,
如果在type中多声明一个变量(任意的一个如strLeadName )就能正常,即变量个数和构造函数中的参数个数相同时,在存储过程中调用的时候就报错
请大侠指点指点

解决方案 »

  1.   

    targetPLSQL TYP_LEAD;
    targetPLSQL := TYP_LEAD('001');你这样写是不是有问题?
      

  2.   

    存储过程不知道你调用的TYP_LEAD('001')是哪个?
    也就是说不识别你写的TYP_LEAD('001'),究竟是想调用type里的变量 还是 type里的construction function,因为从你的type和type body的定义来看,调用变量和function的调用方法是一样的,都可以通过TYP_LEAD('001')来调用。(这里的TYP_LEAD既可以表示type名,也可以表示type里的方法名)
      

  3.   

    以下是oracle文档关于Constructor Methods的描述,Constructor Methods
    Every object type has a constructor method (constructor for short), which is a function with the same name as the object type that initializes and returns a new instance of that object type.Oracle generates a default constructor for every object type, with formal parameters that match the attributes of the object type. That is, the parameters and attributes are declared in the same order and have the same names and datatypes.
    You can define your own constructor methods, either overriding a system-defined constructor, or defining a new function with a different signature.PL/SQL never calls a constructor implicitly, so you must call it explicitly.For more information, see "Defining Constructors".大概说的是每个object 类型都有个默认的构造函数,跟声明的属性是匹配的;你这个object只有一个参数时,默认的构造函数和你后来的构造函数是一样的,也就是说有两个一样的构造函数,当然有报错,你加个参数之后,默认的构造函数也变了,oracle就知道你想调那个构造函数了。
      

  4.   

    在存储过程中调用的就是type的构造函数,不好意思,是我代码贴错了
    plsql中新建了一个type 里面有一个只有一个参数的构造函数 外面还声明了一个变量,编译成功,但是在存储过程中调用的时候却报00307错误type代码如下:
    create or replace type TYP_LEAD as object
      (
      -- Author : ADMINISTRATOR
      -- Created : 2011-10-10 下午 10:04:01
      -- Purpose :  
        
      -- Attributes
      m_strLead varchar2(16),
      -- m_strLeadName varchar2(10),
        
      constructor function TYP_LEAD(p_strLeadCd in varchar2) return self as result,
        
      member function IsExist return boolean  )create or replace type body TYP_LEAD is
       
    constructor function TYP_LEAD(p_strLeadCd in varchar2) return self as result is
    begin
      begin
      select sb.物件cd
      into m_strLead
      from SB物件 sb
      where sb.物件cd = p_strLeadCd;
      exception  
      when no_data_found then
      m_strLead := '';
      end;
      return;
    end;member function IsExist return boolean is
    begin
      if m_strLead is null then
      return false;
      else  
      return true;
      end if;
      end;
       
    end;
    调用的存储过程
    create or replace procedure TEST_TYP_LEAD is
      targetPLSQL TYP_LEAD;
      bolIsExist boolean;
       
      begin
      targetPLSQL := TYP_LEAD('001');  bolIsExist := targetPLSQL.IsExist();
        
      if bolIsExist = true then
      dbms_output.put_line('1');
      else
      dbms_output.put_line('0');
      end if;   
      -- dbms_output.put_line(bolIsExist);
    end TEST_TYP_LEAD;type编译成功,但是用上面的存储过程调用type的时候,总是报一个pls0307:有太多声明和“TYP_LEAD”相同,
    如果在type中多声明一个变量(任意的一个如strLeadName )就能正常,即变量个数和构造函数中的参数个数相同时,在存储过程中调用的时候就报错
    请大侠指点指点
      

  5.   

    你怎么还是不明白呢?PLSQL默认的type的构造方法就是根据你声明的变量个数的,不管你有没有  constructor function TYP_LEAD(p_strLeadCd in varchar2) return self as result这个构造方法,那个默认的都在。所以说是两个构造方法。你新声明的如果与构造的参数个数一样,就报错了。