---
title: "SQLSERVER存储过程改为DB2存储过程"
author: "lumans"
date: "2009-11-21T23:38:22Z"
tags: ["DB2"]
slug: "sqlserver-db2"
draft: false
lastmod: "2009-11-21T23:38:22Z"
id: 522423
url: "https://lumans.iteye.com/blog/522423"
---

CREATE   PROCEDURE   sp抄表   (@jsrq   nvarchar(50))   AS    
  declare   @dybh   nvarchar(50),@bmc   nvarchar(50),@scds   money,@bcds   money,@xd   money,@cbzzrq   nvarchar(50),@jfbz   bit,@gq   bit    
  declare   @ycbbz   bit,@bl   float,@lc   tinyint   ,@bz   nvarchar   (50),@bh   nvarchar   (50),@fb   nvarchar   (50),@zdyl   int,@zgyl   int    
  declare   mycunit   cursor   for   select   单元编号,表名称,上次读数,本次读数,行度,抄表终止日期,计费标志,挂起,已抄表标志,倍率,量程,备注,编号,父表,最低用量,最高用量   from   单元表抄表   where   抄表终止日期='&lt;当前抄表期间&gt;'    
  declare   mycpub   cursor   for   select   表名称,上次读数,本次读数,行度,抄表终止日期,计费标志,挂起,已抄表标志,倍率,量程,备注   from   公用表抄表   where   抄表终止日期='&lt;当前抄表期间&gt;'    
     
  open   mycunit    
  fetch   next   from   mycunit   into   @dybh,@bmc,@scds,@bcds,@xd,@cbzzrq,@jfbz,@gq,@ycbbz,@bl,@lc,@bz,@bh,@fb,@zdyl,@zgyl    
  while   @@fetch\_status=0    
  begin    
  insert   into   单元表抄表   (单元编号,表名称,上次读数,本次读数,行度,抄表终止日期,计费标志,挂起,已抄表标志,倍率,量程,备注,编号,父表,最低用量,最高用量)      
  values(@dybh,@bmc,@scds,@bcds,@xd,@jsrq,0,0,@ycbbz,@bl,@lc,@bz,@bh,@fb,@zdyl,@zgyl)      
  fetch   next   from   mycunit   into   @dybh,@bmc,@scds,@bcds,@xd,@cbzzrq,@jfbz,@gq,@ycbbz,@bl,@lc,@bz,@bh,@fb,@zdyl,@zgyl    
  end    
  close   mycunit    
  deallocate   mycunit    
     
  open   mycpub    
  fetch   next   from   mycpub   into   @bmc,@scds,@bcds,@xd,@cbzzrq,@jfbz,@gq,@ycbbz,@bl,@lc,@bz    
  while   @@fetch\_status=0    
  begin    
  insert   into   公用表抄表   (表名称,上次读数,本次读数,行度,抄表终止日期,计费标志,挂起,已抄表标志,倍率,量程,备注)      
  values(@bmc,@scds,@bcds,@xd,@jsrq,0,0,@ycbbz,@bl,@lc,@bz)      
  fetch   next   from   mycpub   into   @bmc,@scds,@bcds,@xd,@cbzzrq,@jfbz,@gq,@ycbbz,@bl,@lc,@bz    
  end    
  close   mycpub    
  deallocate   mycpub    
     
  update   单元表抄表   set   上次读数=本次读数,行度=0   where   抄表终止日期='&lt;当前抄表期间&gt;'    
  update   公用表抄表   set   上次读数=本次读数,行度=0   where   抄表终止日期='&lt;当前抄表期间&gt;'     
 

**DB2**

CREATE   PROCEDURE   sp抄表   (in   jsrq   nvarchar(50))      
  begin    
            declare   dybh   nvarchar(50);    
            declare   bmc   nvarchar(50);    
            declare   scds   money;    
            declare   bcds   money;    
            declare   xd   money;    
            declare   cbzzrq   nvarchar(50);    
            declare   jfbz   bit;    
            declare   gq   bit;    
            declare   ycbbz   bit;    
            declare   bl   float;    
            declare   lc   tinyint;    
            declare   bz   nvarchar(50);    
            declare   bh   nvarchar(50);    
            declare   fb   nvarchar(50);    
            declare   zdyl   int;    
            declare   zgyl   int;    
            declare   rcount   int;    
            declare   i   int;    
            declare   mycunit   cursor   for   select   单元编号,表名称,上次读数,本次读数,行度,抄表终止日期,计费标志,挂起,已抄表标志,倍率,量程,备注,编号,父表,最低用量,最高用量   from   单元表抄表   where   抄表终止日期='&lt;当前抄表期间&gt;';    
            declare   mycpub   cursor   for   select   表名称,上次读数,本次读数,行度,抄表终止日期,计费标志,挂起,已抄表标志,倍率,量程,备注   from   公用表抄表   where   抄表终止日期='&lt;当前抄表期间&gt;';    
     
            set   i=1;    
            select   count(\*)   into   rcount   from   单元表抄表   where   抄表终止日期='&lt;当前抄表期间&gt;';    
            open   mycunit;    
            while   i&lt;=rcount   do              
  insert   into   单元表抄表   (单元编号,表名称,上次读数,本次读数,行度,抄表终止日期,计费标志,挂起,已抄表标志,倍率,量程,备注,编号,父表,最低用量,最高用量)   values(dybh,bmc,scds,bcds,xd,jsrq,0,0,ycbbz,bl,lc,bz,bh,fb,zdyl,zgyl);      
                  fetch   mycunit   into   dybh,bmc,scds,bcds,xd,cbzzrq,jfbz,gq,ycbbz,bl,lc,bz,bh,fb,zdyl,zgyl;    
                  set   i=i+1;    
            end   while;    
            close   mycunit;    
     
            set   i=1;    
            select   count(\*)   into   rcount   from   公用表抄表   where   抄表终止日期='&lt;当前抄表期间&gt;';    
            open   mycpub;    
            while   i&lt;=rcount   do    
  insert   into   公用表抄表   (表名称,上次读数,本次读数,行度,抄表终止日期,计费标志,挂起,已抄表标志,倍率,量程,备注)   values(bmc,scds,bcds,xd,jsrq,0,0,ycbbz,bl,lc,bz);      
  fetch   mycpub   into   bmc,scds,bcds,xd,cbzzrq,jfbz,gq,ycbbz,bl,lc,bz;    
                  set   i=i+1;    
          end   while;    
          close   mycpub;    
     
          update   单元表抄表   set   上次读数=本次读数,行度=0   where   抄表终止日期='&lt;当前抄表期间&gt;';    
          update   公用表抄表   set   上次读数=本次读数,行度=0   where   抄表终止日期='&lt;当前抄表期间&gt;';    
  end