隨筆-314  評論-209  文章-0  trackbacks-0

          下面就是例子程序

           --明細(xì)表打印予處理  通用報(bào)表:
          procedure mx_print_common(pd_id in mx_pd_syn.pd_id%type,
                             p_pd_mxb_id IN mx_pd_mxb_syn.p_mxb_id%type,
                             p_dept_no IN sc_mxk.dept_code%type,
                             p1 sc_bz_syn.bz_code%type,
                             p2 sc_cjjc_syn.cjjc_code%type,
                             p3 sc_mxk.warehouse_num%type)
          is
            sql2 varchar2(500);             --存儲(chǔ)查詢語句
            sql3 varchar2(500);             --存儲(chǔ)查詢條件
            str1 sc_print_syn.a%type;   --存儲(chǔ)車間進(jìn)程
            str2 sc_print_syn.b%type;   --存儲(chǔ)班組(工藝、工序)進(jìn)程
            s_ip sc_print_syn.ip%type;
            type cursor_type is ref cursor;
            c1 cursor_type;
            type record_type is record(
                  pbom_id sc_mxk.pbom_id%type
            );
            r_c1 record_type;
           /*

          注意上面紅色的兩行和藍(lán)色的兩行

          紅色的兩行定義一個(gè)游標(biāo)

          藍(lán)色的兩行定義一個(gè)游標(biāo)中將要返回的數(shù)據(jù)的數(shù)據(jù)結(jié)構(gòu)

          */
                
              cursor c2(p_pbom_id sc_mxk.pbom_id%type) is
                  select a.dd_count,b.gx_name,c.bz_name,d.cjjc_name
                   from sc_p_gx_syn a,sc_gx_syn b,sc_bz_syn c,sc_cjjc_syn d
                    where pbom_id = p_pbom_id
                    and a.gx_code=b.gx_code(+) and b.dept_code=p_dept_no
                    and a.bz_code=c.bz_code(+)  and b.dept_code=p_dept_no
                    and a.cjjc_code=d.cjjc_code(+)  and b.dept_code=p_dept_no;
             
              r_c2 c2%rowtype;
          BEGIN
                s_ip :=sys_context('USERENV','IP_ADDRESS');
                delete from sc_print_syn where ip=s_ip and p_id=pd_id;
                commit;
               --下面開始構(gòu)造查詢語句
                sql2:='select distinct a.pbom_id from sc_mxk a';
                sql3:=' where a.p_id=' || pd_id || ' and a.dept_code= ''' || p_dept_no || '''';
            
                if  p_pd_mxb_id >0 then
                   sql2:=sql3 || ',mxk c ';
                   sql3:=sql3 || ' and c.m_mxb_id= ' || p_pd_mxb_id || ' and a.mxb_id = c.mxb_id';
                end if;
               
                if p1 is not null then
                   sql2:=sql2 || ',sc_p_gx_syn b';
                   sql3:=sql3 || ' and a.pbom_id=b.pbom_id  and b.bz_code = ''' || p1 || '''';
                end if;
                if p2 is not null then
                   sql2:=sql2 || ',sc_p_gx_syn b';
                   sql3:=sql3 || ' and a.pbom_id=b.pbom_id  and b.cjjc_code = '''  || p2 || '''';
                end if;
                if p3 is not null then
                   sql3:=sql3 || ' and a.warehouse_num = ''' || p3 || '''';
                end if;
                sql2:=sql2 || sql3;

          --打開動(dòng)態(tài)游標(biāo),再往下就都一樣了
                open c1 for sql2;
                  loop
                      fetch c1 into r_c1;
                      exit when c1%notfound;
                      str1:='';
                      str2:='';
                      --打開工序表進(jìn)行處理
                      open c2(r_c1.pbom_id);
                      loop              
                          fetch c2 into r_c2;
                          exit when c2%notfound; --沒有記錄退出
                          if r_c2.cjjc_name is not null then
                             str1 :=str1 || to_char(r_c2.cjjc_name);
                          end if;
                          if r_c2.bz_name is not null then
                             str2 := str2  || r_c2.bz_name  ||  to_char(r_c2.dd_count);
                          elsif r_c2.gx_name is not null then
                             str2 := str2  || to_char(r_c2.gx_name)  ||  to_char(r_c2.dd_count);
                          end if;
                 
                          
                      end loop;
                      close c2;
                      insert into sc_print_syn(a,b,ip,p_id,r_id)
                         values(str1,str2,s_ip,pd_id,r_c1.pbom_id);
                            COMMIT;
                  end loop;
                  close c1;
          END mx_print_common;

          當(dāng)然,實(shí)現(xiàn)的方法一定很多,甚至可以用隱式游標(biāo)。但是隱式游標(biāo)中用動(dòng)態(tài)查詢語句也要費(fèi)一些周折的。

             作者:Northsnow
          電子郵件:northsnow@163.com
          blog:http://blog.csdn.net/precipitant

          posted on 2008-05-27 09:17 xzc 閱讀(10545) 評論(3)  編輯  收藏 所屬分類: Oracle

          評論:
          # re: oracle動(dòng)態(tài)游標(biāo)的簡單實(shí)現(xiàn)方法 2008-05-27 09:18 | xzc
          ----定義
          type cursor_type is ref cursor;
          c1 cursor_type;
          ----使用
          --打開動(dòng)態(tài)游標(biāo),再往下就都一樣了
          open c1 for sql2;
          loop
          fetch c1 into r_c1;
          exit when c1%notfound;   回復(fù)  更多評論
            
          # re: oracle動(dòng)態(tài)游標(biāo)的簡單實(shí)現(xiàn)方法 [未登錄] 2008-05-27 11:43 | xzc
          TYPE cursor_type IS REF CURSOR;
          c1 cursor_type;
          --
          OPEN c1 FOR lc_sql;
          LOOP
          FETCH c1
          INTO lc_source_column_pk_value, lc_source_column_npk_value;
          EXIT WHEN c1%NOTFOUND;
          null;
          END LOOP;
          <<ERROREND>>
          CLOSE c1;  回復(fù)  更多評論
            
          # re: oracle動(dòng)態(tài)游標(biāo)的簡單實(shí)現(xiàn)方法 [未登錄] 2008-05-30 19:32 | xzc
          DECLARE
          v_cursor NUMBER;
          v_stat NUMBER;
          v_row NUMBER;
          v_id NUMBER;
          v_no VARCHAR(100);
          v_date DATE;
          v_sql VARCHAR(200);
          s_id NUMBER;
          s_date DATE;
          BEGIN
          s_id := 3000;
          s_date := SYSDATE;
          v_sql := 'SELECT id,qan_no,sample_date FROM "tblno" WHERE id > :sid and sample_date < :sdate';
          v_cursor := dbms_sql.open_cursor; --打開游標(biāo);
          dbms_sql.parse(v_cursor, v_sql, dbms_sql.native); --解析動(dòng)態(tài)SQL語句;
          dbms_sql.bind_variable(v_cursor, ':sid', s_id); --綁定輸入?yún)?shù);
          dbms_sql.bind_variable(v_cursor, ':sdate', s_date);

          dbms_sql.define_column(v_cursor, 1, v_id); --定義列
          dbms_sql.define_column(v_cursor, 2, v_no, 100);
          dbms_sql.define_column(v_cursor, 3, v_date);
          v_stat := dbms_sql.execute(v_cursor); --執(zhí)行動(dòng)態(tài)SQL語句。
          LOOP
          EXIT WHEN dbms_sql.fetch_rows(v_cursor)<=0; --fetch_rows在結(jié)果集中移動(dòng)游標(biāo),如果未抵達(dá)末尾,返回1。
          dbms_sql.column_value(v_cursor, 1, v_id); --將當(dāng)前行的查詢結(jié)果寫入上面定義的列中。
          dbms_sql.column_value(v_cursor, 2, v_no);
          dbms_sql.column_value(v_cursor, 3, v_date);
          dbms_output.put_line(v_id || ';' || v_no || ';' || v_date);
          END LOOP;
          dbms_sql.close_cursor(v_cursor); --關(guān)閉游標(biāo)。
          END;



            回復(fù)  更多評論
            
          主站蜘蛛池模板: 梁山县| 阳高县| 安溪县| 湖州市| 梁山县| 祥云县| 定襄县| 大余县| 沙坪坝区| 石林| 明水县| 敦化市| 石泉县| 清远市| 洪湖市| 大渡口区| 澄城县| 香格里拉县| 广元市| 莲花县| 汝州市| 启东市| 华池县| 夹江县| 安丘市| 阳高县| 汝州市| 宣化县| 翁源县| 崇礼县| 张家界市| 张家港市| 浦县| 吴旗县| 灵石县| 通州市| 蕉岭县| 琼海市| 海南省| 宁乡县| 会同县|