如何插入带序列号的多键表格

时间:2011-07-18 23:29:10

标签: sql bulkinsert

我想为每个唯一的ID_COST_CENTRE,NM_ROSTER在下表中插入一个新任务,并将No_Order_Number的值设置为MAX(No_Order_Number)+1。

我可以设置NM_TASK,TY_TASK& ID_TASK_TYPE值

INSERT INTO [Microster].[dbo].[MIC_CC_Task_Edit]
   ([ID_COST_CENTRE],[NM_ROSTER],[NM_TASK],[No_Order_Number],[TY_TASK],[ID_TASK_TYPE])
   VALUES
   (<ID_COST_CENTRE, char(15),>
   ,<NM_ROSTER, char(25),>
   ,<NM_TASK, char(15),>
   ,<No_Order_Number, smallint,>
   ,<TY_TASK, smallint,>
   ,<ID_TASK_TYPE, char(15),>)

例如,我想将任务AL添加到以下角度,得到以下结果。

0006    0006 ADMIN              CLERICAL ASSIST 1   1   Worked Task 
0006    0006 ADMIN          COORD       2   1   Worked Task 
0006    0006 ADMIN          MRT             3   1   Worked Task 
0006    0006 ADMIN          ORIENT      4   1   Worked Task 
0006    0006 MRT'S          CLERICAL ASSIST 1   1   Worked Task 
0006    0006 MRT'S          COORD       2   1   Worked Task 
0006    0006 MRT'S          MRT             3   1   Worked Task 
0006    0006 SONOGRAPHER'S      CLERICAL ASSIST 1   1   Worked Task 
0006    0006 SONOGRAPHER'S      COORD       2   1   Worked Task 
0006    0006 SONOGRAPHER'S      MRT             3   1   Worked Task 
0006    0006 SONOGRAPHER'S      ORIENT      4   1   Worked Task 
0006    0006 SONOGRAPHER'S      SONOGRAPHER     5   1   Worked Task

插入后

0006    0006 ADMIN          CLERICAL ASSIST 1   1   Worked Task 
0006    0006 ADMIN          COORD       2   1   Worked Task 
0006    0006 ADMIN          MRT             3   1   Worked Task 
0006    0006 ADMIN          ORIENT      4   1   Worked Task 
0006    0006 ADMIN          AL          5   3   AL 
0006    0006 MRT'S          CLERICAL ASSIST 1   1   Worked Task 
0006    0006 MRT'S          COORD       2   1   Worked Task 
0006    0006 MRT'S          MRT             3   1   Worked Task 
0006    0006 MRT'S          AL          4   3   AL 
0006    0006 SONOGRAPHER'S      CLERICAL ASSIST 1   1   Worked Task 
0006    0006 SONOGRAPHER'S      COORD       2   1   Worked Task 
0006    0006 SONOGRAPHER'S      MRT             3   1   Worked Task 
0006    0006 SONOGRAPHER'S      ORIENT      4   1   Worked Task 
0006    0006 SONOGRAPHER'S      SONOGRAPHER     5   1   Worked Task 
0006    0006 SONOGRAPHER'S      AL      6   3   AL 

1 个答案:

答案 0 :(得分:0)

首先尝试不使用INSERT INTO

INSERT INTO [Microster].[dbo].[MIC_CC_Task_Edit]
   ( [ID_COST_CENTRE]
   , [NM_ROSTER]
   , [NM_TASK]
   , [No_Order_Number]
   , [TY_TASK]
   , [ID_TASK_TYPE]
   )
  SELECT 
       ID_COST_CENTRE
     , NM_ROSTER
     , 'AL'
     , MAX(No_Order_Number) + 1
     , 3
     , 'AL'
  FROM
      [Microster].[dbo].[MIC_CC_Task_Edit]
  GROUP BY
       ID_COST_CENTRE
     , NM_ROSTER