日期:2014-05-18 浏览次数:20650 次
USE tempdb; GO IF OBJECT_ID('furlough') IS NOT NULL DROP TABLE furlough; GO /*创建节日表*/ CREATE TABLE furlough (id INT IDENTITY(1,1),--节日ID fname VARCHAR(20) ,--节日名称 FurloughStarttime DATETIME,--开始放假时间 FurloughEndtime DATETIME, --结束放假时间 updatetime datetime --更新时间 ); -- 添加数据 INSERT INTO dbo.furlough VALUES ('元旦','20120101','20120103',GETDATE()); INSERT INTO dbo.furlough VALUES ('春节','20120122','20120128',GETDATE()); INSERT INTO dbo.furlough VALUES ('清明节','20120402','20120404',GETDATE()); INSERT INTO dbo.furlough VALUES ('劳动节','20120429','20120501',GETDATE()); INSERT INTO dbo.furlough VALUES ('端午节','20120622','20120624',GETDATE()); INSERT INTO dbo.furlough VALUES ('国庆节','20120930','20121007',GETDATE()); INSERT INTO dbo.furlough VALUES ('元旦','20121230','20130101',GETDATE()); INSERT INTO dbo.furlough VALUES ('春节','20130209','20130215',GETDATE()); INSERT INTO dbo.furlough VALUES ('清明节','20130404','20130406',GETDATE()); INSERT INTO dbo.furlough VALUES ('劳动节','20130429','20130501',GETDATE()); INSERT INTO dbo.furlough VALUES ('端午节','20130610','20130612',GETDATE()); INSERT INTO dbo.furlough VALUES ('中秋节','20130919','20120921',GETDATE()); INSERT INTO dbo.furlough VALUES ('国庆节','20131001','20131007',GETDATE()); GO IF TYPE_ID('tyWorkday') IS NOT NULL DROP TYPE tyWorkday; GO /*工作日日期*/ CREATE TYPE tyWorkday AS TABLE (Id INT IDENTITY(1,1), Workday DATETIME);
IF OBJECT_ID('fnWorkday') IS NOT NULL DROP FUNCTION fnWorkday; GO CREATE FUNCTION fnWorkday (@date INT , @n INT ) RETURNS DATETIME AS BEGIN DECLARE @m INT ,--增长的天数 @d INT ,--当月的天数 @wdate VARCHAR(10), --工作日期 @tyw AS tyWorkday, @workday DATETIME --需要返回的工作日期 SET @m=1; SET @wdate=@date*100+@m; SET @d=DATEDIFF(DAY,@wdate,DATEADD(MONTH,1,@wdate )); WHILE @m<=@d BEGIN SET @wdate=@date*100+@m; /*当日期不是周六、周日的时候插入到@tyw表*/ IF DATEPART(dw,@wdate) NOT IN (1,7) BEGIN INSERT INTO @tyw VALUES(@wdate); END; SET @m=@m+1; END; SELECT @workday=Workday FROM @tyw WHERE Id=@n; RETURN @workday; END;