tags:

views:

26

answers:

2

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
A: 

Just set id column to AUTO_INCREMENT. You don't need to toggle something on and off.

Read more in documentation

Naktibalda
A: 

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, or ROLLBACK.

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:

SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;

-- Your SQL statements here.

SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;

I'm not sure why you would need to do that in your trigger, so your MySQL trigger would look something like this:

DELIMITER |
CREATE TRIGGER T1 AFTER INSERT ON A FOR EACH ROW
BEGIN

    INSERT INTO B (id, text) VALUES (NEW.id, NEW.text);

    INSERT INTO C (id, text) VALUES (NEW.id, NEW.text);

END;|
DELIMITER ;

Here's the results of a quick test:

CREATE TABLE `A` (
  `id` int(11) NOT NULL auto_increment,
  `text` varchar(255) default NULL,
  PRIMARY KEY  (`id`)
);

CREATE TABLE `B` (
  `id` int(11) NOT NULL auto_increment,
  `text` varchar(255) default NULL,
  PRIMARY KEY  (`id`)
);

CREATE TABLE `C` (
  `id` int(11) NOT NULL auto_increment,
  `text` varchar(255) default NULL,
  PRIMARY KEY  (`id`)
);

DELIMITER |
CREATE TRIGGER T1 AFTER INSERT ON A FOR EACH ROW
BEGIN

    INSERT INTO B (id, text) VALUES (NEW.id, NEW.text);

    INSERT INTO C (id, text) VALUES (NEW.id, NEW.text);

END;|
DELIMITER ;

INSERT INTO `A` (id, text) VALUES (1, 'Line 1');
INSERT INTO `A` (id, text) VALUES (2, 'Line 3');
INSERT INTO `A` (id, text) VALUES (3, 'Line 3');

SELECT * FROM `A`;
+----+--------+
| id | text   |
+----+--------+
|  1 | Line 1 |
|  2 | Line 3 |
|  3 | Line 3 |
+----+--------+

SELECT * FROM `B`;
+----+--------+
| id | text   |
+----+--------+
|  1 | Line 1 |
|  2 | Line 3 |
|  3 | Line 3 |
+----+--------+

SELECT * FROM `C`;
+----+--------+
| id | text   |
+----+--------+
|  1 | Line 1 |
|  2 | Line 3 |
|  3 | Line 3 |
+----+--------+

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

Mike