---函数名称:日历函数
---函数作用:产生日历--
---使用方法:select * from calendar(3,2006)

ALTER  function calendar(@month int,@year int)
returns @DAY1 table(SUN char(3),MON Char(3),TUE Char(3),WED Char(3),THU Char(3),FRI Char(3),SAT Char(3))
as
begin
declare @i int
declare @j int
declare @intchk int
declare @dnum int
declare @curdate int
declare @month1 char(2)
declare @year1 char(4)
declare @date char(2)
declare @dtchk int
declare @dtval int
set @date='01'
set @month1=@month
set @year1=@year
set @dtchk=1
set @i=1
set @j=1

declare @DAY2 table(SUN char(3) default '',MON Char(3)default '',TUE Char(3)default '',WED Char(3)default '',THU Char(3)default '',FRI Char(3)default '',SAT Char(3)default '')

SELECT @curdate=DATEPART(dw, CONVERT(DATETIME,@date+'-'+@month1+'-'+@year1,103))

select @dnum=datediff(dd,convert(datetime,@date+'-'+@month1+'-'+@year1,103),dateadd(mm,1,convert(datetime,@date+'-'+@month1+'-'+@year1,103)))

while @j<=7
begin
if @curdate=@j
 begin
 if @j=1
  begin
   INSERT INTO @DAY2(sun)VALUES(@i)
   set @intchk=1
   set @dtchk=1
  end
 else if @j=2
  begin
   INSERT INTO @DAY2(MON)VALUES(@i)
   set @intchk=2
   set @dtchk=1
  end
 else if @j=3
  begin
   INSERT INTO @DAY2(TUE)VALUES(@i)
   set @intchk=3
   set @dtchk=1
  end
 else if @j=4
  begin
   INSERT INTO @DAY2(WED)VALUES(@i)
   set @intchk=4
   set @dtchk=1
  end
 else if @j=5
  begin
   INSERT INTO @DAY2(THU)VALUES(@i)
   set @intchk=5
   set @dtchk=1
  end
 else if @j=6
  begin
   INSERT INTO @DAY2(FRI)VALUES(@i)
   set @intchk=6
   set @dtchk=1
  end
 else if @j=7
  begin
   INSERT INTO @DAY2(SAT)VALUES(@i)
   set @intchk=7
   set @dtchk=1
  end 
 
end
set @j=@j+1
end
if @intchk=1
 begin
  update @day2 set mon=@i+1,tue=@i+2,wed=@i+3,thu=@i+4,fri=@i+5,sat=@i+6 where sun=1
  set @dtchk=@dtchk+6
 end
else if @intchk=2
 begin
  update @day2 set tue=@i+1,wed=@i+2,thu=@i+3,fri=@i+4,sat=@i+5 where mon=1
  set @dtchk=@dtchk+5
 end
else if @intchk=3
 begin
  update @day2 set wed=@i+1,thu=@i+2,fri=@i+3,sat=@i+4 where tue=1
  set @dtchk=@dtchk+4
 end
else if @intchk=4
 begin
  update @day2 set thu=@i+1,fri=@i+2,sat=@i+3 where wed=1
  set @dtchk=@dtchk+3
 end
else if @intchk=5
 begin
  update @day2 set fri=@i+1,sat=@i+2 where thu=1
  set @dtchk=@dtchk+2
 end
else if @intchk=6
 begin
  update @day2 set sat=@i+1 where fri=1
  set @dtchk=@dtchk+1
 end

else if @intchk=7
 begin
  Set @dtchk=@dtchk
 end

insert into @day2(sun)values(@dtchk+1)
set @dtchk=@dtchk+1
if @intchk=1
 begin
  update @day2 set mon=@i+8,tue=@i+9,wed=@i+10,thu=@i+11,fri=@i+12,sat=@i+13 where sun=@dtchk
  set @dtchk=@dtchk+6
 end
