r/SQL•
prof436

Transact sql trigger work when doing a merge in ssms but doesn't when doing the same merge in Python

MS SQL

Hello, I made a trigger in sql server to put data in another table on insert or update. It works when I do a merge in a ssms query but when I do the same query in python it doesn't work. I used Microsoft server profiler to see what was Happening but I couldn't see any error and the trigger was triggered by the merge. Is there a reason why it works in ssms but not in python? When I disable the trigger my merge works in python so it is a problem with the trigger but I cannot see the trace when I execute a query with python, is there a way I can see it?

​

Thanks

Posted Dec 14, 2021, 10:28 PM UTC

66
[deleted]•

[removed]

2
xodusprime•

Trying to make sure I understand the narrative here:

Are you saying that when you run it from Python, while using profiler, you see the trigger fire, but that when you look at the records, nothing actually happened?

Also, are you saying that when you turn the trigger off, and run it from python, you do see records being altered in the primary table?

If that's right, I'd want to know - is the merge you're running in SSMS identical to the one from Python, same data elements, etc? I'm wondering if you've got a more full data set in Python, and it's causing problems with constraints or something else when the trigger tries to fire. You should have seen those in the trace, but if a trigger fails, it will also rollback the initial statement - so it would look like the command didn't execute at all.

​

With the normal default selected values on a profiler trace, you should see any errors and rollbacks. Are you using the default template, or are you selecting your own events?

1
prof436OP•

My merge is a string in python so I print it and copy paste it in ssms and it works it's the exact same set. I tried the default template but didn't see any rollback

1
xodusprime•

And are you saying when you run it from Python, while using profiler, you see the trigger fire, but that when you look at the records, nothing actually happened?

1
prof436OP•

Yes

1
xodusprime•

If you're seeing the trigger fire, and nothing rolling back, my next best guess would be that there's an empty data set going to the trigger. Did you back your change out of the table after running the merge in SSMS? If not, does the matching criteria on the merge still cause the exact same merge to update records?

1