码迷,mamicode.com
首页 > 其他好文 > 详细

存储过程

时间:2016-10-11 14:04:52      阅读:242      评论:0      收藏:0      [点我收藏+]

标签:

中间存储过程

CREATE  PROCEDURE [dbo].[summary_TradePriceWeight]
@pin VARCHAR(10),--油品 93 ,0, 10,20......
@datetime DATETIME--时间
AS 
BEGIN 
 DECLARE @p VARCHAR(10)
 IF @pin=93
 begin
   SET @p=汽油批发
 END 
 ELSE IF @pin=柴油平均 
 BEGIN  
   SET @p=柴油批发 
 END
  

        if object_id(tempdb..#BaseTable) IS NOT  null 
                    Begin  
                        DROP TABLE #BaseTable
                    End
                else 
                    begin
                        create table #BaseTable(OrganizationID VARCHAR(3),
                        ProvinceName varchar(10),ReportDate DATETIME,PIN VARCHAR(10), CNPC DECIMAL(18,4),
                        Sinopec DECIMAL(18,4),Society DECIMAL(18,4), GuiDindJia DECIMAL(18,4),xfl DECIMAL(18,4) ,
                        sc DECIMAL(18,4),zsy DECIMAL(18,4),zsh DECIMAL(18,4))
                END
                
                
        INSERT INTO #BaseTable 
        SELECT    OrganizationID ,ProvinceName,ReportDate,PIN, CNPC,Sinopec,Society,(CNPC-CNPC_c)  AS GuiDindJia ,
        xfl,sc,zsy,zsh
        FROM dbo.Sum_TradePrice_Province  AS pt LEFT JOIN Dic_PriceWeight AS pw 
        ON pw.ProvinceID=pt.OrganizationID AND pt.ReportDate BETWEEN pw.BeginTime AND  pw.EndTime AND pw.[type]=@p
        WHERE  OilStandard=全部  AND PIN=@pin AND ReportDate=@datetime AND OrganizationID !=113   --113西藏
          ORDER BY  ProvinceID

        if object_id(tempdb..#Table) IS NOT  null 
                    Begin  
                        DROP TABLE #table
                    End
                else 
                    begin
                        create table #Table(rowcoun INT ,OrganizationID VARCHAR(3),
                        ProvinceName VARCHAR(10),ReportDate DATETIME,PIN VARCHAR(10), CNPC DECIMAL(18,4),
                        Sinopec DECIMAL(18,4),Society DECIMAL(18,4), GuiDindJia DECIMAL(18,4),xfl DECIMAL(18,4) ,
                        sc DECIMAL(18,4),zsy DECIMAL(18,4),zsh DECIMAL(18,4))
                END

        INSERT INTO #Table 
        SELECT * FROM (
        SELECT   ROW_NUMBER() OVER( ORDER BY t.OrganizationID) AS ROW  ,t.* FROM #BaseTable AS t  )AS tab


        DECLARE @btcount int --#Table表数据条数
        SELECT @btcount=COUNT(1) FROM #Table
        IF(@btcount>0)--#Table表数据条数大于0
        BEGIN 
                DECLARE @i  int
                SET @i=1; 

                if object_id(tempdb..#Jtable) is  NOT null 
                            Begin  
                                DROP TABLE  #Jtable
                            End
                        else 
                            begin
                                create table #Jtable(OrganizationID VARCHAR(3),
                                ProvinceName VARCHAR(10),Guidingjia DECIMAL(18,4),shdwsjjg DECIMAL(18,4), zsysjjg DECIMAL(18,4),
                                zshsjjg DECIMAL(18,4),xflzb DECIMAL(18,4),shdwscfezb DECIMAL(18,4),zsyscfezb DECIMAL(18,4),
                                zshscfezb DECIMAL(18,4), gdjjq DECIMAL(18,4),sjjjq DECIMAL(18,4), zysjj DECIMAL(18,4),scsjj DECIMAL(18,4),
                                qssjj DECIMAL(18,4) ,shdwjqsjj DECIMAL(18,4) ,zsyjqsjj DECIMAL(18,4) ,zshjqsjj DECIMAL(18,4) ,zhysjj DECIMAL(18,4) ,
                                zsyzyzb DECIMAL(18,4) ,zshzyzb DECIMAL(18,4) )
                        END


                WHILE @i<=@btcount --循环#Table
                BEGIN 
                --声明变量 (保存省的数据)
                DECLARE @provinceid  VARCHAR(3)--省份id
                
                DECLARE @provincename VARCHAR(10)--省份名称

                DECLARE @p_guidingjia  DECIMAL(18,4) --省规定价
                
                DECLARE @p_sjjgshdw DECIMAL(18,4) --省市场(社会单位)实际价格
                 
                DECLARE @p_sjjgzsy DECIMAL(18,4) --省中石油实际价格
                
                DECLARE @p_sjjgzsh DECIMAL(18,4) --省中石化实际价格
                  
                DECLARE @p_xflzb  DECIMAL(18,4)--省消费量占比
                  
                DECLARE @p_scfezbshdw DECIMAL(18,4) --省市场(社会单位)市场份额占比
                   
                DECLARE @p_scfezbzsy DECIMAL(18,4) --省中石油市场份额占比
                  
                DECLARE @p_scfezbzsh DECIMAL(18,4) --省中石化市场份额占比
                   
                DECLARE @p_gdjjq   DECIMAL(18,4)--省规定价加权(按消费量)
                
                DECLARE @p_sjjjq   DECIMAL(18,4)--省实际价加权(按消费量)
                  
                DECLARE @p_zysjj   DECIMAL(18,4)--省主营实际价(按消费量)
                
                DECLARE @p_scsjj   DECIMAL(18,4)--省市场实际价(按消费量)
                  
                DECLARE @p_qssjj  DECIMAL(18,4) --省全省实际价(按份额)
                  
                DECLARE @p_shdwjqsjj  DECIMAL (18,4)--省市场(社会单位)加权实际价(按份额)
                  
                DECLARE @p_zsyjqsjj  DECIMAL (18,4)--省中石油加权实际价(按份额)
                  
                DECLARE @p_zshjqsjj  DECIMAL (18,4)--省中石化加权实际价(按份额)
                   
                DECLARE @p_zhysjj  DECIMAL (18,4) --省主营实际价(按份额)
                  
                DECLARE @p_zsyzyzb  DECIMAL (18,4) --省中石油主营占比
                  
                DECLARE @p_zshzyzb  DECIMAL (18,4) --省中石化主营占比
                  
                --------结束声明变量
                --获取从基表中可以直接得到的数据给变量赋值,用于计算
                SELECT @provinceid=OrganizationID  , 
                 @provincename=ProvinceName, 
                @p_guidingjia=ISNULL(GuiDindJia,0),  
                @p_sjjgshdw=ISNULL(Society,0),  
                @p_sjjgzsy=ISNULL(CNPC,0), 
                 @p_sjjgzsh=ISNULL(Sinopec,0) 
                ,@p_xflzb=ISNULL(xfl,0) , 
                 @p_scfezbshdw=ISNULL(sc,0) ,
                 @p_scfezbzsy=ISNULL(zsy,0)  ,
                 @p_scfezbzsh=ISNULL(zsh,0)
                FROM #Table WHERE rowcoun=@i
                ---计算
                --
                SET  @p_gdjjq= @p_guidingjia*@p_xflzb   --J列


                SET   @p_shdwjqsjj=  @p_sjjgshdw*@p_scfezbshdw  --O列
                SET   @p_zsyjqsjj=  @p_sjjgzsy*@p_scfezbzsy    --P列
                SET   @p_zshjqsjj=  @p_sjjgzsh*@p_scfezbzsh    --Q列

                SET  @p_qssjj=@p_shdwjqsjj+@p_zsyjqsjj+@p_zshjqsjj  --N列

                SET   @p_sjjjq=@p_xflzb*@p_qssjj       --K列

IF((@p_scfezbzsy+@p_scfezbzsh)!=0)
BEGIN 
                SET @p_zsyzyzb= @p_scfezbzsy/(@p_scfezbzsy+@p_scfezbzsh)    --S列
                END ELSE BEGIN SET @p_zsyzyzb=0 END 
                
                IF((@p_scfezbzsy+@p_scfezbzsh)!=0)
                begin     SET @p_zshzyzb= @p_scfezbzsh/(@p_scfezbzsy+@p_scfezbzsh)    --T列
                 END
                   ELSE  begin SET @p_zshzyzb=0 END  
            
                SET  @p_zhysjj= (@p_sjjgzsy* @p_zsyzyzb)+(@p_sjjgzsh* @p_zshzyzb)  --R列

                SET  @p_zysjj=@p_zhysjj* @p_xflzb             --L列

                SET   @p_scsjj= @p_sjjgshdw* @p_xflzb  --M列 

                INSERT INTO  #Jtable
                        ( OrganizationID , ProvinceName ,   Guidingjia ,  shdwsjjg , zsysjjg , zshsjjg ,   xflzb ,
                          shdwscfezb ,   zsyscfezb ,  zshscfezb , gdjjq ,sjjjq ,   zysjj , scsjj , qssjj ,
                          shdwjqsjj ,   zsyjqsjj , zshjqsjj , zhysjj , zsyzyzb , zshzyzb
                        )
                VALUES  ( @provinceid , @provincename , @p_guidingjia,  @p_sjjgshdw ,  @p_sjjgzsy ,  @p_sjjgzsh , @p_xflzb , 
                          @p_scfezbshdw ,  @p_scfezbzsy ,   @p_scfezbzsh ,  @p_gdjjq ,  @p_sjjjq ,  @p_zysjj ,  @p_scsjj , @p_qssjj ,
                          @p_shdwjqsjj , @p_zsyjqsjj ,  @p_zshjqsjj ,@p_zhysjj ,  @p_zsyzyzb ,   @p_zshzyzb 
                        )

                      SET @i=@i+1
                END 
                
                
            
            if object_id(tempdb..#AllCountryTable) IS NOT  null 
                    Begin  
                        DROP TABLE #AllCountryTable
                    End
                else 
                    begin
                        create table #AllCountryTable(OrganizationID VARCHAR(3),ProvinceName VARCHAR(10),
                        Guidingjia DECIMAL(18,4),qssjj DECIMAL(18,4) ,shdwsjjg DECIMAL(18,4),zhysjj DECIMAL(18,4)   )
                END
        
INSERT  INTO #AllCountryTable
        SELECT   010,全国 ,     SUM(gdjjq),SUM(sjjjq), SUM(scsjj),SUM(zysjj)
         FROM #Jtable UNION SELECT  OrganizationID,ProvinceName,Guidingjia,qssjj,shdwsjjg,zhysjj FROM #jTable 
        
        SELECT * FROM #AllCountryTable  ORDER BY OrganizationID
        
        DROP TABLE #basetable
        DROP TABLE #Table
        DROP TABLE #Jtable
        DROP TABLE #AllCountryTable
    end

END 

结果存储过程:

CREATE   PROCEDURE [dbo].[summary_TradePriceWeightEnd]
@pin VARCHAR(10),--油品 93 ,0, 10,20......
@datetime DATETIME--时间
AS 
BEGIN 
 select @datetime=max([Date]) from Sys_Workdays where [Date]<=@datetime  and IsWorkday=1 
        declare @yesterday varchar(10)
         select @yesterday=max([Date]) from Sys_Workdays where [Date]<@datetime  and IsWorkday=1 
        ----------  
        if object_id(tempdb..#LingshiTable) is NOT  null 
                Begin  
                    DROP TABLE  #LingshiTable
                End
            ELSE
            BEGIN 
                  create table #LingshiTable(  OrganizationID VARCHAR(3),ProvinceName VARCHAR(10),
                                      Guidingjia decimal(18,4), qssjj DECIMAL(18,4) ,
                                      shdwsjjg DECIMAL(18,4),zhysjj DECIMAL(18,4))
            END
            ---
             INSERT into #LingshiTable    EXEC  summary_TradePriceWeight @pin ,@datetime
            
            
            
        ------------------
        if object_id(tempdb..#tmp_priceToday) is NOT  null 
                Begin  
                    DROP TABLE  #tmp_priceToday
                End
            ELSE
            BEGIN 
                  create table #tmp_priceToday( rowcoun int , OrganizationID VARCHAR(3),ProvinceName VARCHAR(10),
                                      Guidingjia decimal(18,4), qssjj DECIMAL(18,4) ,
                                      shdwsjjg DECIMAL(18,4),zhysjj DECIMAL(18,4))
            END
        INSERT INTO #tmp_priceToday 
        SELECT * FROM (
        SELECT   ROW_NUMBER() OVER( ORDER BY t.OrganizationID) AS ROW  ,t.* FROM #LingshiTable AS t  )AS tab


        ----------------------
            if object_id(tempdb..#tmp_priceYesday) is NOT  null 
                Begin  
                    DROP TABLE  #tmp_priceYesday
                End
            ELSE
            BEGIN 
                  create table #tmp_priceYesday(  OrganizationID VARCHAR(3),ProvinceName VARCHAR(10),
                                      Guidingjia decimal(18,4), qssjj DECIMAL(18,4) ,
                                      shdwsjjg DECIMAL(18,4),zhysjj DECIMAL(18,4))
            END
            ---
             INSERT into  #tmp_priceYesday   EXEC  summary_TradePriceWeight @pin ,@yesterday
        ----------------
            if object_id(tempdb..#tmp_ReturnDateTable) is NOT  null 
                Begin  
                    DROP TABLE  #tmp_ReturnDateTable
                End
            ELSE
            BEGIN 
                  create table #tmp_ReturnDateTable(  OrganizationID VARCHAR(3),ProvinceName VARCHAR(10),
                                      Guidingjia decimal(18,4), qssjj DECIMAL(18,4) ,shdwsjjg DECIMAL(18,4),zhysjj DECIMAL(18,4),
                                      qssjj_than DECIMAL(18,4), shdwsjjg_than DECIMAL(18,4),zhysjj_than DECIMAL(18,4),
                                       qssjj_ty DECIMAL(18,4), shdwsjjg_ty DECIMAL(18,4),zhysjj_ty DECIMAL(18,4) )
            END
            
            
        DECLARE @counttoday INT 
        SELECT  @counttoday = count(1) FROM #tmp_priceToday
        DECLARE @countyesday INT 
        SELECT @countyesday=COUNT(1) FROM #tmp_priceYesday
        IF(@counttoday>0 AND @countyesday >0)
            BEGIN 
                 DECLARE @i INT 
                 SET @i=1
                 WHILE @i<=@counttoday
                 BEGIN
                         --声明变量
                         DECLARE @organizationid VARCHAR(3)
                         DECLARE @provincename VARCHAR(10)
                         DECLARE @Guidingjia decimal(18,4)
                         DECLARE @qssjj DECIMAL(18,4)
                         DECLARE  @shdwsjjg DECIMAL(18,4)
                         declare @zhysjj DECIMAL(18,4)
                         DECLARE @qssjj_than DECIMAL(18,4)
                         DECLARE @shdwsjjg_than DECIMAL(18,4)
                         DECLARE @zhysjj_than DECIMAL(18,4)
                         DECLARE   @qssjj_ty DECIMAL(18,4)
                         DECLARE @shdwsjjg_ty DECIMAL(18,4)
                         declare @zhysjj_ty DECIMAL(18,4)
                         -----
                        SELECT  @organizationid=OrganizationID ,@provincename=ProvinceName,
                        @Guidingjia=Guidingjia ,@qssjj=qssjj,@shdwsjjg=shdwsjjg,@zhysjj=zhysjj 
                        FROM #tmp_priceToday WHERE   rowcoun=@i
                        
                        SET @qssjj_than =@qssjj-@Guidingjia        --F列
                        SET @shdwsjjg_than=@shdwsjjg-@Guidingjia   --G列
                        SET @zhysjj_than=@zhysjj-@Guidingjia       --     H列
                        
                        ----
                        SELECT @qssjj_ty=qssjj,@shdwsjjg_ty=shdwsjjg,@zhysjj_ty=zhysjj FROM  
                        #tmp_priceYesday WHERE  OrganizationID=@organizationid
                        
                        
                             SET @qssjj_ty= @qssjj-@qssjj_ty        --I列
                              SET @shdwsjjg_ty= @shdwsjjg-@shdwsjjg_ty    --J列
                               SET @zhysjj_ty= @zhysjj-@zhysjj_ty     --k列
                               
                               
                            INSERT INTO  #tmp_ReturnDateTable
                                    ( OrganizationID ,
                                      ProvinceName ,
                                      Guidingjia ,
                                      qssjj ,
                                      shdwsjjg ,
                                      zhysjj ,
                                      qssjj_than ,
                                      shdwsjjg_than ,
                                      zhysjj_than ,
                                      qssjj_ty ,
                                      shdwsjjg_ty ,
                                      zhysjj_ty
                                    )
                            VALUES  ( @OrganizationID , -- OrganizationID - varchar(3)
                                      @provincename , -- ProvinceName - varchar(10)
                                      @Guidingjia , -- Guidingjia - decimal
                                      @qssjj , -- qssjj - decimal
                                      @shdwsjjg , -- shdwsjjg - decimal
                                      @zhysjj , -- zhysjj - decimal
                                      @qssjj_than , -- qssjj_than - decimal
                                      @shdwsjjg_than , -- shdwsjjg_than - decimal
                                      @zhysjj_than , -- zhysjj_than - decimal
                                      @qssjj_ty , -- qssjj_ty - decimal
                                      @shdwsjjg_ty , -- shdwsjjg_ty - decimal
                                      @zhysjj_ty  -- zhysjj_ty - decimal
                                    )
                        SET @i=@i+1    
                 END 
            
            
            END 
    
    SELECT * FROM #tmp_ReturnDateTable ORDER BY  OrganizationID  
    
    END 

执行存储过程:
技术分享

调用存储过程:

        public DataTable   GetData(string pin, string date)
        {
            try
            {

                List<SqlParameter> para = new List<SqlParameter>();
                para.Add(new SqlParameter("@pin", pin));
                para.Add(new SqlParameter("@datetime", date));
                DbHelper summaryHelper = DbHelper.Create(Database.Summary);
                DataTable dt = summaryHelper.GetDataTable("summary_TradePriceWeightEnd", para, true);
             }
            catch (Exception ex)
            {
             }
            return dt;

        }

 

存储过程

标签:

原文地址:http://www.cnblogs.com/lk516924/p/5948915.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!