这是原始的SQL Server 2008触发器:
CREATE TRIGGER InsertVehTrig ON dbo.Vehicle
FOR INSERT AS
declare @Cph char(50),
@DevID char(12),
@VehID bigint,
@TmpID bigint,
@DevCount int
begin
set nocount on
select @Cph = rtrim(ltrim(i.cph)), @VehID = i.ID, @DevID = DevID
from inserted i
if(@VehID is null)
return select @DevCount= count(id) from vehicle where (cph=@cph) or (DevID=@DevID)
--选择记录--条件:当前的车牌号 或 设备ID
if ( @DevCount>1)
--如果记录数,超过1,则认为有重复
rollback tran
else if (@DevCount = 1)
begin
select @TmpID = id from vehicle where (cph = @cph) or (DevID = @DevID)
if (@TmpID != @VehID)
--如果增加的车牌号码与数据库中的在相同的,则不允许增加
rollback tran
end
--增加,此时需要在Tbl_Toll中增加
/* insert into tbl_Toll (VehID,[Money], Payer, PayerTel, TollDate, ValidDate, Demo, UserName)
values(@VehID, 0, 'LiaoYuan', '0755-26506932', DATEADD(month, 1, getDate()), DATEADD(month, 13, getDate()), '' , 'admin' )*/
update vehicle set cph = @Cph where ID = @VehID
end
我正在尝试将其转换为MySQl触发器
DROP TRIGGER IF EXISTS InsertVehTrig;
DELIMITER $$
CREATE TRIGGER InsertVehTrig AFTER INSERT
ON Vehicle FOR EACH ROW
SWL_return:
BEGIN
DECLARE Cph CHAR(50);
DECLARE DevID CHAR(12);
DECLARE VehID BIGINT;
DECLARE TmpID BIGINT;
DECLARE DevCount INT;
SET Cph = rtrim(ltrim(NEW.cph));
SET VehID = NEW.ID;
SET DevID = NEW.DevID;
if(VehID is null) then
select count(id) into @DevCount from vehicle where (cph=@Cph) or (DevID=@DevID);
-- 条件:当前的车牌号 或 设备ID
end if;
if (DevCount > 1) then -- 如果记录数,超过1,则认为有重复
-- Rollback not supported in trigger
SET @SWV_Null_Var = 0;
Leave SWL_return;
else
if (DevCount = 1) then
select ID INTO @TmpID from Vehicle where (Vehicle.cph = @Cph) or (Vehicle.DevID = @DevID);
if (TmpID != VehID) then -- --如果增加的车牌号码与数据库中的在相同的,则不允许增加
-- Rollback not supported in trigger
Leave SWL_return;
SET @SWV_Null_Var = 0;
end if;
end if;
end if;
update vehicle set cph = @Cph where ID = @VehID;
END;
问题
我在变量前使用@
是否正确?据我所知,SQL Server使用@
作为变量声明。但我不知道的MySQL是否正常工作
SQL Server有rollback tran
我该怎么把它更改为MySQL?当我想取消插入数据操作时,如何在MySQL中替换为rollback
?
SET @SWV_Null_Var = 0;
是什么意思?我没有看到这个变量的声明?这是什么?
答案 0 :(得分:0)
从MS SQL到MY SQL的完全转换
/ *防止用户插入的数据中,车牌号码的前后含有空格,此处是将这样的空格去掉
程宪平----- 2009-04-19
修改记录:2009-04-23
修改原因:车牌必须唯一,DEVID也必须唯一,所以其一致性必须通过数据库自身来进行处理,保证一致性* /
DROP TRIGGER如果存在InsertVehTrig;
DELIMITER $$
CREATE TRIGGER InsertVehTrig after INSERT
ON Vehicle FOR EACH ROW
SWL_return:
BEGIN
DECLARE TmpID BIGINT;
DECLARE DevCount INT;
DECLARE msg VARCHAR(255);
DECLARE New_Cph VARCHAR(100);
SET New_Cph = NEW.Cph;
select count(id) into DevCount from vehicle where (vehicle.Cph=New_Cph) or (vehicle.DevID=New.DevID);
if (DevCount > 1) then -- 如果记录数,超过1,则认为有重复
set msg = concat('Cannot Create Same CPH or DevID : \r\nDevCount=',DevCount,'\r\nCPH=',New_Cph,'\r\nDevID=',New.DevID);
SIGNAL sqlstate '45001' set message_text = msg;
else
if (DevCount = 1) then
select ID INTO TmpID from Vehicle where (Vehicle.Cph = New_Cph) or (Vehicle.DevID = New.DevID);
if (TmpID != New.ID) then -- --如果增加的车牌号码与数据库中的在相同的,则不允许增加
set msg = concat('Cannot Create Same CPH or DevID : \r\nTmpID=',TmpID,'\r\nCPH=',New_Cph,'\r\nDevID=',New.DevID);
SIGNAL sqlstate '45001' set message_text = msg;
end if;
end if;
end if;
END;
在MySql中,这用于取消Insert语句。
set msg = concat('Cannot Create Same CPH or DevID : \r\nTmpID=',TmpID,'\r\nCPH=',New_Cph,'\r\nDevID=',New.DevID);
SIGNAL sqlstate '45001' set message_text = msg;