在DB中添加多个记录,并在PK变量上增加

问题描述 投票:-1回答:1

我在一个表中插入了很多记录。如果我只插入1条数据记录,则效果很好。当我添加第二组要插入的值或更多时,我会在下面收到错误消息。我不确定是否可以用不同的方式来解决这个问题。在设施tbl中的最后一个PK是(299461)时,(299462)使用第一组插入值生成。对于插入的每个记录,@tempfacilityID应该以1的增量生成。如果对此有不同的处理方法,请告诉我。这种方法似乎不起作用,它给我一个@tempfacilityID变量的错误。

Error Message
    "Violation of PRIMARY KEY constraint 'PK_FacilityData'. Cannot insert duplicate key in object 'dbo.Facility'. The duplicate key value is (299462).
    The statement has been terminated.
    "

    Begin transaction 

    SET IDENTITY_INSERT Facility ON
    DECLARE @tempfacilityID int
    SELECT @tempfacilityID = MAX(facilityID)+ 1 FROM Facility 

    DECLARE @tempCategoryID int
    SELECT @tempCategoryID = CategoriesServicesID 
    FROM CategoriesServicesList
    inner join CategoriesList on CategoriesList.CategoryID = CategoriesList.CategoryID
    where CategoriesList.CategoryID = '10' and CategoriesServicesList.CategoriesServicesID = '5'

    INSERT INTO Facility (
           [FacilityID]
          ,[CategoriesServicesID]
          ,[FacilityName]
          ,[BuildingNumber]
          ,[Address]
          ,[Address2]
          ,[Borough]
          ,[area]
          ,[Latitude]
          ,[Longitude]
          ,[Phone]
          ,[AdditionalInfo]
          ,[StartDate]
          ,[EndDate]
          ,[Monday]
          ,[Tuesday]
          ,[Wednesday]
          ,[Thursday]
          ,[Friday]
          ,[Saturday]
          ,[Sunday]
          ,[IsActive]
          ,[Website]
    )
    VALUES(
    @tempfacilityid
    ,@tempCategoryID
    ,'Friends Equality'
    ,111
    ,'111 Brewer Street'
    , NULL
    , 'stan Isl'
    , 12359
    ,90.8594712
    ,-37.8951468
    ,'(646) 845-9868'
    ,'<b>Friendships start at a young age.</p>'
    ,'1900-01-01 00:00:00.000'
    ,'1900-01-01 00:00:00.000'
    ,''
    ,''
    ,''
    ,''
    ,''
    ,''
    ,''
    ,1
    ,'www.CareHand.org'
    )
     ,
    union all

    Values(

    @tempfacilityid
    ,@tempCategoryID
    ,'Self Care(specific)'
    ,189
    ,'189 Jaysone Street'
    , NULL
    , 'honran'
    , 105552
    ,26.7143459
    ,-73.992332
    ,'(212) 579-4658'
    ,'Self Awareness'
    ,'1900-01-01 00:00:00.000'
    ,'1900-01-01 00:00:00.000'
    ,''
    ,''
    ,''
    ,''
    ,''
    ,''
    ,''
    ,1
    ,'www.awarenesWpeople.org'

    )
     ,
union all

Values(

@tempfacilityid
,@tempCategoryID
,'Self Care(specific)'
,536
,'536 Melrose Ave'
, NULL
, 'Woodbury'
, 12578
,66.259459
,-93.933332
,'(912) 486-9436'
,'Joyful Life'
,'1900-01-01 00:00:00.000'
,'1900-01-01 00:00:00.000'
,''
,''
,''
,''
,''
,''
,''
,1
,'www.JoyfulLiving.org'

)


SET IDENTITY_INSERT Facility OFF
sql tsql sql-insert
1个回答
0
投票

您正在尝试插入相同的PK ID 3x,这就是生成错误的原因。您应该省略[FacilityID]列(因为它是主键,它将自动递增,您需要删除SET IDENTITY_INSERT Facility ON / OFF):

DECLARE @tempfacilityID INT, @tempfacilityID1 INT,@tempfacilityID2 INT


DECLARE @tempCategoryID int
SELECT @tempCategoryID = CategoriesServicesID 
FROM CategoriesServicesList
inner join CategoriesList on CategoriesList.CategoryID = CategoriesList.CategoryID
where CategoriesList.CategoryID = '10' and CategoriesServicesList.CategoriesServicesID = '5'

INSERT INTO Facility (
      [CategoriesServicesID]
      ,[FacilityName]
      ,[BuildingNumber]
      ,[Address]
      ,[Address2]
      ,[Borough]
      ,[area]
      ,[Latitude]
      ,[Longitude]
      ,[Phone]
      ,[AdditionalInfo]
      ,[StartDate]
      ,[EndDate]
      ,[Monday]
      ,[Tuesday]
      ,[Wednesday]
      ,[Thursday]
      ,[Friday]
      ,[Saturday]
      ,[Sunday]
      ,[IsActive]
      ,[Website]
)
VALUES(
@tempCategoryID
,'Friends Equality'
,111
,'111 Brewer Street'
, NULL
, 'stan Isl'
, 12359
,90.8594712
,-37.8951468
,'(646) 845-9868'
,'<b>Friendships start at a young age.</p>'
,'1900-01-01 00:00:00.000'
,'1900-01-01 00:00:00.000'
,''
,''
,''
,''
,''
,''
,''
,1
,'www.CareHand.org'
)
 ,
union all

Values(
 @tempCategoryID
,'Self Care(specific)'
,189
,'189 Jaysone Street'
, NULL
, 'honran'
, 105552
,26.7143459
,-73.992332
,'(212) 579-4658'
,'Self Awareness'
,'1900-01-01 00:00:00.000'
,'1900-01-01 00:00:00.000'
,''
,''
,''
,''
,''
,''
,''
,1
,'www.awarenesWpeople.org'

)
 ,
union all

Values(
 @tempCategoryID
,'Self Care(specific)'
,536
,'536 Melrose Ave'
, NULL
, 'Woodbury'
, 12578
,66.259459
,-93.933332
,'(912) 486-9436'
,'Joyful Life'
,'1900-01-01 00:00:00.000'
,'1900-01-01 00:00:00.000'
,''
,''
,''
,''
,''
,''
,''
,1
,'www.JoyfulLiving.org'

)

这样,无论您插入多少条记录,都会生成正确的新PK.enter code here

© www.soinside.com 2019 - 2024. All rights reserved.