Question : Insert into MySQL Table from MSSQL Insert Trigger

Hi there!

I'm now a few days on solving the following problem:

I have an application, that runs on a MSSQL Database. Now an other Application, running on an MySQL-Database.
Now - they share some data, which makes it necessary to transfer partial data between two tables.

The approach is now, to create a trigger on the MSSQL-Table (t1) and to write the data from "Insert"-Table to the MySQL-Table (t2).
Remember: I do only need a partial set of data, where column names on t1 and t2 vary. (ffieldms1-data will be filled to fieldmy1)

MySQL-Database is set up as Linked Server via ODBC 5.1 Driver.

My konzeptual Trigger is the follow:


1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:

CREATE TRIGGER myInsertTrigger
    ON [MSSQLDB].[dbo].[t1]
    FOR INSERT,UPDATE
    AS
		DECLARE @ID int
		SELECT @ID = (SELECT [id] FROM Inserted)
		PRINT @ID
		IF ( @ID >= 5000 ) AND ( @ID < 5999 )
        BEGIN
		SET XACT_ABORT ON
		BEGIN TRANSACTION
			INSERT INTO 
				OPENQUERY([MYSQL-REMOTE],'
				SELECT fieldmy1, fieldmy2
				FROM t1')
			SELECT fieldms1, fieldms2
			FROM Inserted

			DELETE FROM [MSSQLDB].[dbo].[t1] WHERE [ID] = @ID
		COMMIT TRANSACTION
		END


When Trigger is executed (e.g. with a update of ID to above range) following error occurs

The operation could not be performed because the OLE DB provider 'MSDASQL' was unable to begin a distributed transaction.
[MYSQL][ODBC 5.1 Driver] Optional Feature not supported

I need specific help - google brings up a mass of threads, i did not find the right one.

If someone knows, how to configure the mssql-script, the mssql server or the mysql-server

THANK YOU

Answer : Insert into MySQL Table from MSSQL Insert Trigger

If you are going to do this in a trigger, I'm betting your driving reason is that you don't have control of the client issuing the INSERT statement.

If this is the case, I'd suggest that you create a new table - call it MySQLQueue and insert the data selectively that is pushed into this table, into that Queue table, then use a job that runs and pushes it over into MySQL.

If you need certain records deleted from your original table you could also do that inside your trigger, but I'd just suggest using an INSTEAD OF trigger to do the job.

Using this approach won't lock your inserts on your original table while you try to synchronize with a remote MySQL server.  It does, however, introduce some latency based on the frequency that the job processes your queue, but they are processed asynchronously at least.

While triggers are a very powerful tool, they can easily bring a system to its knees.  Use them with extreme caution and ALWAYS very judiciously.
Random Solutions  
 
programming4us programming4us