Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Please HELP !! :SQL SERVER with MySQL Linked Server. Error Executing Trigger Expand / Collapse
Author
Message
Posted Saturday, July 6, 2013 6:42 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, July 9, 2013 2:34 AM
Points: 5, Visits: 10
Hi sirs ! If somebody can help me i would appreciate a lot !

I have SQL server with MySQL Linked server through ODBC.
Also have a Trigger on SQL Server Table :

ALTER TRIGGER [dbo].[TriggerStock]
ON [dbo].[T_Contabilidad]
FOR UPDATE,insert
AS
BEGIN
DECLARE
@Id integer,
@Stock integer

SELECT @ID = Idt_Conta,@stock=Stock FROM INSERTED
IF @Stock>0
BEGIN
UPDATE OPENQUERY(MYSQLVINC,'SELECT Idt_web, disponible FROM mibasedatos.t_web
where idt_web=@id') SET disponible= 1
END
ELSE
BEGIN
UPDATE OPENQUERY(MYSQLVINC,'SELECT Idt_web, disponible FROM mibasedatos.t_web
where idt_web=@id') SET disponible= 0
END

END

The idea : I NEED TO UPDATE MySQL TABLE WHEN SQL SERVER Table is UPDATED/INSERT
PROBLEM : SQL throws an error :

Mens 7390, Nivel 16, Estado 2, Procedimiento TriggerStock, Línea 18
No se pudo realizar la operación solicitada porque el proveedor OLE DB "MSDASQL" del servidor vinculado "MYSQLVINC" no admite la interfaz de transacción requerida
.

(In english : Could'nt do the operation because of OLE DB MSDASQL from linked server does'nt admit transation required interface.

Any help please ? Thanks in advance


Post #1470953
Posted Saturday, July 6, 2013 7:38 PM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: 2 days ago @ 3:40 PM
Points: 1,787, Visits: 5,692
First, let me say that your trigger will cause you problems as it does not cater for multiple row updates, this is something you should look into changing as soon as possible.

Now, moving on to your immediate problem, this can be handled slightly differently.

My recommendation to you is to not attempt to do cross system updates within a trigger.

Better would be to write the updates to a local sql server database and process them outside of the transaction (the transaction that fired the trigger), this will remove the potential bottleneck every time you update your table.

Something like this would be your table to hold details of updates required to your MySQL table:

create table some_holding_table(inserted_time datetime default(getdate()) not null, ID int, Stock int);

GO

And your trigger would be something like this:

ALTER TRIGGER [dbo].[TriggerStock] 
ON [dbo].[T_Contabilidad]
FOR UPDATE,insert
AS
BEGIN

INSERT some_holding_table(ID,Stock)
SELECT Idt_Conta,Stock FROM INSERTED

END

GO

Then you have a periodic process to read "some_holding_table" and perform the required updates to the remote system.


MM


  • MMGrid Addin
  • MMNose Addin


  • Forum Etiquette: How to post Reporting Services problems
  • Forum Etiquette: How to post data/code on a forum to get the best help - by Jeff Moden
  • How to Post Performance Problems - by Gail Shaw

  • Post #1470957
    Posted Sunday, July 7, 2013 9:17 AM
    Forum Newbie

    Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

    Group: General Forum Members
    Last Login: Tuesday, July 9, 2013 2:34 AM
    Points: 5, Visits: 10
    First of all, thanks for your advices.
    The problem continues alive, I need to update to MySQL Server, how can i do that ? Could you be more explicit ? I m sorry, i m not an expert on SQL Server.

    Thanks again
    Post #1470976
    Posted Sunday, July 7, 2013 1:50 PM


    SSC Eights!

    SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

    Group: General Forum Members
    Last Login: 2 days ago @ 3:19 PM
    Points: 800, Visits: 713
    This is not a simple problem, but mister.magoo is correct. The trigger way is not likely to work out well. First a few words on why the trigger you have does not work at all:

    SELECT @ID = Idt_Conta,@stock=Stock FROM INSERTED

    This is the first error. A trigger fires once per statement, and a INSERT or UPDATE statement could affect multiple rows. You are only catering for one of them.

    UPDATE OPENQUERY(MYSQLVINC,'SELECT Idt_web, disponible FROM mibasedatos.t_web
    where idt_web=@id')

    This is the second error. The SELECT statement you pass to OPENQUERY is executed in MySQL where @id is likely to be a syntax error. It certainly has no relation to your local variable @id. To get the value into the query you would need to use dynamic SQL. But as already noted, @id should not be there at all, since you need to handle multiple-row operations.

    This could work in theory:

    UPDATE MYSQLVINC..mibasedatos.t_web
    SET disponible = CASE WHEN i.Stock > 0 THEN 1 ELSE 0 END
    FROM MYSQLVINC..mibasedatos.t_web m
    JOIN inserted i ON m.idt_web = i.Idt_conta

    But I would be surprised that it actually does in practice.

    As Mister.magoo suggests you need to find an asynchronous solution. The trigger could popoulate a queue table which you poll reguarly. You could also implement Change Tracking (which means that SQL Server maintains that queue table for you.)




    Erland Sommarskog, SQL Server MVP, www.sommarskog.se
    Post #1470987
    Posted Monday, July 8, 2013 5:20 AM
    Forum Newbie

    Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

    Group: General Forum Members
    Last Login: Tuesday, July 9, 2013 2:34 AM
    Points: 5, Visits: 10
    Thank you so much !

    I 'll try this first, if it works, I 'll study the problem of several rows.

    Thanks a lot, seriously !
    Post #1471117
    Posted Monday, July 8, 2013 6:59 AM


    SSCommitted

    SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

    Group: General Forum Members
    Last Login: 2 days ago @ 3:40 PM
    Points: 1,787, Visits: 5,692
    your original error may be due to SQL server attempting to enlist a distributed transaction and MySQL cannot take part in that transaction.

    This would be because you are attempting to update MySQL from a trigger and the code is therefore running as part of the transaction that fires the trigger.

    I am not sure about this because I don't ever use MySQL, but that seems to be what the error is saying.

    This is another reason to move the remote update outside of the trigger....


    MM


  • MMGrid Addin
  • MMNose Addin


  • Forum Etiquette: How to post Reporting Services problems
  • Forum Etiquette: How to post data/code on a forum to get the best help - by Jeff Moden
  • How to Post Performance Problems - by Gail Shaw

  • Post #1471165
    Posted Tuesday, July 9, 2013 2:00 AM


    SSC Eights!

    SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

    Group: General Forum Members
    Last Login: 2 days ago @ 3:19 PM
    Points: 800, Visits: 713
    mister.magoo (7/8/2013)
    your original error may be due to SQL server attempting to enlist a distributed transaction and MySQL cannot take part in that transaction.

    This would be because you are attempting to update MySQL from a trigger and the code is therefore running as part of the transaction that fires the trigger.


    You are right. I did not look too close on the error message, as there was an obvious syntax error in the MySQL query (the variable @id). But I more or less expect that the query I suggested would die with that error. Since you are in a trigger, you are in a transaction, and by default SQL Server attempts to make that a distributed transaction. (This can be changed with sp_serveroption). A distributed query may have better odds to work, if one uses the OLE DB provider for MySQL rather than MSDASQL + ODBC, but I would not count on it.

    (Just like Mister Magoo, I have no experience of MySQL myself.)


    Erland Sommarskog, SQL Server MVP, www.sommarskog.se
    Post #1471469
    Posted Tuesday, July 9, 2013 8:04 AM
    SSCarpal Tunnel

    SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

    Group: General Forum Members
    Last Login: Yesterday @ 1:53 PM
    Points: 4,130, Visits: 3,157
    Erland Sommarskog - Author of several excellent writings and the host of sp_sqltrace. Very nice you see you here. Welcome!


    Tally Tables - Performance Personified
    String Splitting with True Performance
    Best practices on how to ask questions
    Post #1471639
    « Prev Topic | Next Topic »

    Add to briefcase

    Permissions Expand / Collapse