else if @intchk=2
 begin
  update @day2 set mon=@i+7,tue=@i+8,wed=@i+9,thu=@i+10,fri=@i+11,sat=@i+12 where sun=@dtchk
  set @dtchk=@dtchk+6
 end
else if @intchk=3
 begin
  update @day2 set mon=@i+6,tue=@i+7,wed=@i+8,thu=@i+9,fri=@i+10,sat=@i+11 where sun=@dtchk
  set @dtchk=@dtchk+6
 end
else if @intchk=4
 begin
  update @day2 set mon=@i+5,tue=@i+6,wed=@i+7,thu=@i+8,fri=@i+9,sat=@i+10 where sun=@dtchk
  set @dtchk=@dtchk+6
 end
else if @intchk=5
 begin
  update @day2 set mon=@i+4,tue=@i+5,wed=@i+6,thu=@i+7,fri=@i+8,sat=@i+9 where sun=@dtchk
  set @dtchk=@dtchk+6
 end
else if @intchk=6
 begin
  update @day2 set mon=@i+3,tue=@i+4,wed=@i+5,thu=@i+6,fri=@i+7,sat=@i+8 where sun=@dtchk
  set @dtchk=@dtchk+6
 end

else if @intchk=7
 begin
  update @day2 set mon=@i+2,tue=@i+3,wed=@i+4,thu=@i+5,fri=@i+6,sat=@i+7 where sun=@dtchk
  set @dtchk=@dtchk+6
 end
insert into @day2(sun)values(@dtchk+1)
set @dtchk=@dtchk+1
if @intchk=1
 begin
  update @day2 set mon=@i+15,tue=@i+16,wed=@i+17,thu=@i+18,fri=@i+19,sat=@i+20 where sun=@dtchk
  set @dtchk=@dtchk+6
 end
else if @intchk=2
 begin
  update @day2 set mon=@i+14,tue=@i+15,wed=@i+16,thu=@i+17,fri=@i+18,sat=@i+19 where sun=@dtchk
  set @dtchk=@dtchk+6
 end
else if @intchk=3
 begin
  update @day2 set mon=@i+13,tue=@i+14,wed=@i+15,thu=@i+16,fri=@i+17,sat=@i+18 where sun=@dtchk
  
  set @dtchk=@dtchk+6
 end
else if @intchk=4
 begin
  update @day2 set mon=@i+12,tue=@i+13,wed=@i+14,thu=@i+15,fri=@i+16,sat=@i+17 where sun=@dtchk
  set @dtchk=@dtchk+6
 end
else if @intchk=5
 begin
  update @day2 set mon=@i+11,tue=@i+12,wed=@i+13,thu=@i+14,fri=@i+15,sat=@i+16 where sun=@dtchk
  set @dtchk=@dtchk+6
 end
else if @intchk=6
 begin
  update @day2 set mon=@i+10,tue=@i+11,wed=@i+12,thu=@i+13,fri=@i+14,sat=@i+15 where sun=@dtchk
  set @dtchk=@dtchk+6
 end

else if @intchk=7
 begin
  update @day2 set mon=@i+9,tue=@i+10,wed=@i+11,thu=@i+12,fri=@i+13,sat=@i+14 where sun=@dtchk
  set @dtchk=@dtchk+6
 end
insert into @day2(sun)values(@dtchk+1)
set @dtchk=@dtchk+1

if @intchk=1
 begin
  update @day2 set mon=@i+22,tue=@i+23,wed=@i+24,thu=@i+25,fri=@i+26,sat=@i+27 where sun=@dtchk
  set @dtchk=@dtchk+6
 end
else if @intchk=2
 begin
  update @day2 set mon=@i+21,tue=@i+22,wed=@i+23,thu=@i+24,fri=@i+25,sat=@i+26 where sun=@dtchk
  set @dtchk=@dtchk+6
 end
else if @intchk=3
 begin
  update @day2 set mon=@i+20,tue=@i+21,wed=@i+22,thu=@i+23,fri=@i+24,sat=@i+25 where sun=@dtchk
  
  set @dtchk=@dtchk+6
 end
