zoukankan      html  css  js  c++  java
  • sqlServer游标的使用

    USE [PatPD1]
    GO
    /****** Object:  UserDefinedFunction [dbo].[fun_GetConditionInner]    Script Date: 2015/5/9 9:03:38 ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO

    ALTER function [dbo].[fun_GetConditionInner] (@Product varchar(20))
      returns  varchar(8000)
    AS
    begin
       declare @ReturnValue varchar(8000)=''--用于拼接返回值
       declare @Status varchar(8000)--用于存储状态值
       declare @Type nvarchar(20)--EQPtype
       declare @TempType nvarchar(20)--EQPtype的临时变量用于比较
       declare @temp table(
          Status nvarchar(20),
          Type nvarchar(20)
       )
        -- declare @Product nvarchar(20)--EQPtype -- 测试放开
                                       -- set @Product='8701BA'
                                       -- set @Product='8725AA-0030A'
          insert into @temp select b.Status,a.Type from [dbo].[MtfSetting] a join [dbo].[MtfDetailSetting] b on a.Id=b.Id and a.Product =@Product
      --申明游标 为EquipmentID,FixQty
    declare MTF_Cursor CURSOR FOR select Status,Type from @temp  
    --打开游标  

    open MTF_Cursor
          fetch next from MTF_Cursor into @Status,@Type --将游标向下移1行,获取的数据放入之前定义的变量@Status,@JoinCalculate中

             while  @@FETCH_STATUS = 0 --返回 FETCH 执行语句时游标的状态(1:fetch获取数据成功 -1:fetch语句失败或此行不在结果集中 -2:被提取的行不存在)  
       begin   
            if(@TempType!=@Type)--动态拼接投入数
                 begin
                    set @ReturnValue+='
                    sum(CASE WHEN EQPTYPE='''+RTrim(@TempType)+''' THEN QUANTITY ELSE 0 END)  qty_TOTAL_'+Replace(@TempType,' ','')+','
                 end

                     set @ReturnValue+='
                                sum(case when a.STATUS='''+RTrim(@Status)+''''--拼接条件头部

          if((select count(*) from [dbo].[MtfSetting]where Product=@Product)>1 )--属于多种type的组合方式,多拼接一个 a.EQPTYPE 的条件
                 begin
                   set @ReturnValue+=' and a.EQPTYPE='''+RTrim(@Type)+''''
                  end

                     set @ReturnValue+=' then QUANTITY else 0 end) qty_'+RTrim(@Status)+'_'+Replace(@Type,' ','')+','--拼接条件尾部
            set @TempType=@Type--赋值给临时存储变量
           fetch next from MTF_Cursor into @Status,@Type --将游标向下移1行
       end
    close MTF_Cursor--关闭游标  
    deallocate MTF_Cursor--释放游标  


     if((select count(*) from [dbo].[MtfSetting]where Product=@Product)>1 )--属于多种type的组合方式
       begin
         set @ReturnValue+='
                    sum(CASE WHEN EQPTYPE='''+RTrim(@TempType)+''' THEN QUANTITY ELSE 0 END)  qty_TOTAL_'+Replace(@TempType,' ','')
       end
       else
       begin
       set @ReturnValue+='
                    sum(QUANTITY)  qty_TOTAL_'+Replace(@TempType,' ','')
       end
    --print @ReturnValue
      return @ReturnValue
     
    end



     

  • 相关阅读:
    解决Ubuntu下gedit中文乱码
    Linux下安装Matlab软件
    3.5mm耳机/麦克接头
    IIS无法加载PHP.ini
    修改mysql用户密码
    dhtmlxgrid使用基础
    远程桌面“终端服务器超出了最大允许连接数”的解决
    MATLAB中格式化M文件注释
    MATLAB中使用Cell对M文件分节
    MATLAB下一些常用易忘命令
  • 原文地址:https://www.cnblogs.com/fighting2014/p/4489540.html
Copyright © 2011-2022 走看看