Do you know how to rewrite these this query in MySQL ?
I can’t find Identity insert, I can’t find any try catch,
I don’t understand it.
CREATE TRIGGER T1 ON DB1.dbo.A
AFTER INSERT AS
BEGIN TRY
SET IDENTITY_INSERT DB2.dbo.B ON
INSERT INTO dbo.B(id, text) SELECT A.id,A.text FROM dbo.A INNER JOIN inserted I ON I.id = A.id
SET IDENTITY_INSERT DB2.dbo.B OFF
SET IDENTITY_INSERT DB2.dbo.D ON
INSERT INTO dbo.D(id, text) SELECT A.id,A.text FROM dbo.A INNER JOIN inserted I ON I.id = A.id
SET IDENTITY_INSERT DB2.dbo.D OFF
END TRY
BEGIN CATCH
ROLLBACK TRANSACTION
SET IDENTITY_INSERT DB2.dbo.B OFF
SET IDENTITY_INSERT DB2.dbo.D OFF
END CATCH
GO
MySQL triggers have implicit transaction support, so the trigger cannot use statements that explicitly or implicitly begin or end a transaction such as
START TRANSACTION,COMMIT, orROLLBACK.It is not necessary in MySQL to enable the insertion of values into primary key columns – this is already allowed. You can, however, toggle foreign key constraint checking and unique index checking:
http://dev.mysql.com/doc/refman/5.1/en/innodb-foreign-key-constraints.html
http://dev.mysql.com/doc/refman/5.1/en/server-system-variables.html#sysvar_unique_checks
A common way to do this is to store the existing values in user variables, change the settings, then restore the settings after your script is complete:
I’m not sure why you would need to do that in your trigger, so your MySQL trigger would look something like this:
Here’s the results of a quick test:
If you want something similar to
TRY…CATCH, you’ll need to use handlers instead:http://dev.mysql.com/doc/refman/5.1/en/declare-handler.html
Here’s the documentation on MySQL triggers:
http://dev.mysql.com/doc/refman/5.1/en/commit.html