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
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.)
[font="Times New Roman"]Erland Sommarskog, SQL Server MVP, www.sommarskog.se[/font]
Thank you so much !
I 'll try this first, if it works, I 'll study the problem of several rows.
Thanks a lot, seriously !
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
select geometry::STGeomFromWKB(0x0106000000020000000103000000010000000B0000001000000000000840000000000000003DD8CCCCCCCCCC0840000000000000003DD8CCCCCCCCCC08408014AE47E17AFC3F040000000000104000CDCCCCCCCCEC3F9C999999999913408014AE47E17AFC3F9C99999999991340000000000000003D0000000000001440000000000000003D000000000000144000000000000000400400000000001040000000000000F03F100000000000084000000000000000401000000000000840000000000000003D0103000000010000000B000000000000000000143D000000000000003D009E99999999B93F000000000000003D009E99999999B93F8014AE47E17AFC3F400000000000F03F00CDCCCCCCCCEC3FA06666666666FE3F8014AE47E17AFC3FA06666666666FE3F000000000000003D1800000000000040000000000000003D18000000000000400000000000000040400000000000F03F000000000000F03F000000000000143D0000000000000040000000000000143D000000000000003D, 0);
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.)
[font="Times New Roman"]Erland Sommarskog, SQL Server MVP, www.sommarskog.se[/font]
Erland Sommarskog - Author of several excellent writings and the host of sp_sqltrace. Very nice you see you here. Welcome!
Viewing 8 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply