使用SQL Server表中的序号填充主ID列

时间:2012-09-24 11:46:05

标签: sql sql-server case dateadd

我浏览过论坛,找不到任何符合我问题的内容。此查询应该为员工添加定期假期。如果我排除PL_ID变量并且只有一个没有主键的表,则代码会在case语句中定义的正确的日期假期中添加,这是在测试表上,但需要添加到包含超过25,000个条目的表中。 / p>

该表包括PL_ID,Log_Date,Out_Time,In_Time

我目前有这段代码:

DECLARE @StartDate DateTime
DECLARE @EndDate DateTime
DECLARE @DateVar DateTime
DECLARE @PL_ID int

SET @StartDate = '1/10/2012'
SET @EndDate = '1/11/2012'
SET @DateVar = @StartDate
SET @PL_ID = 0

WHILE @DateVar <= @EndDate

BEGIN 
 INSERT INTO Logger_test2(PL_ID,Log_Date,In_Time,Out_Time)
 SELECT @PL_ID,@DateVar,
        CASE WHEN DATENAME(dw,@DateVar) = 'Thursday' THEN '9'
             WHEN DATENAME(dw,@DateVar) = 'Monday' THEN '12'
         END AS In_Time,
        CASE WHEN DATENAME(dw,@DateVar) = 'Thursday' THEN 'After 5'
             WHEN DATENAME(dw,@DateVar) = 'Monday' THEN 'After 5'
         END AS Out_Time      
  WHERE DATENAME(dw,@DateVar) IN ('Thursday','Monday')

 SET @DateVar = DATEADD(d,1,@DateVar)
 UPDATE Logger_test2 set @PL_ID = PL_ID = @PL_ID +1
 END

使用此代码,它只会在具有最大PL_ID的表中添加一行,但我需要它为每个条目创建一个新ID,抱歉它有点笨拙。非常感谢你的帮助!

离现在更近了!但是,如果我在获得重复键错误后将日期更改为下个月:Msg 2627,Level 14,State 1,Line 15违反PRIMARY KEY约束'PK_Logger_test2'。无法在对象'dbo.Logger_test2'中插入重复键。重复的键值是(4) - 是否有另一种解决方法,以便它不会尝试插入重复的键?

解决: 这是完成的代码:),工作了一个魅力,让我的生活更轻松!仅仅通过更改用户ID就可以输入每个人预先计划好的明年假期:

DECLARE @StartDate DateTime
DECLARE @EndDate DateTime
DECLARE @DateVar DateTime
DECLARE @PL_ID int
DECLARE @User_ID int
DECLARE @Reason nvarchar(50)
DECLARE @Details nvarchar (500)

SET @StartDate = '30/09/2012'
SET @EndDate = '24/12/2012'
SET @DateVar = @StartDate
SET @User_ID = 55
SET @Reason = 'Day off'
SET @Details = 'Day off' 

--SET @PL_ID = 0
SELECT @PL_ID = max(PL_ID) from People_Logger

WHILE @DateVar <= @EndDate

BEGIN
 select @PL_ID = @PL_ID +1
 SET IDENTITY_INSERT dbo.People_logger ON;
 INSERT INTO People_Logger(PL_ID,LOG_Date,User_ID,Reason,Details,In_Time,Out_Time)
 SELECT @PL_ID,@DateVar,@User_ID,@Reason,@Details,
        CASE WHEN DATENAME(dw,@DateVar) = 'Thursday' THEN 'After 5'
             WHEN DATENAME(dw,@DateVar) = 'Friday' THEN 'After 5'
         END AS In_Time,
        CASE WHEN DATENAME(dw,@DateVar) = 'Thursday' THEN '9'
             WHEN DATENAME(dw,@DateVar) = 'Friday' THEN '9'
         END AS Out_Time
  WHERE DATENAME(dw,@DateVar) IN ('Thursday','Friday')
SET IDENTITY_INSERT dbo.People_Logger OFF; 
 SET @DateVar = DATEADD(d,1,@DateVar)
END

2 个答案:

答案 0 :(得分:1)

好的我认为您只需要在插入之前增加变量的值。喜欢这个

WHILE @DateVar <= @EndDate
BEGIN

  select @PL_ID = @PL_ID + 1

  INSERT INTO Logger_test2(PL_ID,Log_Date,In_Time,Out_Time)
  SELECT @PL_ID,@DateVar,
  etc etc

  Set @DateVar= DATEADD(d,1,@DateVar)
  -- NO Update statement
END

答案 1 :(得分:0)

DECLARE @StartDate DateTime
DECLARE @EndDate DateTime
DECLARE @DateVar DateTime
DECLARE @PL_ID int

SET @StartDate = '1/10/2012'
SET @EndDate = '1/11/2012'
SET @DateVar = @StartDate
SET @PL_ID = 0

WHILE @DateVar <= @EndDate

BEGIN 
 INSERT INTO Logger_test2(PL_ID,Log_Date,In_Time,Out_Time)
 SELECT @PL_ID,@DateVar,
        CASE WHEN DATENAME(dw,@DateVar) = 'Thursday' THEN '9'
             WHEN DATENAME(dw,@DateVar) = 'Monday' THEN '12'
         END AS In_Time,
        CASE WHEN DATENAME(dw,@DateVar) = 'Thursday' THEN 'After 5'
             WHEN DATENAME(dw,@DateVar) = 'Monday' THEN 'After 5'
         END AS Out_Time      
  WHERE DATENAME(dw,@DateVar) IN ('Thursday','Monday')

 SET @DateVar = DATEADD(d,1,@DateVar)
 SET @PL_ID = @PL_ID +1
 END