lei's profile我就是我PhotosBlogListsMore Tools Help
    September 13

    再加一篇DB2的sql文,这东西资料太少到要自己摸

    CREATE PROCEDURE P_SE_FPollutantDischarge(in v_fdepc varchar(30), in v_date varchar(10))--电厂编码,上报日期
    --科环基层上报集团数据(污染物排放情况)
         LANGUAGE SQL    
       p1:BEGIN
        DECLARE FZJG VARCHAR(20); --分支机构编码
        DECLARE JZ VARCHAR(20); --机组编码
        DECLARE CZ_TYPE int ; --存在标志
        DECLARE COLNAME VARCHAR(100); --T_SE_FPOLLUTANTDISCHARGE的字段名
        DECLARE value DECIMAL(20,5); --项目值
        DECLARE   sqlcode   INT   DEFAULT   0;--判断游标结束(100结束)
        DECLARE   u_sqlcode   INT   DEFAULT   0;--判断游标结束(自己复制游标结束标志)
        declare usersql varchar(4000); --动态sql;
       
    --索引'T_SE_FPOLLUTANTDISCHARGE'的字段
     DECLARE c1 CURSOR FOR
      select colname 
      from SYSCAT.COLUMNS where tabname = 'T_SE_FPOLLUTANTDISCHARGE'and typename = 'DECIMAL'; 
      
      
    --索引各机组项目值的字段
     DECLARE c2 CURSOR FOR
      select a.FGEN_CODE,case b.FPROJTYPE when '1' then a.FMONTHVALUE when '2' then a.FYEARVALUE when '3' then a.FLASTMONTVALUE end as A
      from TK_SE_MONTH a, TK_PROJECT b
      where a.FPROJ_ID = b.FPROJID+1
      and a.FReportPeriod =  v_date
      and a.FDEPC_CODE = v_fdepc
      and b.FPROJNAME_EN = COLNAME
      and a.FGEN_CODE <>'' ;
         
    --查找分支机构和集团公司,不存在的在表中添加
        -- 判断分支机构
        select FFATHER_CODE into FZJG from T_PLANTDEFINE where FDEPC_CODE = v_fdepc;
       
        select count(*) into CZ_TYPE from T_SE_FPollutantDischarge
        where FReportPeriod =  v_date
        and FDepNo = FZJG;
       
        if  CZ_TYPE<1
        then
      INSERT INTO T_SE_FPollutantDischarge(FDepNo,FFatherDepNo,FDepLevel,FReportPeriod,FNewFactory)
      select FFATHER_CODE,'0','1',v_date,FFATHER_NAME from T_PLANTDEFINE where FDEPC_CODE = v_fdepc ;
     end if; 
     
     --判断集团公司
     select count(*) into CZ_TYPE from T_SE_FPollutantDischarge
     where FReportPeriod =  v_date
        and FDepNo = '0';
       
        if  CZ_TYPE<1
        then
      INSERT INTO T_SE_FPollutantDischarge(FDepNo,FFatherDepNo,FDepLevel,FReportPeriod,FNewFactory)
      values('0','','0',v_date,'华电集团');
     end if; 
      
    --删除本期电厂及其机组信息
     delete from T_SE_FPollutantDischarge
     where FReportPeriod =  v_date
     and (FDepNo = v_fdepc or FFatherDepNo = v_fdepc);
     
    --插入电厂基本信息   
     INSERT INTO T_SE_FPollutantDischarge(FDepNo,FFatherDepNo,FDepLevel,FReportPeriod,FNewFactory)
     select distinct FDEPC_CODE,FZJG,'2',v_date,FDEPC_NAME
     from TK_SE_MONTH
     where FReportPeriod =  v_date
     and FDEPC_CODE = v_fdepc ;
     
    --插入机组基本信息 
     INSERT INTO T_SE_FPollutantDischarge(FDepNo,FFatherDepNo,FDepLevel,FReportPeriod,FNewFactory)
     select distinct FGEN_CODE,v_fdepc,'3',v_date,FGEN_NAME
     from TK_SE_MONTH
     where FReportPeriod =  v_date
     and FDEPC_CODE = v_fdepc
     and FGEN_CODE <> '' ;
     
    --更新电厂指标值
     open c1;
     ins_loop1:  
            LOOP  
                FETCH   c1   INTO   COLNAME; 
        --判断游标结束        
                IF sqlcode=100  THEN         
                    LEAVE   ins_loop1;
                END   IF;       
        --查询出电厂一个项目的项目值
       select count(*) into CZ_TYPE
       from TK_SE_MONTH a, TK_PROJECT b
       where a.FPROJ_ID = b.FPROJID
       and a.FReportPeriod =  v_date
       and a.FDEPC_CODE = v_fdepc
       and b.FPROJNAME_EN = COLNAME
       and a.FGEN_CODE ='' ;
       
       if CZ_TYPE > 0
       then   
        select case b.FPROJTYPE when '1' then a.FMONTHVALUE when '2' then a.FYEARVALUE when '3' then a.FLASTMONTVALUE end as A  into value
        from TK_SE_MONTH a, TK_PROJECT b
        where a.FPROJ_ID = b.FPROJID
        and a.FReportPeriod =  v_date
        and a.FDEPC_CODE = v_fdepc
        and b.FPROJNAME_EN = COLNAME
        and a.FGEN_CODE ='' ;
      
      --组合动态的电厂项目修改语句 
        set usersql = 'update T_SE_FPOLLUTANTDISCHARGE set '||COLNAME||'='|| char(value)||' where FDepNo ='||''''||v_fdepc||''''||' and FReportPeriod = '||''''||v_date||'''';
        if usersql <> ''
        then
         prepare s1 from usersql;
         execute s1;
        end if; 
       end if;               
            END LOOP ins_loop1; 
     close c1;
     
    --更新机组指标值
     open c1;
     ins_loop2:  
            LOOP
       set sqlcode = u_sqlcode;   
                FETCH   c1   INTO   COLNAME; 
        --判断游标结束        
                IF sqlcode=100  THEN         
                    LEAVE   ins_loop2;
                END   IF;
                set u_sqlcode = sqlcode;         
        --查询出机组一个项目的项目值
       select count(*) into CZ_TYPE
       from TK_SE_MONTH a, TK_PROJECT b
       where a.FPROJ_ID = b.FPROJID+1
       and a.FReportPeriod =  v_date
       and a.FDEPC_CODE = v_fdepc
       and b.FPROJNAME_EN = COLNAME
       and a.FGEN_CODE <>'' ;
       
       if CZ_TYPE > 0
       then
        open c2;
        ins_loopJZ:
         LOOP
          FETCH   c2   INTO   JZ,value;
          IF sqlcode=100  THEN         
           LEAVE   ins_loopJZ;
          END   IF;   
        --组合动态的机组项目修改语句 
          set usersql = 'update T_SE_FPOLLUTANTDISCHARGE set '||COLNAME||'='|| char(value)||' where FDepNo ='||''''||JZ||''''||' and FReportPeriod = '||''''||v_date||'''';
          if usersql <> ''
          then
           prepare s2 from usersql;
           execute s2;
          end if; 
         END LOOP ins_loopJZ;
        close c2; 
       end if;               
            END LOOP ins_loop2; 
     close c1;  
     
       END  p1
       @
     

    Comments (1)

    Please wait...
    Sorry, the comment you entered is too long. Please shorten it.
    You didn't enter anything. Please try again.
    Sorry, we can't add your comment right now. Please try again later.
    To add a comment, you need permission from your parent. Ask for permission
    Your parent has turned off comments.
    Sorry, we can't delete your comment right now. Please try again later.
    You've exceeded the maximum number of comments that can be left in one day. Please try again in 24 hours.
    Your account has had the ability to leave comments disabled because our systems indicate that you may be spamming other users. If you believe that your account has been disabled in error please contact Windows Live support.
    Complete the security check below to finish leaving your comment.
    The characters you type in the security check must match the characters in the picture or audio.

    To add a comment, sign in with your Windows Live ID (if you use Hotmail, Messenger, or Xbox LIVE, you have a Windows Live ID). Sign in


    Don't have a Windows Live ID? Sign up

    Picture of Anonymous
    13 Sept.

    Trackbacks

    The trackback URL for this entry is:
    http://mousexian.spaces.live.com/blog/cns!15D473ECCE04126!710.trak
    Weblogs that reference this entry
    • None