存储过程 游标 事例

Create  PROC [GetPerformance]
(
    @vp varchar(25),
    @range char(1) ,
    @depetid int   ,
    @Q varchar(1),         

    @QBeginDate datetime ,
    @QEndDate datetime ,            

    @YBeginDate datetime ,
    @YEndDate datetime
)
AS
Begin
      --1:创建临时表存储全部数据的容器
      Create Table #MyTempTable
        (
            Did int ,
            MRange varchar(20),
            TeamName varchar(50),
            MGRName varchar(25),        

            M1 decimal(18,2),
            M2 decimal(18,2),
            M3 decimal(18,2),          

            C decimal(18,2),
            F decimal(18,2),
            J decimal(18,2),
            T decimal(18,2),            

            QuarterML DECIMAL(18,2),
            QuarterMLRW decimal(18,2),
            QuarterMLRate varchar(30),         

            YearHTPrice DECIMAL(18,2),         

            YearML DECIMAL(18,2),
            YearMLRW decimal(18,2),
            YearMLRate varchar(30),
            YML DECIMAL(18,2),
            QML decimal(18,2)
        )
    --2:获得部门信息,利用游标循环每一个部门,获得相应的数据

            declare @Did int
            declare @MRange varchar(20)
            declare @TeamName varchar(50)
            declare @MGRName varchar(25)       

            declare @M1 decimal(18,2)
            declare @M2 decimal(18,2)
            declare @M3 decimal(18,2)           

            declare @C decimal(18,2)
            declare @F decimal(18,2)
            declare @J decimal(18,2)
            declare @T decimal(18,2)            

            declare @QuarterML DECIMAL(18,2)
            declare @QuarterMLRW decimal(18,2)
            declare @QuarterMLRate varchar(30)         

            declare @YearHTPrice DECIMAL(18,2)         

            declare @YearML DECIMAL(18,2)
            declare @YearMLRW decimal(18,2)
            declare @YearMLRate varchar(30)            

            declare @vpid varchar(25)                  -

            declare @YML decimal(18,2)   --用于排序没有百分比
            declare @QML decimal(18,2)
     --3:获得年度
     declare @nowYear varchar(20)    --当前年
     set @nowYear = Year(GETDATE())  --默认为当前年
     if(MONTH(GETDATE())=1 or MONTH(GETDATE())=2 or MONTH(GETDATE())=3)    -- 当前时间为1,2,3月
     Begin
          if(@Q = ‘1‘ or @Q = ‘2‘ or @Q = ‘3‘)
          begin
          set @nowYear = Year(GETDATE()) - 1
          end
     End
     Else
     Begin                         ---否则4~12月为当前年,查询的季度为4,则年+1
          if(@Q = ‘4‘)
          Begin
          set @nowYear = Year(GETDATE()) + 1
          End
     End

     --3:获得季度是从那年开始的,方便取出任务
     declare @qrwYear varchar (20)
     set @qrwYear = Year(GETDATE())
     if(MONTH(GETDATE())=1 or MONTH(GETDATE())=2 or MONTH(GETDATE())=3)    -- 当前时间为1,2,3月,并且查询的季度是1.2.3,则年减1
     Begin
        set @qrwYear = Year(GETDATE()) - 1
    End
     ---------------------------------------------------------------------------------------
     --4:获得月分
     declare @tempm1 varchar(10)  --第一个月
     declare @tempm2 varchar(10)  --第二个月
     declare @tempm3 varchar(10)  --第三个月
     if(@Q = ‘1‘)
     Begin
       set @tempm1 =‘4‘
       set @tempm2 =‘5‘
       set @tempm3 =‘6‘
     End
     else if (@Q = ‘2‘)
     Begin
       set @tempm1 =‘7‘
       set @tempm2 =‘8‘
       set @tempm3 =‘9‘
     End
     else if (@Q = ‘3‘)
     Begin
       set @tempm1 =‘10‘
       set @tempm2 =‘11‘
       set @tempm3 =‘12‘
     End
     else if (@Q = ‘4‘)
     Begin
       set @tempm1 =‘1‘
       set @tempm2 =‘2‘
       set @tempm3 =‘3‘
     End
   ---------------------------------------------------------------------------------------

    declare cur cursor  --定义一个游标
    read_only
    for

            SELECT     dbo.Department.DepartmentID, CASE WHEN Department.Range = ‘0‘ THEN ‘内‘ ELSE ‘外‘ END AS ‘范围‘, dbo.Department.DepartmentName AS ‘部门‘,
                       T.mgrName AS ‘总监‘, M.VPEid
            FROM         dbo.Department LEFT OUTER JOIN
                          (SELECT     dbo.Employee.EmployeeID AS mgrEid, dbo.Employee.DepartmentID AS mgrDid, dbo.Employee.EmployeeName AS mgrName
                            FROM          dbo.Employee INNER JOIN
                                                   dbo.Role ON dbo.Employee.RoleID = dbo.Role.RoleID
                            WHERE      (dbo.Role.RoleName = ‘salesmgr‘) AND (dbo.Employee.State = 1)) AS T ON T.mgrDid = dbo.Department.DepartmentID LEFT OUTER JOIN
                          (SELECT     Employee_1.EmployeeID AS VPEid, Employee_1.EmployeeName AS VPName, dbo.EmployeeOwnDepartment.DepartmentID AS VPID
                            FROM          dbo.Employee AS Employee_1 INNER JOIN
                                                   dbo.Role AS Role_1 ON Employee_1.RoleID = Role_1.RoleID RIGHT OUTER JOIN
                                                   dbo.EmployeeOwnDepartment ON Employee_1.EmployeeID = dbo.EmployeeOwnDepartment.EmployeeID
                            WHERE      (Role_1.RoleName = ‘saleVP‘) AND (Employee_1.State = 1)) AS M ON M.VPID = dbo.Department.DepartmentID

            WHERE     (dbo.Department.IsValid = 1)
            and (@vp =‘‘ or M.VPEid=@vp)
            and (@range =‘‘ or Department.Range = @range)
            and (@depetid =‘‘ or Department.DepartmentID=@depetid)
    open cur            --打开游标

        fetch next from cur into @Did,@MRange,@TeamName,@MGRName,@vpid --把提取操作的列数据放到局部变量中
    while(@@fetch_status=0) --返回被 FETCH 语句执行的最后游标的状态
    Begin
        --1:获得季度内3个月份的合同金额

        select @M1=CAST((SUM(SalesAgreement.HTPrice)/10000) as decimal(18,2)) from SalesAgreement inner join Project on Project.ProjectID=SalesAgreement.ProjectID
        where project.EmployeeID in ( SELECT EmployeeID FROM Employee WHERE DepartmentID= @Did )
        AND YEAR(SalesAgreement.HtDate) = @nowYear AND MONTH(SalesAgreement.HtDate)=@tempm1

        --4: 季度毛利任务
        select @QuarterMLRW = MAX(ML) from DeptQuarterTask where [Year]=@qrwYear and [Quarter]=@Q AND DepartmentID=@Did
        --5: 季度完成率
        if(@QuarterMLRW = 0)
        begin
           set  @QuarterMLRate = ‘0‘
           set @QML = 0
        end
        else
        begin
           set @QuarterMLRate =CAST (  cast (  ( (@QuarterML / @QuarterMLRW) * 100) as decimal(18,2) )    as varchar(30))  + ‘%‘
           set @QML = cast (   (  (@QuarterML / @QuarterMLRW) * 100) as decimal(18,2))
        end

        --10:插入各个字段的值到临时表产生一行部门的所有数据
        insert into #MyTempTable values(
             @Did ,
             @MRange ,
             @TeamName ,
             @MGRName,
             @M1 ,
             @M2 ,
             @M3 ,
             @C ,
             @F ,
             @J ,
             @T ,
             @QuarterML ,
             @QuarterMLRW ,
             @QuarterMLRate ,
             @YearHTPrice ,
             @YearML ,
             @YearMLRW ,
             @YearMLRate , @YML,@QML)

        --提前下一位信息
        fetch next from cur into @Did,@MRange,@TeamName,@MGRName,@vpid
    End
    close cur
    deallocate cur --删除游标

    select * from #MyTempTable order by  MRange asc,YML desc , QML desc , TeamName asc

    --查询临时表传入数据进行分页
    --set @sql = ‘select * from #MyTempTable‘
    --set @sqlCountText = ‘select count(*) from #MyTempTable‘

    ----执行分页存储过程
    --exec dbo.GetPageData @sql,@PageCurrent,@PageSize,@PageCount,@sqlCountText

    --删除临时表
    drop table #MyTempTable
