日期:2014-05-16  浏览次数:20487 次

存储过程实现 日期统计期间

CREATE TABLE [dbo].[t_kjqj] (
?[nf] [numeric](4, 0) NOT NULL ,
?[yf] [numeric](4, 0) NOT NULL ,
?[rq1] [smalldatetime] NOT NULL ,
?[rq2] [smalldatetime] NOT NULL ,
?[qybz] [varchar] (1) COLLATE Chinese_PRC_CI_AS NULL
)

?

?

?

?

CREATE? PROCEDURE [dbo].[p_newkjqj] AS
declare
?@li_maxnf integer,
?@li_nf integer,
?@li_yf integer,
?@ldt_1 datetime,
?@ldt_2 datetime
? select @li_maxnf=max(nf) from t_kjqj
? select @li_nf=@li_maxnf+1,@li_yf=1
? select @ldt_1=convert(datetime, convert(varchar(10),dateadd(day,1,rq2),111)) from t_kjqj where nf=@li_maxnf and yf=12

? while @li_yf < 13
? begin
??? select @ldt_2=dateadd(day,-1,dateadd(month,1,@ldt_1))
??? insert into t_kjqj(nf,yf,rq1,rq2) values(@li_nf,@li_yf,@ldt_1,@ldt_2)
??? select @li_yf=@li_yf+1,@ldt_1=dateadd(month,1,@ldt_1)
? end

GO

?

生成如下数据:

?2001?1?2001-1-1?2001-1-31?0
?2001?2?2001-2-1?2001-2-28?0
?2001?3?2001-3-1?2001-3-31?0
?2001?4?2001-4-1?2001-4-30?0
?2001?5?2001-5-1?2001-5-31?0
?2001?6?2001-6-1?2001-6-30?0
?2001?7?2001-7-1?2001-7-31?0
?2001?8?2001-8-1?2001-8-31?0
?2001?9?2001-9-1?2001-9-30?0
?2001?10?2001-10-1?2001-10-31?0
?2001?11?2001-11-1?2001-11-30?0
?2001?12?2001-12-1?2001-12-31?0