SQL日历表数据的简单生成
为了公司考勤系统的需要
编写的几个简单存储过程(可以手动运行,也可以设置事务自动运行!感觉还行比较通用,写出来共享下)
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日历表数据的简单生成相关推荐
- sql 生成csv数据_创建包含SQL Server数据的动态生成的CSV文件
sql 生成csv数据 介绍 ( Introduction ) A few months back, I presented a paper at SQL Saturday 327 in Johann ...
- 实验四 数据查询——简单查询 Sql Server数据库实验
实验四 数据查询--简单查询 一.实验目的 1.掌握SQL查询语句的基本概念 2.掌握SQLServer查询语句的基本语法 3.熟练使用SQL的Select语句对单表进行查询 4.熟练掌握并运 ...
- 用sql语句快速生成大量数据,批量生成数据
用sql语句快速生成大量数据,批量生成数据 1.首先先建两个表 – 创建一个临时内存表 set global log_bin_trust_function_creators=1; DROP TABLE ...
- SQL Server 2008 R2如何生成带数据的数据库脚本
1.对想要复制的数据库右键,"任务","生成脚本" 2.下面需要注意的是,默认情况下,只会生成仅架构的脚本,也就是说仅仅有表结构,而没有数据的空壳.所以需要额外 ...
- mysql 列数据显示转成行数据显示_Mysql的列修改成行并显示数据的简单实现
创建测试表: DROP TABLE IF EXISTS `test`; CREATE TABLE `test` ( `year` int(11) DEFAULT NULL, `month` int(1 ...
- 学习SQL:SQL Server数据透视表
In the previous few articles in this series, we've set the foundations on how to create a report. We ...
- 使用SQL Server数据工具进行SQL单元测试
This article on SQL Unit Testing is the second part on the series about SSDT and database developmen ...
- 使用SQL Server数据工具和Visual Studio Online进行连续部署
In the previous posts 在以前的帖子中 Deployment to several databases using SQL Server Data Tools and TFS us ...
- 数据权限简单设计思路
数据权限简单设计思路 现有权限控制分类 数据权限控制 服务架构流程 权限字段的注册 权限字段授权 权限字段鉴权 前端界面适配 动态渲染数据字段 开发注意事项及思考 权限合并 权限的叠加 规则的叠加 S ...
- 《MySQL技术》学习笔记——使用SQL管理数据
MySQL技术内幕--使用SQL管理数据 使用SQL管理数据 服务器的SQL模式 MySQL的标识符语法和命名规则 SQL语句的大小写规则 字符集支持 指定字符集 确定可用字符集和当前设置 Unico ...
最新文章
- hibernate多个主键
- MIUI应用权限设置
- 阿里云API网关(14)流控策略
- TIOBE 2011年7月编程语言排行榜:Objective-C成为年度语言
- 安卓设置菊花动画_Android仿ios加载loading菊花图效果
- POJ 1014 Dividing(多重背包 + 倍增优化)
- php 获取js对象的属性值,js获取对象,数组所有属性键值(key)和对应值(value)的方法示例...
- 深圳市收运体系运营管理_华为公司质量管理体系构建和运营实践
- canvas一些属性
- 2014蓝桥杯C++A:猜年龄;扑克序列(全排列)
- Linux下,Pycharm到期,源不好使,无法安装pyqt5及pyqy5-tools的解决办法
- 台式安装nas系统_个人云盘搭建延伸二:我也可以搭建NAS!黑群辉系统安装配置及测试...
- java并发编程(1)--线程 可见性 volatile怎么用
- 如何使用Mobile_Detect来判断访问网站的设备:安卓,平板,电脑
- 使用图形编辑框架GEF创建基于Eclipse的应用程序
- 关闭阿里云的短信提醒
- 智能渠道商分销系统开发方案:打通协作壁垒,实现渠道商数字管理
- 【渝粤题库】陕西师范大学180210 国际市场营销学 作业
- 计算机上面的字母代表什么意思,电脑主板上面的字母型号是什么意思
- 大治河西枢纽二线船闸总体设计(水利设计资料)
热门文章
- 亚信科技外包_外包到亚信---转正疑问 - 菜鸟@大虾的个人空间 - 51Testing软件测试网 51Testing软件测试网-软件测试人的精神家园...
- python 去除txt文本内容重复值
- Python语法都会,一写程序就懵,有解么?
- 数据库系统概论第五版 课后习题答案王珊
- eclispe 下载与安装
- 《啊哈算法》知识点总结
- eds能谱图分析实例_SPC控制图公式_均值极差SPC控制图公式应用实例分析
- 智能手机玩转Smart3D三维建模介绍
- [Win+RF]新人视角-快速上手RF的接口测试
- 小米路由pro php,小米路由器开启frp