else if @intchk=4
 begin
  update @day2 set mon=@i+19,tue=@i+20,wed=@i+21,thu=@i+22,fri=@i+23,sat=@i+24 where sun=@dtchk
  set @dtchk=@dtchk+6
 end
else if @intchk=5
 begin
  update @day2 set mon=@i+18,tue=@i+19,wed=@i+20,thu=@i+21,fri=@i+22,sat=@i+23 where sun=@dtchk
  set @dtchk=@dtchk+6
 end
else if @intchk=6
 begin
  update @day2 set mon=@i+17,tue=@i+18,wed=@i+19,thu=@i+20,fri=@i+21,sat=@i+22 where sun=@dtchk
  set @dtchk=@dtchk+6
 end

else if @intchk=7
 begin
  update @day2 set mon=@i+16,tue=@i+17,wed=@i+18,thu=@i+19,fri=@i+20,sat=@i+21 where sun=@dtchk
  set @dtchk=@dtchk+6
 end
/*set @i=@i+1
end*/

if @dtchk<>@dnum
 begin
  set @dtchk=@dtchk+1
  insert into @day2(sun)values(@dtchk)
  set @dtval=@dtchk
  
    set @dtchk=@dtchk+1
    if  @dtchk<=@dnum
     begin
      update @day2 set mon =@dtchk where sun=@dtval
      set @dtchk=@dtchk+1
     end
    /*insert into @dd(jk1,jk2,jk3)values(@dtchk,@dtval,@dnum)*/
    if  @dtchk<=@dnum
     begin
      update @day2 set tue=@dtchk where sun=@dtval
      set @dtchk=@dtchk+1
     end
    
    if  @dtchk<=@dnum
     begin
      update @day2 set wed=@dtchk where sun=@dtval
      set @dtchk=@dtchk+1
     end
    
    if  @dtchk<=@dnum
     begin
      update @day2 set thu=@dtchk where sun=@dtval
      set @dtchk=@dtchk+1
     end
    
    if  @dtchk<=@dnum
     begin
      update @day2 set fri=@dtchk where sun=@dtval
      set @dtchk=@dtchk+1
     end
    
    if  @dtchk<=@dnum
     begin
      update @day2 set sat=@dtchk where sun=@dtval
      set @dtchk=@dtchk+1
     end
    if @dtchk<=@dnum
     begin
      insert into @day2(sun)values(@dtchk)
      set @dtval=@dtchk
      set @dtchk=@dtchk+1
      if @dtchk<=@dnum
         begin
       update @day2 set mon=@dtchk where sun=@dtval
         end 
     end
  
 end
insert into @DAY1 select * from @DAY2

return
end

转载于:https://www.cnblogs.com/sanle/archive/2008/08/21/1273063.html

