为了公司考勤系统的需要

编写的几个简单存储过程(可以手动运行,也可以设置事务自动运行!感觉还行比较通用,写出来共享下)

Calendar表结构很简单,2个字段:

fdDate 日期
fdType 考勤类型(工作日N,周末W,节假日H[需要根据需要自己修改])

--判断一段时间范围内的工作日(N)和周末(W)

Create     PROCEDURE [dbo].[NewMonthWeekDay_Calendar]
@sdate smalldatetime,
@edate smalldatetime
AS
declare @fdDate smalldatetime
declare @WeekDay varchar(20)
declare cr0 cursor for
 select fdDate from calendar where fddate>=@sdate and fddate<=@edate
open cr0
fetch next from cr0 into @fdDate
 while @@fetch_status=0
 begin
  if (datename(weekday,@fdDate)='星期一')
   update calendar set fdType='N' where fdDate=@fdDate
  if (datename(weekday,@fdDate)='星期二')
   update calendar set fdType='N' where fdDate=@fdDate
  if (datename(weekday,@fdDate)='星期三')
   update calendar set fdType='N' where fdDate=@fdDate
  if (datename(weekday,@fdDate)='星期四')
   update calendar set fdType='N' where fdDate=@fdDate
  if (datename(weekday,@fdDate)='星期五')
   update calendar set fdType='N' where fdDate=@fdDate
  if (datename(weekday,@fdDate)='星期六')
   update calendar set fdType='W' where fdDate=@fdDate
  if (datename(weekday,@fdDate)='星期日')
   update calendar set fdType='W' where fdDate=@fdDate
  
  fetch next from cr0 into @fdDate

end
close cr0
deallocate cr0

--根据年和月自动插入Calendar表新日期数据

Create    PROCEDURE [dbo].[NewMonth_Calendar]
@Year int,
@Month int
AS

insert into cas..calendar(fdDate)
 Select Convert(Varchar(10), DateAdd(dd, ID, Cast(Rtrim(@Year) + '-' + Rtrim(@Month) + '-' + '01' As DateTime)), 120) as fdDate
 From (SELECT TOP 50 ID=number FROM  MASTER..SPT_VALUES WHERE TYPE='P' )T
 Where T.ID < DateDiff(dd, Cast(Rtrim(@Year) + '-' + Rtrim(@Month) + '-' + '01' As DateTime),
 DateAdd(mm, 1, Cast(Rtrim(@Year) + '-' + Rtrim(@Month) + '-' + '01' As DateTime)))

/*

--sql2000可以使用这个
 Select TOP 50 ID = Identity(Int, 0, 1) Into #T From SysColumns
 insert into cas..calendar(fdDate)
 Select Convert(Varchar(10), DateAdd(dd, ID, Cast(Rtrim(@Year) + '-' + Rtrim(@Month) + '-' + '01' As DateTime)), 120) From #T
 Where ID < DateDiff(dd, Cast(Rtrim(@Year) + '-' + Rtrim(@Month) + '-' + '01' As DateTime), DateAdd(mm, 1, Cast(Rtrim(@Year) + '-' + Rtrim(@Month) + '-' + '01' As DateTime)))
 Drop Table #T

*/

--插入日历表全年的数据,并自动判断工作日(N)周末(W)

Create   PROCEDURE [dbo].[NewYearInsrt]
@Year int
AS
if(@Year>2008)
begin
 exec NewMonth_Calendar @Year,1
 exec NewMonth_Calendar @Year,2
 exec NewMonth_Calendar @Year,3
 exec NewMonth_Calendar @Year,4
 exec NewMonth_Calendar @Year,5
 exec NewMonth_Calendar @Year,6
 exec NewMonth_Calendar @Year,7
 exec NewMonth_Calendar @Year,8
 exec NewMonth_Calendar @Year,9
 exec NewMonth_Calendar @Year,10
 exec NewMonth_Calendar @Year,11
 exec NewMonth_Calendar @Year,12
declare @sdate smalldatetime
declare @edate smalldatetime
set @sdate=cast(cast(@Year as varchar(4))+'-1-1' as smalldatetime)
set @edate=cast(cast(@Year as varchar(4))+'-12-31' as smalldatetime)
 exec NewMonthWeekDay_Calendar @sdate,@edate
end

--直接运行 EXEC [dbo].[NewYearInsrt] 2010 即可

