zoukankan      html  css  js  c++  java
  • sqlserver一个循环圆的算法

    在sqlserver中没有数组的概率,我们就把数组的形式改成临时表的形式,临时表分为两列,第一列就是id,即为数组的下标;第二列为值,即为数组的值;

    上sql

    ALTER PROCEDURE [dbo].[miao]
       @start  datetime
       as 
    begin
    set nocount on 
    if exists(select * from sysobjects where name ='#temp_clss') --查找命令
    	drop table #temp_clss --删除 命令
    if exists(select * from sysobjects where name ='#temp_cls_dat') --查找命令
    	drop table #temp_cls_dat --删除 命令
    create table #temp_clss(id int,cls varchar(20))
    insert into #temp_clss values (0,'中')
    insert into #temp_clss values (1,'中')
    insert into #temp_clss values (2,'中')
    insert into #temp_clss values (3,'早')
    insert into #temp_clss values (4,'早')
    insert into #temp_clss values (5,'早')
    insert into #temp_clss values (6,'休')
    insert into #temp_clss values (7,'夜')
    insert into #temp_clss values (8,'夜')
    insert into #temp_clss values (9,'夜')
    insert into #temp_clss values (10,'休')
    insert into #temp_clss values (11,'休')
    SELECT rowid1=identity(int,0,1),convert(char(10), DATEADD(dd, number, @start) - 2, 120) AS rptday into #temp_cls_dat FROM master .. spt_values WHERE type = 'p' AND DATEDIFF(MI,  DATEADD(dd, number, @start) - 24, @start) > 0
    declare curs_dat cursor for 
    select * from #temp_cls_dat
    /*结果表*/
    create table #temp_result(日期 date,[甲值] varchar(20),[乙值] varchar(20),[丙值] varchar(20),[丁值] varchar(20))
    declare @id_res int
    declare @id_dat int
    declare @dat date
    open curs_dat
    /*甲乙丙丁*/
    declare @j varchar(20)
    declare @y varchar(20)
    declare @b varchar(20)
    declare @d varchar(20)
    fetch next from curs_dat into @id_dat,@dat
    while @@fetch_status=0
    begin
    	set @id_res=@id_dat%12
    	/*甲乙丙丁*/
    	/*循环圆*/
    	if @id_res<12
    	begin
    		select @j=cls from #temp_clss where id=@id_res
    		select @y=cls from #temp_clss where id=((11-2+@id_res)%12)
    		select @b=cls from #temp_clss where id=((11-5+@id_res)%12)
    		select @d=cls from #temp_clss where id=((11-8+@id_res)%12)
    	end
    	insert into #temp_result values (@dat,@j,@y,@b,@d)
    	fetch next from curs_dat into @id_dat,@dat
    end
    close curs_dat
    deallocate curs_dat
    select 
    [日期],
    case 
    	when 甲值='早' then '甲值' 
    	when 乙值='早' then '乙值' 
    	when 丙值='早' then '丙值'
    	when 丁值='早' then '丁值'
    	else 
    	'error'
    	end as [早],
    case 
    	when 甲值='中' then '甲值' 
    	when 乙值='中' then '乙值' 
    	when 丙值='中' then '丙值'
    	when 丁值='中' then '丁值'
    	else 
    	'error'
    	end as [中],
    case 
    	when 甲值='夜' then '甲值' 
    	when 乙值='夜' then '乙值' 
    	when 丙值='夜' then '丙值'
    	when 丁值='夜' then '丁值'
    	else 
    	'error'
    	end as [夜],
    case 
    	when 甲值='休' then '甲值' 
    	when 乙值='休' then '乙值' 
    	when 丙值='休' then '丙值'
    	when 丁值='休' then '丁值'
    	else 
    	'error'
    	end as [休]
     from #temp_result
    drop table #temp_clss
    drop table #temp_cls_dat
    drop table #temp_result
    set nocount off 
    end
    

      在这里,我的循环是以12为一个周期,所以

    在这些地方都用到了%12,即取余的意思,一旦id大于等于12的时候我就用取余处理,这样永远就不会超过最大id,就可以理解为循环圆的形式。

    **********初心不改,方得始终。
  • 相关阅读:
    html添加遮罩层
    html绝对定位如何居中
    springboot上传文件到本地服务器
    遍历一个文件目录,把所有的子目录都列出来
    Java判断cookie中是否有某个具体的cookie
    Redis 如何打开rdb 文件
    crontab -e 如何保存退出
    linux服务器设备上没有空间
    IDEA的SonarLint插件报错Unable to create symbol table for
    使用Rome读取RSS报错,org.xml.sax.SAXParseException: 前言中不允许有内容。
  • 原文地址:https://www.cnblogs.com/salv/p/10722714.html
Copyright © 2011-2022 走看看