create or replace procedure p_complte_d_rank(filterStr varchar2 , p_period varchar2) is
  type c_total is ref cursor;
  v_total c_total;   -- 游标记录每行值
  v_unitid SGCC_FACT_COMPLETE_D.unit_id%type;  -- 单位ID
  v_total_value number; -- 总得分
  v_rank number ;  -- 排名
beginopen v_total for 
     select unit_id from sgcc_fact_complete_d
         where sj_type= p_period and unit_id = filterStr;  --  这样写就没有值循环
         
-- select unit_id from sgcc_fact_complete_d
--         where sj_type= '201104' and unit_id = '501101010000009112';  这样写就有值循环
    LOOP
       fetch v_total into v_unitid ; 
       exit when v_total%notfound;
       
       dbms_output.put_line(v_unitid);
     end LOOP ;
end p_complte_d_rank;我来描述下问题, 我的输入参数是 '201104' ,'501101010000009112' , 如果我用输入参数来代替 p_period , filterStr的值, 就没有值循环, 如果我直接把时间 , 和单位编号写固定, 就有值可以进行循环, 请问各位大神, 这是什么原因造成的,  小弟不才,  在线等 。。

解决方案 »

  1.   

    --将游标查询放在定义部分试试
    open v_total for 
         select unit_id from sgcc_fact_complete_d
             where sj_type= p_period and unit_id = filterStr;
    begin
    ...
      

  2.   


     按照你的意思, 我改成如下, 还是不行呢, 没有值进行循环create or replace procedure p_complte_d_rank(filterStr varchar2 , p_period varchar2) is
      v_unitid SGCC_FACT_COMPLETE_D.unit_id%type;  -- 单位ID
      v_total_value number; -- 总得分
      v_rank number ;  -- 排名
      cursor c_total is select unit_id from sgcc_fact_complete_d
             where sj_type= p_period and unit_id = filterStr;
    begin
     for data in c_total
     loop
       dbms_output.put_line('111');
     end loop;    
    end p_complte_d_rank;
      

  3.   

    --改成这样
    CREATE OR REPLACE PROCEDURE p_complte_d_rank(filterStr VARCHAR2,
                                                 p_period  VARCHAR2) IS
      TYPE c_total IS REF CURSOR;
      v_total       c_total; -- 游标记录每行值
      v_unitid      SGCC_FACT_COMPLETE_D.unit_id%TYPE; -- 单位ID
      v_total_value NUMBER; -- 总得分
      v_rank        NUMBER; -- 排名
    BEGIN  OPEN v_total FOR 'SELECT unit_id
          FROM sgcc_fact_complete_d
         WHERE sj_type = ''' || p_period || ''' AND unit_id = ''' || filterStr || '''';
      LOOP
        FETCH v_total
          INTO v_unitid;
        EXIT WHEN v_total%NOTFOUND;
      
        dbms_output.put_line(v_unitid);
      END LOOP;
    END p_complte_d_rank;
      

  4.   

    回复楼上的, 确实单引号的问题, 我刚才也成功试出来了, 但是有个问题, 如果 filterStr 这个字段是 多个值组成的, 例如 501101010000009112,501101010000009113,501101010000009111 , 那这个单 引号要怎么写? 我修改成如下了
    open v_total for 
         select unit_id from sgcc_fact_complete_d
             where sj_type= ''||p_period||'' and unit_id = ''||filterStr ||'';
    多个值的话。
    open v_total for 
         select unit_id from sgcc_fact_complete_d
             where sj_type= ''||p_period||'' and unit_id in (这里要怎么用单引号
    );
      

  5.   

    open v_total for 
      select unit_id from sgcc_fact_complete_d
      where sj_type= ''||p_period||'' and substr(',' || filterStr || ',',',' || unit_id || ',') > 0
    ;
    如果用in就得动态sql了.
      

  6.   

      我的 sql 非常长啊, 如果用 动态 拼 sql 的 话, 那拼到明年去了。
      

  7.   

    我写的那个没用动态sql,你没懂哦,方法写错了,改下
    open v_total for 
      select unit_id from sgcc_fact_complete_d
      where sj_type= ''||p_period||'' and instr(',' || filterStr || ',',',' || unit_id || ',') > 0
    ;
      

  8.   

    open v_total for  
      select unit_id from sgcc_fact_complete_d
      where sj_type= "||p_period||" and unit_id in ("|| REPLACE( '||filterStr||',',','","' ))
    );这个尝试一下
      

  9.   

    11 楼, 我像你那 open 游标, 报错了。
      

  10.   

    你使用in的方式:
       select * from where unit_id in (501101010000009112,501101010000009113,501101010000009111)
       
    等价于:
       select * from where and instr(',501101010000009112,501101010000009113,501101010000009111,',',' || unit_id || ',') > 0 
    意思是字符串',501101010000009112,501101010000009113,501101010000009111,'中包含',unit_id,',加逗号是为了确定你传递参数中查询的条件,比如:',501101010000009112,'表示501101010000009112是你的检索条件。