如何将mysql触发器转换/迁移到Sql Server触发器

时间:2014-05-20 10:09:34

标签: mysql sql-server

我在mysql中触发了一个触发警报,总是输入值小于设定值。但现在我需要它在SQL SERVER中完成。

如果有人能帮助我将mysql触发器转换为SQL Server触发器,我将不胜感激。

立即感谢所有人。


我的触发器是:

DELIMITER $$
create TRIGGER alert
AFTER INSERT ON records
FOR EACH ROW
begin
Set @comp=0;
Set @tempmax=0;
Set @tempmin=0;

select lim_inf_temp into @tempmin from sensores where idSensor=NEW.idSensor;


Set @maxidAlarme=0;
if (CAST(NEW.Temperatura AS UNSIGNED)<@tempmin) then
SELECT MAX(idAlarme) into @maxidAlarme FROM alarmes;
SET @maxidAlarme=@maxidAlarme+1;
INSERT INTO alarmes(idAlarme,descricao_alarme, idRegisto) VALUES (@maxidAlarme,"inserted below the normal temperature",New.idRegisto);
INSERT INTO sensores_tem_alarmes(idSensor,idAlarme,dataAlarme) VALUES (NEW.idSensor,@maxidAlarme,NOW());
set @comp=+1;
end if; 


set @id_sensores_em_alerta=1;
SELECT MAX(id_sensores_em_alerta) into @id_sensores_em_alerta FROM sensores_em_alerta;
 INSERT INTO sensores_em_alerta(id_sensores_em_alerta, idSensor, idAlarme, data_registo, numerosensoresdisparados) VALUES (id_sensores_em_alerta,NEW.idSensor, @maxidAlarme, NOW(), @comp);
end $$;

DELIMITER  ;

我试图在SQL Server中创建触发器,但由于脚本不同而且我正在以正确的方式遇到很多困难。

我的尝试并不顺利:

CREATE TRIGGER Alert ON registos AFTER INSERT AS
BEGIN

DECLARE @comp decimal= 0
DECLARE @tempmax decimal= 0
DECLARE @tempmin decimal= 0


DECLARE @current_max_idAlarme int = (SELECT MAX(IdAlarme) FROM alarmes)

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

END

但是什么都不做。 不要在表警报上创建数据:S

有人可以帮助我吗?我会永远感激。

许多问候,谢谢大家。

2 个答案:

答案 0 :(得分:0)

首先,MSSQL没有选项FOR EACH ROW,所以它一次将多个插入的行视为一组。因此,您必须将值插入表变量中。

不幸的是我实际上并不了解MySQL,但我相信这是一个起点?

CREATE TRIGGER ALERT
ON records
AFTER INSERT
AS
BEGIN
 DECLARE @comp INT;
 DECLARE @tempmax INT;
 DECLARE TABLE @tempmin (tempmin INT);

 INSERT INTO @tempmin
 SELECT s.lim_inf_temp FROM sensores s WHERE s.idSensor IN (inserted.idSensor);
--rest of the code

答案 1 :(得分:0)

我将发布此代码以反对我更好的判断 - 重新设计表格比这个黑客更好。

这使用ROW_number()来虚拟化警报表的代理身份密钥。这是一个“糟糕的计划”(tm)。

答案也很局部 - 它没有完成你的问题所要求的所有内容 - 我希望它能让你走得更远。使用它作为如何与虚拟INSERTED表进行交互的指南。祝你好运

CREATE TRIGGER Alert ON records AFTER INSERT AS
BEGIN

DECLARE @comp INT = 0
DECLARE @tempmax INT = 0
DECLARE @tempmin INT = 0

-- get the max current id.
-- note that this is EXTREMELY unsafe as if two pieces of code are executing
-- at the same time then you *will* end up with key conflicts.
-- you could use SERIALIZABLE.... but better would be to redisn the schema
DECLARE @current_max_idAlarme = (SELECT MAX(IdAlarme) FROM alarmes)

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

END