This simply makes the whole UPDATE/INSERT not work. When I remove the cursor declaration, it works. The cursor is just selecting a field from a table that is existing in the same database called "contacts". What is wrong?
(comments are locked)
|
|
A trigger should ideally do something very fast, so that it can run "incognito". If what you want is to loop through a child table and send a mail for each row, I agree with the suggestion to change the trigger to instead just update a table with the information that should be e-mailed, and have a job run every n seconds/minutes/hours/whateverintervalyouwant. Here's how I would change your current trigger. First a table to store the information to send: And then some changes to the trigger: Finally I would create a stored procedure which actually sends the emails: An alternative to this would be to include the logic to send e-mails into the original stored procedure which performs the updates. But that would mean the application has to wait for an e-mail to be sent for each child-row, and that's not very efficient either. My suggestion is written totally without even the simplest compilation tests, so it probably needs a few tweaks... Oh, and by the way - I changed the query to use ansi joins, and I included the statusid-check in the query instead of doing it afterwards.
Jul 29 '10 at 03:42 AM
Magnus Ahlkvist
Thanks a lot for the help! There is just a type in the last code: "DECLARE TRIGGER tr FOR select email,..." should be "DECLARE CURSOR tr FOR select email,.."
Jul 29 '10 at 07:13 AM
johnshaddad
I wish I could say "It's because I never declare cursors", but unfortunately most of our databases are flooded with cursor definitions, so I guess I'll have to blame the typo on something else.. I haven't worked for three weeks, but instead enjoyed sunny weather and beers. That must be it.
Jul 29 '10 at 01:32 PM
Magnus Ahlkvist
(comments are locked)
|
|
I'm not sure, entirely, but I have a feeling it's SQL Server's way of telling you that a cursor inside a trigger is not a good way to go. Triggers, ok (some people will try and avoid them like the plague, but I accept that they have their place) - but a cursor within a trigger is going to be a performance issue. Try and do what you wanted to do in a set-based manner, rather than using the cursor. Any suggestion? What I am trying to do is whenever a trigger of TABLE1 is fired (when a row is inserted/updated), I want to jump to TABLE2 another table and get all the records that are associated with that record in TABLE1 (through foreign key) and then loop through those results to call a procedure to do something with each row.
Jul 29 '10 at 12:26 AM
johnshaddad
Any ideas?
Jul 29 '10 at 12:44 AM
johnshaddad
Really wouldn't do this in the trigger, move the logic out to the place where the insert/update is performed - hopefully a stored procedure.
Jul 29 '10 at 12:56 AM
Kev Riley ♦♦
using cursor is not efficient at all. You should do whatever you need to do in that stored procedure inside the trigger code
Jul 29 '10 at 12:57 AM
Squirrel
Ouch - sending mail from a cursor inside a trigger. Honestly, your best bet is to re-think this process. Inside the trigger simply insert the data that you need to send the email into another table, then have another process that runs off-line to send the email. What do you mean, exactly, when you say that it makes the UPDATE/INSERT 'not work'?
Jul 29 '10 at 01:52 AM
Matt Whitfield ♦♦
(comments are locked)
|


is the posted trigger code complete ? or is it just a section of it ? Can you post the full trigger code ?
There you go, I modified the question and posted the full code for the trigger.