对于表'X',IDENTITY_INSERT已经为ON。无法对表'Y'执行SET操作

时间:2014-05-23 15:04:15

标签: sql sql-server

我创建了一个执行检查的触发器,并自动将数据填充到2个表中。只有发生以下错误:

IDENTITY_INSERT is already ON for table 'X'. Cannot perform SET operation for table 'Y'.

我在研究错误时发现了这个:

"会话中只有一个表可以将IDENTITY_INSERT属性设置为ON。“

所以修复很简单:

SET IDENTITY_INSERT Table1 ON
-- insert statements for table1
SET IDENTITY_INSERT Table1 OFF

SET IDENTITY_INSERT Table2 ON
-- insert statements for table2
SET IDENTITY_INSERT Table2 OFF

SET IDENTITY_INSERT Table3 ON
-- insert statements for table3
SET IDENTITY_INSERT Table3 OFF

但由于通过触发器填充数据是不可能的。

有人能解决我的问题吗?

我道歉。

谢谢大家。

触发-----

CREATE TRIGGER Alert ON registos AFTER INSERT AS
BEGIN

DECLARE @comp decimal = 0
DECLARE @id_sensores_em_alerta decimal
DECLARE @tempmin decimal = 0
DECLARE @current_max_idAlarme int = (SELECT MAX(IdAlarme) FROM alarmes)
DECLARE @maxidAlarme int
DECLARE @temp decimal = (SELECT s.lim_inf_temp  from sensores s JOIN inserted i ON s.idSensor=i.idSensor )


-- Insert into alarmes from the inserted rows if temperature less than tempmin
INSERT alarmes (IdAlarme, descricao_alarme,data_criacao, idRegisto)
    SELECT
    ROW_NUMBER() OVER (ORDER BY i.idRegisto) + @current_max_idAlarme, 'temp Error', GETDATE(), i.idRegisto
    FROM
inserted AS i
    WHERE
i.Temperatura < @temp

SET @maxidAlarme = (SELECT MAX(IdAlarme) FROM alarmes)

INSERT INTO sensores_tem_alarmes(idSensor,idAlarme,dataAlarme) 
SELECT i.idSensor, @maxidAlarme, GETDATE()
FROM inserted i
SET @comp += 1;


SET @id_sensores_em_alerta=1;

SET  @id_sensores_em_alerta = (SELECT MAX(id_sensores_em_alerta)  FROM sensores_em_alerta)

INSERT INTO sensores_em_alerta(id_sensores_em_alerta, idSensor, idAlarme, data_registo, numerosensoresdisparados) 
SELECT @id_sensores_em_alerta, i.idSensor, @maxidAlarme, GETDATE(), @comp
FROM inserted i
end

库----

enter image description here

2 个答案:

答案 0 :(得分:40)

我有类似的问题,但它没有涉及表触发器。我正在运行一个脚本来刷新多个表的数据,并且我遇到了外键引用错误。

根据MSDN

  

任何时候,会话中只有一个表可以拥有IDENTITY_INSERT   属性设置为ON。

要解决此问题,我为每个要插入的表运行SET IDENTITY_INSERT [dbo].[table_name] OFF。然后,在纠正参考错误后,我能够再次刷新表格。

编辑:我还应该提一下,您可以断开然后重新连接以重置会话。

答案 1 :(得分:6)

允许SQL Server自动为您插入标识值。由于这是一个触发器,因此一次可以插入多行。对于一行插入,您可以使用SCOPE_IDENTITY()函数(http://msdn.microsoft.com/en-us/library/ms190315.aspx)来检索上一个插入行的标识值。 但是,因为我们可以在触发器中插入多行,我们将使用OUTPUT子句(http://msdn.microsoft.com/en-us/library/ms177564.aspx)来获取插入的IdAlarme列表每个idRegisto的值。

我假设alarmes.IdAlarmesensores_em_alerta.id_sensores_em_alerta是此触发器中的两个标识字段。如果是这种情况,那么这应该有效:

CREATE TRIGGER Alert ON registos AFTER INSERT AS
BEGIN

DECLARE @comp decimal = 0
DECLARE @id_sensores_em_alerta decimal
DECLARE @tempmin decimal = 0
DECLARE @temp decimal = (SELECT s.lim_inf_temp  from sensores s JOIN inserted i ON s.idSensor=i.idSensor )

DECLARE @tblIdAlarme TABLE (idRegisto int not null, IdAlarme int not null);

-- Insert into alarmes from the inserted rows if temperature less than tempmin
--  IdAlarme is identity field, so allow SQL Server to insert values automatically.
--      The new IdAlarme values are retrieved using the OUTPUT clause http://msdn.microsoft.com/en-us/library/ms177564.aspx
INSERT alarmes (descricao_alarme,data_criacao, idRegisto)
OUTPUT inserted.idRegisto, inserted.IdAlarme INTO @tblIdAlarme(idRegisto, IdAlarme)
    SELECT descricao_alarme = 'temp Error', data_criacao = GETDATE(), i.idRegisto
    FROM inserted AS i
    WHERE i.Temperatura < @temp
;

--It looks like this table needs a PK on both idSensor and idAlarme fields, or else you will get an error here 
--  if an alarm already exists for this idSensor.
INSERT INTO sensores_tem_alarmes(idSensor,idAlarme,dataAlarme) 
SELECT i.idSensor, a.IdAlarme, dataAlarme = GETDATE()
FROM inserted i
INNER JOIN @tblIdAlarme a ON i.idRegisto = a.idRegisto
;

--not sure what this is doing?? Will always be 1.
SET @comp += 1;

--id_sensores_em_alerta is an identity field, so allow SQL Server to insert values automatically
INSERT INTO sensores_em_alerta(idSensor, idAlarme, data_registo, numerosensoresdisparados) 
SELECT i.idSensor, a.IdAlarme, data_registo = GETDATE(), numerosensoresdisparados = @comp
FROM inserted i
INNER JOIN @tblIdAlarme a ON i.idRegisto = a.idRegisto
;

END