CREATE OR REPLACE FUNCTION f_get_satff_id(org_type integer,org_id varchar2)
RETURN nvarchar2 
as
  s_staff nvarchar2(32767);
  nCnt INTEGER;   --循环次数
  TYPE CURSORTYPE IS REF CURSOR;
  cCURSOR CURSORTYPE;      --游标
  staffid VARCHAR2(20);
begin
  if org_type=2 then
     select count(r.staff_id) into nCnt from SSO_STAFF_ROLE r,hrm_staff s,hrm_group_info g where r.staff_id=s.staff_id and s.group_id=g.group_id and sso_role_id=70 and g.department_id in ('|| org_id ||');
     if nCnt > 0 then
       open cCURSOR for
       select r.staff_id from SSO_STAFF_ROLE r,hrm_staff s,hrm_group_info g where r.staff_id=s.staff_id and s.group_id=g.group_id and sso_role_id=70 and g.department_id in ('|| org_id ||');
       loop 
        FETCH cCURSOR INTO staffid;
           EXIT WHEN cCURSOR%NOTFOUND;
           s_staff :=  s_staff ||staffid ||',';
       end loop;
     else
       s_staff:='部门';
     end if;
     --s_staff:='部门';
  elsif org_type=1 then
     s_staff:='班组';
  elsif org_type=0 then
     s_staff:='员工';
  end if;  
     
  RETURN s_staff;
END;调用函数时报错“无效数字”,我把'|| org_id ||'替换成1,2就没问题,应该是传入的参数问题,org_id参数传入的是1,2等字符串,请问为什么调用函数会出错

解决方案 »

  1.   

    你动态sql格式错了。没法像你这样用。
      

  2.   

    if org_type=2 then 
    v_sql:='select ...'||...;
    execute immediate v_sql into nCnt;
    ...
      

  3.   

    select count(r.staff_id) into nCnt from SSO_STAFF_ROLE r,hrm_staff s,hrm_group_info g where r.staff_id=s.staff_id and s.group_id=g.group_id and sso_role_id=70 and g.department_id in ('|| org_id ||'); 直接写
    select count(r.staff_id) into nCnt from SSO_STAFF_ROLE r,hrm_staff s,hrm_group_info g where r.staff_id=s.staff_id and s.group_id=g.group_id and sso_role_id=70 and g.department_id in (org_id ); 
    就可以了后面那个也是
      

  4.   

    啊,引号是将||org_id||括在里面,必然出错了
    g.department_id in ('|| org_id ||'); 换成
    g.department_id =org_id 不行吗
      

  5.   

    v_sql:='select count(r.staff_id) from SSO_STAFF_ROLE r,hrm_staff s,hrm_group_info g where r.staff_id=s.staff_id and s.group_id=g.group_id and sso_role_id=70 and g.department_id in ('|| org_id ||')';
         execute immediate v_sql into nCnt; 
    上面的这样改了没报错了,但是这句怎么改
    open cCURSOR for
           select r.staff_id from SSO_STAFF_ROLE r,hrm_staff s,hrm_group_info g where r.staff_id=s.staff_id and s.group_id=g.group_id and sso_role_id=70 and g.department_id in ('|| org_id ||');
           loop 
      

  6.   

    v_sql := 'select r.staff_id from SSO_STAFF_ROLE r,hrm_staff s,hrm_group_info g where r.staff_id=s.staff_id and s.group_id=g.group_id and sso_role_id=70 and g.department_id in ('|| org_id ||')'; 
    open cCURSOR for v_sql
    loop 
      

  7.   

    CREATE OR REPLACE FUNCTION f_get_satff_id(org_type integer,org_id varchar2) 
    RETURN nvarchar2 
    as 
       s_staff nvarchar2(32767); 
       nCnt INTEGER;  --循环次数 
       TYPE CURSORTYPE IS REF CURSOR; 
       cCURSOR CURSORTYPE;      --游标 
       staffid VARCHAR2(20); 
       v_sql  VARCHAR2(2000);
    begin 
       if org_type=2 then 
          v_sql := 'select count(r.staff_id) 
                    from SSO_STAFF_ROLE r,hrm_staff s,hrm_group_info g 
                    where r.staff_id=s.staff_id and s.group_id=g.group_id 
                    and sso_role_id=70 and g.department_id in ('|| org_id ||')'; 
          execute immediate v_sql into nCnt; 
          if nCnt > 0 then 
             v_sql := 'select r.staff_id 
                       from SSO_STAFF_ROLE r,hrm_staff s,hrm_group_info g 
                       where r.staff_id=s.staff_id and s.group_id=g.group_id 
                       and sso_role_id=70 and g.department_id in ('|| org_id ||')';
             open cCURSOR for v_sql;
             loop 
                FETCH cCURSOR INTO staffid; 
                EXIT WHEN cCURSOR%NOTFOUND; 
                s_staff := s_staff ||staffid ||','; 
             end loop; 
          else 
             s_staff:='部门'; 
          end if; 
        --s_staff:='部门'; 
       elsif org_type=1 then 
          s_staff:='班组'; 
       elsif org_type=0 then 
          s_staff:='员工'; 
       end if;    RETURN s_staff; 
    END;
      

  8.   

    open cCURSOR for 
          select r.staff_id from SSO_STAFF_ROLE r,hrm_staff s,hrm_group_info g where r.staff_id=s.staff_id and s.group_id=g.group_id and sso_role_id=70 and g.department_id =to_number(org_id) ;
          loop 干嘛那么麻烦,这样不行吗
      

  9.   

    #11通过,open cCURSOR for v_sql;有分号