End

存储过程 游标 事例

时间: 2024-10-25 18:43:11

存储过程 游标 事例的相关文章

存储过程 游标

CREATE PROCEDURE [dbo].[RuleExecute_Temp] AS declare @executedate varchar(20) declare @branchcode varchar(50) declare @rulenumber varchar(50) declare @number varchar(20) declare @JLWZCode varchar(20) declare cur cursor for SELECT ExcuteDate,RuleNumbe

一次使用存储过程游标遇到的坑

一次使用存储过程游标遇到的坑 有这样一个需求:统计某省某市某区前6个月的数据,直接sql查询效率很低,于是打算做定时任务,用定时器执行存储过程的方式在每月初统计上月的相关数据. 使用存储过程就要用到游标了,之前很少写存储过程,对游标也不是熟悉,咋办呢,现学现用啦. 创建存储过程 1 CREATE 2 [DEFINER = { user | CURRENT_USER }] 3 PROCEDURE sp_name ([proc_parameter[,...]]) 4 [characteristic

mysql中存储过程游标使用

mysql游标的使用能够循环获取结果级进行操作 定义游标 DECLARE id int; DECLARE name varchar(30); DECLARE cursor_name CURSOR FOR select c_id,c_name from tables (把select查询到的数据赋给游标cursor_name ) OPEN cursor_name (打开游标) FETCH cursor_name INTO id,name; (打开游标去取一条值如果该值存在,并把游标前进该行) CL

Oracle存储过程,游标使用

Oracle存储过程: 语法: CREATE [OR REPLACE] PROCEDURE procedure_name (arg1 [mode1] datatype1,arg2 [mode2] datatype2,...) IS [AS] PL/SQL BLOCK; mode用于指定输入输出参数:IN为输入参数,OUT为输出参数,当为输入参数时可以省去IN,OUT不能省去. 如果没有输入输出参数,可以省去 (arg1 [mode1] datatype1,arg2 [mode2] datatyp

Oracle数据库PL/SQL存储过程游标触发器

创建一个添加FOOD的存储过程 create or replace procedure add_food_pro (name in varchar,price in number,description in varchar) as  begin insert into food (f_name,f_price,description)values(name,price,description); commit; end; --下面的代码是调用存储过程 begin add_food_pro('糖

MySQL 存储过程/游标/触发器/事务

来源:http://www.cnblogs.com/zhuyp1015/p/3575823.html 将会用到的几个表 mysql> DESC products; +------------+--------------+------+-----+---------+----------------+| Field      | Type         | Null | Key | Default | Extra          |+------------+--------------+-

My sql之存储过程+游标

sql 实例如下: /**************定义更改car_station_user_acct_his new_balance old_balance存储过程**************/ create procedure abc (in number varchar(256))  -- [in表示这个参数是传入参数,out表示这个是传出参数(类似Java中的return),in out表示这个既是传入,又是传出参数,可以利用它传入该存储过程,然后接到处理后的这个参数] begin --

mysql之——存储过程 + 游标 + 事务

下面是自己曾经编写过的mysql数据库存储过程,留作存档,以后用到的时候拿来参考. 其中,涉及到了存储过程.游标(双层循环).事务. [说明]:代码中的注释只针对当时业务而言,无须理会. 代码如下: DELIMITER $$ DROP PROCEDURE IF EXISTS `transferEmailTempData`$$ CREATE PROCEDURE transferEmailTempData(IN jobId VARCHAR(24)) BEGIN DECLARE idval VARCH

SQL存储过程+游标 循环批量()操作数据

本人收集的,挺有用的 1. 利用游标循环更新.删除MemberAccount表中的数据 DECLARE My_Cursor CURSOR --定义游标 FOR (SELECT * FROM dbo.MemberAccount) --查出需要的集合放到游标中 OPEN My_Cursor; --打开游标 FETCH NEXT FROM My_Cursor ; --读取第一行数据 WHILE @@FETCH_STATUS = 0 BEGIN --UPDATE dbo.MemberAccount SE