SQL日历表数据的简单生成相关推荐

  1. sql 生成csv数据_创建包含SQL Server数据的动态生成的CSV文件

    sql 生成csv数据 介绍 ( Introduction ) A few months back, I presented a paper at SQL Saturday 327 in Johann ...

  2. 实验四 数据查询——简单查询 Sql Server数据库实验

    实验四  数据查询--简单查询   一.实验目的 1.掌握SQL查询语句的基本概念  2.掌握SQLServer查询语句的基本语法 3.熟练使用SQL的Select语句对单表进行查询 4.熟练掌握并运 ...

  3. 用sql语句快速生成大量数据,批量生成数据

    用sql语句快速生成大量数据,批量生成数据 1.首先先建两个表 – 创建一个临时内存表 set global log_bin_trust_function_creators=1; DROP TABLE ...

  4. SQL Server 2008 R2如何生成带数据的数据库脚本

    1.对想要复制的数据库右键,"任务","生成脚本" 2.下面需要注意的是,默认情况下,只会生成仅架构的脚本,也就是说仅仅有表结构,而没有数据的空壳.所以需要额外 ...

  5. mysql 列数据显示转成行数据显示_Mysql的列修改成行并显示数据的简单实现

    创建测试表: DROP TABLE IF EXISTS `test`; CREATE TABLE `test` ( `year` int(11) DEFAULT NULL, `month` int(1 ...

  6. 学习SQL:SQL Server数据透视表

    In the previous few articles in this series, we've set the foundations on how to create a report. We ...

  7. 使用SQL Server数据工具进行SQL单元测试

    This article on SQL Unit Testing is the second part on the series about SSDT and database developmen ...

  8. 使用SQL Server数据工具和Visual Studio Online进行连续部署

    In the previous posts 在以前的帖子中 Deployment to several databases using SQL Server Data Tools and TFS us ...

  9. 数据权限简单设计思路

    数据权限简单设计思路 现有权限控制分类 数据权限控制 服务架构流程 权限字段的注册 权限字段授权 权限字段鉴权 前端界面适配 动态渲染数据字段 开发注意事项及思考 权限合并 权限的叠加 规则的叠加 S ...

  10. 《MySQL技术》学习笔记——使用SQL管理数据

    MySQL技术内幕--使用SQL管理数据 使用SQL管理数据 服务器的SQL模式 MySQL的标识符语法和命名规则 SQL语句的大小写规则 字符集支持 指定字符集 确定可用字符集和当前设置 Unico ...

最新文章

  1. hibernate多个主键
  2. MIUI应用权限设置
  3. 阿里云API网关(14)流控策略
  4. TIOBE 2011年7月编程语言排行榜:Objective-C成为年度语言
  5. 安卓设置菊花动画_Android仿ios加载loading菊花图效果
  6. POJ 1014 Dividing(多重背包 + 倍增优化)
  7. php 获取js对象的属性值,js获取对象,数组所有属性键值(key)和对应值(value)的方法示例...
  8. 深圳市收运体系运营管理_华为公司质量管理体系构建和运营实践
  9. canvas一些属性
  10. 2014蓝桥杯C++A:猜年龄;扑克序列(全排列)
  11. Linux下,Pycharm到期,源不好使,无法安装pyqt5及pyqy5-tools的解决办法
  12. 台式安装nas系统_个人云盘搭建延伸二:我也可以搭建NAS!黑群辉系统安装配置及测试...
  13. java并发编程(1)--线程 可见性 volatile怎么用
  14. 如何使用Mobile_Detect来判断访问网站的设备:安卓,平板,电脑
  15. 使用图形编辑框架GEF创建基于Eclipse的应用程序
  16. 关闭阿里云的短信提醒
  17. 智能渠道商分销系统开发方案:打通协作壁垒,实现渠道商数字管理
  18. 【渝粤题库】陕西师范大学180210 国际市场营销学 作业
  19. 计算机上面的字母代表什么意思,电脑主板上面的字母型号是什么意思
  20. 大治河西枢纽二线船闸总体设计(水利设计资料)

热门文章

  1. 亚信科技外包_外包到亚信---转正疑问 - 菜鸟@大虾的个人空间 - 51Testing软件测试网 51Testing软件测试网-软件测试人的精神家园...
  2. python 去除txt文本内容重复值
  3. Python语法都会,一写程序就懵,有解么?
  4. 数据库系统概论第五版 课后习题答案王珊
  5. eclispe 下载与安装
  6. 《啊哈算法》知识点总结
  7. eds能谱图分析实例_SPC控制图公式_均值极差SPC控制图公式应用实例分析
  8. 智能手机玩转Smart3D三维建模介绍
  9. [Win+RF]新人视角-快速上手RF的接口测试
  10. 小米路由pro php,小米路由器开启frp