1

我在为存储过程执行作业时遇到问题,它返回一个错误,我可以将数据插入表中

当 IDENTITY_INSERT 设置为 OFF 时,无法在表“TagMovementTable_temp”中插入标识列的显式值。[SQLSTATE 23000](错误 544)。步骤失败。,00:00:00,16,544,,,,0

这是我的程序,我已经设置IDENTITY_INSERT为 ON,我不知道该怎么办,请帮助和想法?

SET IDENTITY_INSERT Backup_movment.dbo.TagMovementTable_temp ON
go

ALTER procedure [dbo].[DeleteTagData]
as
begin
   insert into Backup_movment.dbo.TagMovementTable_temp([ID]
      ,[TagID]
      ,[AntennaID]
      ,[RSSI]
      ,[Timestamp]
      ,[TagData]
      ,[ZoneCoordinateGroupID]
      ,[DBTimeStamp]
      ,[BatteryStatus]
      ,[Longitude]
      ,[Latitude]
      ,[Accuracy]) 
   select 
      [ID]
      ,[TagID]
      ,[AntennaID]
      ,[RSSI]
      ,[Timestamp]
      ,[TagData]
      ,[ZoneCoordinateGroupID]
      ,[DBTimeStamp]
      ,[BatteryStatus]
      ,[Longitude]
      ,[Latitude]
      ,[Accuracy] 
   FROM 
      [TrailBlazerNG].[dbo].[TagMovementTable] 
   where not exists (select 1 
                     from Backup_movment.dbo.TagMovementTable_temp 
                     where Backup_movment.dbo.TagMovementTable_temp.ID =  [TrailBlazerNG].[dbo].[TagMovementTable].ID  )

if not exists(select top 1 * from ZoneTable where EmergencyMode=1)
begin
    delete top(10000) from TagMovementTable where [DBTimeStamp]<DATEADD(hh,-24,getdate())
end

Delete from tagstrongestpingdatatable where [dbtimestamp] < dateadd(hh,-4,getdate())

Delete from tagrawdatatable where [dbtimestamp] < dateadd(hh,-1,getdate())
end
4

1 回答 1

0

SET IDENTITY_INSERT语句移到INSERT.

ALTER procedure [dbo].[DeleteTagData]
AS
BEGIN

SET IDENTITY_INSERT Backup_movment.dbo.TagMovementTable_temp ON

INSERT INTO Backup_movment.dbo.TagMovementTable_temp([ID]
  ,[TagID]
  ,[AntennaID]
  ,[RSSI]
  ,[Timestamp]
  ,[TagData]
  ,[ZoneCoordinateGroupID]
  ,[DBTimeStamp]
  ,[BatteryStatus]
  ,[Longitude]
  ,[Latitude]
  ,[Accuracy]
)  
    SELECT 
      [ID]
      ,[TagID]
      ,[AntennaID]
      ,[RSSI]
      ,[Timestamp]
      ,[TagData]
      ,[ZoneCoordinateGroupID]
      ,[DBTimeStamp]
      ,[BatteryStatus]
      ,[Longitude]
      ,[Latitude]
      ,[Accuracy] 
    FROM [TrailBlazerNG].[dbo].[TagMovementTable] 
    WHERE NOT EXISTS(
        SELECT 1 
        FROM Backup_movment.dbo.TagMovementTable_temp 
        WHERE Backup_movment.dbo.TagMovementTable_temp.ID =  [TrailBlazerNG].[dbo].[TagMovementTable].ID
    )

IF NOT EXISTS(SELECT TOP 1 * FROM ZoneTable WHERE EmergencyMode = 1)
BEGIN
    DELETE TOP(10000) FROM TagMovementTable WHERE [DBTimeStamp ] < DATEADD(hh, -24, GETDATE())
END

DELETE FROM tagstrongestpingdatatable WHERE [dbtimestamp] < DATEADD(hh, -4, GETDATE())

DELETE FROM tagrawdatatable WHERE [dbtimestamp] < DATEADD(hh, -1, GETDATE())


END
于 2015-02-11T06:33:54.743 回答