关于日历的一个超级存储过程相关推荐

  1. php 自定义超全局,一个超级简单的 PHP 超全局变量管理扩展

    一个超级简单的 PHP 超全局变量管理扩展(自卖自夸) 介绍 SG 全称 Superglobals,它的诞生为了方便快捷操作 PHP 预定义的超全局变量,用户定义的超全局变量. 如果在非 CLI 模式 ...

  2. 涅槃重生的下一个超级风口:8本书助你赢在未来

    导读:经济学家郎咸平发表文章<剥离货币属性,区块链涅槃重生--疫情下的区块链产业>,文中提到,未来,工业互联网和区块链的结合将是下一个超级风口.随着工业互联网连接规模的扩大,我们迫切需要通 ...

  3. 适合千万数据查询分页操作的一个通用存储过程

    适合千万数据查询分页操作的一个通用存储过程 一.引言                最近上班比较忙,所以就很少写东西了,MongoDB系列的文章也要拖后了,没办法,工作第一,没工作就没饭吃了.今天正好 ...

  4. ShoeBox一个超级好用的图片切割工具

    ShoeBox一个超级好用的图片切割工具 两款图片切割工具 ShoeBox:http://renderhjs.net/shoebox/ BigShear:https://www.fancynode.c ...

  5. 【资源分享】一个超级方便的看电子书方法推荐

    [资源分享]一个超级方便的看电子书方法推荐 1.使用电子书网站 Zlibrary下载各类资源 Zlibrary全球最大数字图书馆,所有图书免费 | Tbox导航 Zlibrary 被称为 全球最大的数 ...

  6. Java一个超级简易的超市管理积分的系统

    第一步我们首先要想清楚思路,比如我们在超市消费的时候是不是每个人都会有一张会员卡,会员卡号都是随机生成的.然后客人凭借这个卡号去消费的时候累计积分 所以第一步我们建立一个关于用户的一个Class类 p ...

  7. 分享一个超级好用的文本编辑器,完爆曾经的np++

    1024G 嵌入式资源大放送!包括但不限于C/C++.单片机.Linux等.关注微信公众号[嵌入式大杂烩],回复1024,即可免费获取! 前言 大家好,我是ZhengN.本次给大家分享一个超级好用的. ...

  8. 一个简单存储过程的测试

    一个简单存储过程的测试 --创建表 create table test001 (id number(4)); --表建好后,创建PROCEDURE CREATE OR REPLACE PROCEDUR ...

  9. 微信正在变成另外一个超级浏览器

    微信账号是一个通行证.在一个用户数量庞大的体系中,腾讯构建起一个封闭式的,相对安全的一站式浏览器.腾讯站在了顶端,控制着这个封闭空间的开关,左下是用户,右下是内容提供者.目前内容的提供主要是腾讯的产品 ...

最新文章

  1. Ubuntu 14.04 64位上安装Valgrind 3.13.0 操作步骤
  2. 人群计数--Single-Image Crowd Counting via Multi-Column Convolutional Neural Network
  3. 《黑客与画家》读后感
  4. 算法每日学打卡:java语言基础题目打卡(01-10)
  5. 基于Kafka+Flink+Redis的电商大屏实时计算案例
  6. javax.servlet.ServletException: 实例化Servlet类[com.wk.web.RegisterServlet]异常
  7. Apache CXF入门
  8. 我决定不能贱卖自己(2010年11月13日志)
  9. 领航机器人广告段子_教育机器人广告宣传语_段子网收录最新段子
  10. 符号库匹配不对的原因_整理了几种离合器打滑的原因,再安装调试时可别弄错了...
  11. [Swift]LeetCode1051.高度检查器 | Height Checker
  12. typedef struct 和 struct 的区别
  13. iOS 新浪微博客户端Demo实践之(六) 微博评论列表页面和发评论
  14. 【mock】数据模板定义规范DTD 数据占位符定义规范DPD
  15. 程序员提高工作效率的方法
  16. Kalman Fuzzy Actor-Critic Learning Automaton Algorithm for the Pursuit-Evasion Differential Game
  17. VC++ 6.0实现视频捕捉(VFW)
  18. veu中高德怎么定义多个marker?
  19. python中的headers是什么意思_Python设置Headers
  20. linux 关闭桌面休眠,Linux关闭休眠和屏保模式

热门文章

  1. Centos 6安装完美搭建mysql、php、apache之旅
  2. CentOS 安装JDK跟TOMCAT
  3. 转:SQL Server 2005安装过程图解
  4. 【分享】Java软件架构师所要需的东西 (希望对很多迷茫的朋友指个路)
  5. Linux用户管理详解大结局(下)
  6. css3中的渐变效果及花斑动画的实现
  7. android给图片加边框,Android学习笔记进阶19之给图片加边框
  8. devops工程师_如何过渡为DevOps工程师的职业
  9. emacs latex_如何使用Emacs创建LaTeX文档
  10. 开源硬件_扩展对开源硬件的访问