自动生成10位ID

DECLARE @num INT
DECLARE @strID NVARCHAR(20)

SET @strID = '00000000000000000' + cast(@num AS NVARCHAR(10))
SET @strID = RIGHT(@strID, 10)

 

Date的运算

DECLARE @StartDate DATETIME

SET @StartDate = dateadd(day, 5, GetDate()) --add day
PRINT @StartDate

SET @StartDate = dateadd(hh, 5, GetDate()) --add hour
PRINT @StartDate

DATEADD() 函数在日期中添加或减去指定的时间间隔。
DATEADD(datepart,number,date)

datepart 缩写
年 yy, yyyy
季度 qq, q
月 mm, m
年中的日 dy, y
日 dd, d
周 wk, ww
星期 dw, w
小时 hh
分钟 mi, n
秒 ss, s
毫秒 ms
微妙 mcs
纳秒 ns

 

DECLARE @num INT
DECLARE @strID NVARCHAR(20) 

--DELETE 
DELETE FROM [dbo].[Banner]

SET @num = 0
WHILE @num < 10
BEGIN

    SET @strID = '00000000000000000' + cast(@num AS NVARCHAR(10))
    SET @strID = RIGHT(@strID, 10)
    --print   RIGHT( @strID,10) 

    --INSERT
    INSERT INTO [dbo].[Banner] VALUES (
        @strID
        ,@strID + '.jpg'
        ,''
        ,'1'
        ,@num
        ,getdate()
        ,getdate()
        ,getdate()
        ,'false'
        )

    SET @num = @num + 1
END
View Code
DECLARE @num INT
DECLARE @strID NVARCHAR(20) 
DECLARE @StartDate DateTime

--DELETE 
DELETE FROM [dbo].[Event]

SET @num = 0
WHILE @num < 10
BEGIN

    SET @strID = '00000000000000000' + cast(@num AS NVARCHAR(10))
    SET @strID = RIGHT(@strID, 10)
    --print   RIGHT( @strID,10) 

    SET @StartDate = dateadd(day,@num, GetDate()) --add day
    --print   @StartDate

    --print dateadd(hh,1,getdate()) --add hour

    --INSERT
    INSERT INTO [dbo].[Event] VALUES
           (@strID
           ,'Title '+@strID
           ,'Description '+@strID
           ,@strID+'.jpg'
           ,@StartDate
           ,dateadd(hh,3,@StartDate) 
           ,'Venue ' +@strID
           ,'1'
           ,'Creator'
           ,''
           ,'20'
           ,GetDate()
           ,GetDate()
           ,GetDate()
           ,'false')

    SET @num = @num + 1
END
View Code

 

posted on 2015-12-29 11:40  QZB  阅读(187)  评论(0编辑  收藏  举报