Fooo

 

/*
sql 语句如何插入N 年 每天日期
by 2013-3-11
生成表 SQL:
CREATE TABLE [dbo].[Tts_System_Date](
    [SD_Date] [smalldatetime] NOT NULL,
 CONSTRAINT [PK_Tts_System_Date] PRIMARY KEY CLUSTERED 
(
    [SD_Date] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

*/
declare @i datetime
declare @today smalldatetime
declare @year int,@y int
set @y =1 -- 起始1年
set @i= dateadd(d,1-datepart(dy,getdate()),getdate())

----开始遍历N年
while @y <=2  --如果@y=2,即插入2年日期记录
begin
--当前年份的当天
set @i= CONVERT(varchar(100), dateadd(yy, 0,@i), 23) 
set @year=datepart(yy,@i)

----开始遍历当前年份
while(datepart(yy,@i) = @year)
begin 
   --当天
   set @today = CONVERT(varchar(100), dateadd(d,1,@i), 23)
   --判断当天日期是否存在
   if not exists(select top(1) SD_Date from Tts_System_Date where SD_Date=@today or  SD_Date=@i )
     begin  
      insert into Tts_System_Date values(@i) 
      set @i=@today  
     end
   else
     begin  
        set @i=@today   
     end

end
----结束遍历当前年份

set @y  = @y  + 1
end
----结束遍历N年

select * from Tts_System_Date
----delete from Tts_System_Date

 

分类:

技术点:

相关文章:

  • 2022-01-19
  • 2021-12-04
  • 2021-12-02
  • 2021-11-30
  • 2021-08-11
  • 2022-12-23
猜你喜欢
  • 2021-12-06
  • 2022-12-23
  • 2021-11-23
  • 2021-11-17
  • 2021-11-19
  • 2021-10-13
相关资源
相似解决方案