陋室铭
永远也不要停下学习的脚步(大道至简至易)

posts - 2169,comments - 570,views - 413万
下面就是例子程序

 --明细表打印予处理  通用报表:
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);             --存储查询语句
  sql3 varchar2(500);             --存储查询条件
  str1 sc_print_syn.a%type;   --存储车间进程
  str2 sc_print_syn.b%type;   --存储班组(工艺、工序)进程
  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;
 /*

注意上面红色的两行和蓝色的两行

红色的两行定义一个游标

蓝色的两行定义一个游标中将要返回的数据的数据结构

*/
      
    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;
     --下面开始构造查询语句
      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;

--打开动态游标,再往下就都一样了
      open c1 for sql2;
        loop
            fetch c1 into r_c1;
            exit when c1%notfound;
            str1:='';
            str2:='';
            --打开工序表进行处理
            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;

当然,实现的方法一定很多,甚至可以用隐式游标。但是隐式游标中用动态查询语句也要费一些周折的。

posted on   宏宇  阅读(2028)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
历史上的今天:
2006-11-28 什么是Web Service
2006-11-28 实现treeview的动态加载
< 2007年11月 >
28 29 30 31 1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 1
2 3 4 5 6 7 8

点击右上角即可分享
微信分享提示