好久没上bbs.54master了。有一朋友问了一条有关通过SQL语句自动生成打卡问题。
原问题:姓名 第一次打卡时间 第二次打卡时间 日期
A 8:01 17:01 07-01-01
B 8:04 17:02 07-01-01
A 8:03 17:07 07-01-02
.............
我要做一份假的考勤记录,需批量更新考勤打卡时间, 如果我需要更改07年1月份的所有人员第一,
二次打卡时间,且产生虚似随机的一个打卡时间,第一次打卡时间范围为8:00~8:10 第二次时间范围为17:00~17:10之间
/*
功能:按某一段时间,自动生成员工的打卡记录
设计:OK_008(wgh)
时间:2007-04-14
*/
-- 人事表
CREATE TABLE #Employee(EmployeeID int ,ChineseName nvarchar ( 20 ))
-- 考勤表
CREATE TABLE #OnOffDuty(EmployeeID int ,Check1 datetime ,Check2 datetime ,CheckDate datetime )
-- 虚拟人事信息
INSERT INTO #Employee
SELECT 1 , ' 张小名 ' UNION ALL
SELECT 2 , ' 黄大 ' UNION ALL
SELECT 3 , ' 李兵 ' UNION ALL
SELECT 4 , ' 刘达 ' UNION ALL
SELECT 5 , ' 张雷 ' UNION ALL
SELECT 6 , ' 郭小妹 '
/*
自动生成2007年1月份的考勤打卡时间
方法:
使用游标按员工再按日期生成打卡记录
*/
DECLARE @BeginDate datetime
, @EndDate datetime
, @CheckDate datetime
, @EmployeeID int
, @EXECUTE_SQL nvarchar ( 4000 )
, @For int
SELECT @BeginDate = ' 2007-01-01 ' , @EndDate = ' 2007-01-31 ' , @EXECUTE_SQL = '' , @For = 0
DECLARE CursorEmployee CURSOR FOR
SELECT EmployeeID FROM #Employee
OPEN CursorEmployee
FETCH NEXT FROM CursorEmployee INTO @EmployeeID
WHILE @@FETCH_STATUS = 0
BEGIN
SET @CheckDate = @BeginDate
WHILE ( @CheckDate <= @EndDate )
BEGIN
-- 根据实际调整RAND()的值范围
SELECT @EXECUTE_SQL = @EXECUTE_SQL + ' INSERT INTO #OnOffDuty SELECT '
+ CAST ( @EmployeeID AS nvarchar ( 20 )) + ' , '''
+ CONVERT ( nchar ( 10 ), @CheckDate , 121 ) + ' 08:0 ' + SUBSTRING ( CAST ( RAND () AS nvarchar ( 20 )), 3 , 1 ) + ''' , '''
+ CONVERT ( nchar ( 10 ), @CheckDate , 121 ) + ' 17:0 ' + SUBSTRING ( CAST ( RAND () AS nvarchar ( 20 )), 3 , 1 ) + ''' , '''
+ CONVERT ( nchar ( 10 ), @CheckDate , 121 ) + '''' + CHAR ( 10 )
, @For = @For + 1
IF ( @For % 40 ) = 0 -- 每40行保存一次,根据实际的@EXECUTE_SQL长度来调试多少行。
BEGIN
EXECUTE ( @EXECUTE_SQL )
SET @EXECUTE_SQL = ''
END
SET @CheckDate = DATEADD ( day , 1 , @CheckDate )
END
FETCH NEXT FROM CursorEmployee INTO @EmployeeID
END
CLOSE CursorEmployee
DEALLOCATE CursorEmployee
IF ( @EXECUTE_SQL <> '' ) EXECUTE ( @EXECUTE_SQL ) -- 判断@EXECUTE_SQL是否已经执行完成
-- 查询结果:
SELECT B.ChineseName,A.Check1,A.Check2,A.CheckDate FROM #OnOffDuty A
INNER JOIN #Employee B ON B.EmployeeID = A.EmployeeID
ORDER BY A.EmployeeID,A.CheckDate
DROP TABLE #Employee,#OnOffDuty
功能:按某一段时间,自动生成员工的打卡记录
设计:OK_008(wgh)
时间:2007-04-14
*/
-- 人事表
CREATE TABLE #Employee(EmployeeID int ,ChineseName nvarchar ( 20 ))
-- 考勤表
CREATE TABLE #OnOffDuty(EmployeeID int ,Check1 datetime ,Check2 datetime ,CheckDate datetime )
-- 虚拟人事信息
INSERT INTO #Employee
SELECT 1 , ' 张小名 ' UNION ALL
SELECT 2 , ' 黄大 ' UNION ALL
SELECT 3 , ' 李兵 ' UNION ALL
SELECT 4 , ' 刘达 ' UNION ALL
SELECT 5 , ' 张雷 ' UNION ALL
SELECT 6 , ' 郭小妹 '
/*
自动生成2007年1月份的考勤打卡时间
方法:
使用游标按员工再按日期生成打卡记录
*/
DECLARE @BeginDate datetime
, @EndDate datetime
, @CheckDate datetime
, @EmployeeID int
, @EXECUTE_SQL nvarchar ( 4000 )
, @For int
SELECT @BeginDate = ' 2007-01-01 ' , @EndDate = ' 2007-01-31 ' , @EXECUTE_SQL = '' , @For = 0
DECLARE CursorEmployee CURSOR FOR
SELECT EmployeeID FROM #Employee
OPEN CursorEmployee
FETCH NEXT FROM CursorEmployee INTO @EmployeeID
WHILE @@FETCH_STATUS = 0
BEGIN
SET @CheckDate = @BeginDate
WHILE ( @CheckDate <= @EndDate )
BEGIN
-- 根据实际调整RAND()的值范围
SELECT @EXECUTE_SQL = @EXECUTE_SQL + ' INSERT INTO #OnOffDuty SELECT '
+ CAST ( @EmployeeID AS nvarchar ( 20 )) + ' , '''
+ CONVERT ( nchar ( 10 ), @CheckDate , 121 ) + ' 08:0 ' + SUBSTRING ( CAST ( RAND () AS nvarchar ( 20 )), 3 , 1 ) + ''' , '''
+ CONVERT ( nchar ( 10 ), @CheckDate , 121 ) + ' 17:0 ' + SUBSTRING ( CAST ( RAND () AS nvarchar ( 20 )), 3 , 1 ) + ''' , '''
+ CONVERT ( nchar ( 10 ), @CheckDate , 121 ) + '''' + CHAR ( 10 )
, @For = @For + 1
IF ( @For % 40 ) = 0 -- 每40行保存一次,根据实际的@EXECUTE_SQL长度来调试多少行。
BEGIN
EXECUTE ( @EXECUTE_SQL )
SET @EXECUTE_SQL = ''
END
SET @CheckDate = DATEADD ( day , 1 , @CheckDate )
END
FETCH NEXT FROM CursorEmployee INTO @EmployeeID
END
CLOSE CursorEmployee
DEALLOCATE CursorEmployee
IF ( @EXECUTE_SQL <> '' ) EXECUTE ( @EXECUTE_SQL ) -- 判断@EXECUTE_SQL是否已经执行完成
-- 查询结果:
SELECT B.ChineseName,A.Check1,A.Check2,A.CheckDate FROM #OnOffDuty A
INNER JOIN #Employee B ON B.EmployeeID = A.EmployeeID
ORDER BY A.EmployeeID,A.CheckDate
DROP TABLE #Employee,#OnOffDuty