SQL Server trigger not working while inserting values - sql-server-2008

Is it possible the insert trigger is not run while the values are inserted on the table? Anybody was experience this?

No it's not possible, if you have the trigger set up correctly. Are you sure you've indicated you want the trigger to fire on an Insert? You have the option of specifying a trigger to fire on an Insert, Update, Delete or any combination.
Or the trigger could be disabled. Or there could be a Return statement in the first line of the trigger. I've seen people do that, as a way to disable a trigger.
Also, if you are inserting multiple rows into the table, you need to make sure your trigger is created correctly to handle that. Handling multiple inserts can be a bit more complicated than handling a single insert, and unexpected results could occur if you are not aware of the difference.

Related

Query related to trigger fire in SQL database?

Is it possible to fire a trigger when a row value in the table equals a certain value? Like if a value in the table hits zero, I want a certain DML operation to occur using a trigger, is this possible?
Triggers fire based on events on DB objects. For example you can define a trigger to fire before or after insert, update or delete events on tables. What you do in the trigger implementation is entirely up to you, DB will make old record (for update and delete) and new record (for insert and update) available to you.
For your requirement, you can implement a logic that will check for the attribute's value and if it hits zero, you perform the DML operation else just return without doing anything.

TRIGGER: Read NEW.values and OLD.values during "ON DUPLICATE KEY UPDATE"

I'm trying to write a mini-auditing system for individual tables in MySQL.
I had good luck working with basic INSERT/UPDATE/DELETE commands, however now a table I'm interested in auditing uses INSERT ... ON DUPLICATE KEY UPDATE.
By using a trigger ON BEFORE INSERT I can tell this event is occurring, however I can only get half the data I'm interested in. NEW.values are readily available, but I've no idea how to get the OLD.values that came before. I suppose I could do a query using the NEW.ID in existing table, but I'm not sure about performance and reliability.
I need the OLD.values because I'm storing both old and new values for each change event since I read somewhere that was a good idea for collapsing data etc...
Is there a way in MySQL 5.0 (or newer GA release) to reliably retrieve these values as though I were in an UPDATE trigger?
EDIT: Another wrinkle:
Looks like the NEW.values do not match the data after update. They match the INSERT statement not the ON DUPLICATE KEY UPDATE data that will actually go into the record.
It looks like the trigger event ON AFTER UPDATE also catches the ON DUPLICATE KEY UPDATE change. From here, I was able to get OLD/NEW values and perform the logging I needed to perform.

sql : Enable and Disable Triggers

I have trigger on a table which is written longtime back and can’t retire or modify at this moment. There are lot of select statements are there which get fired irrespective of any condition in this trigger.
Now I have another Stored Procedure which will update the two columns in the above mentioned table and I don’t want any other operation or any queries which were written in the trigger needs to be fired when this operation ( calling SP) is performed.
So I though before I call the update statement in this stored procedure, I disable the update trigger on this table and once I done with update statement will again enable the trigger .
Is this is good idea ? Any issues with this approach? I will do this operation in transaction so that if anything goes wrong , it will come back to original stage .
You can disable/enable a trigger by hand.
It is a good idea, as long as you are sure that the trigger does not update some other field or table and if no other job launching that trigger may run at the same time.

MySQL trigger which triggers on either INSERT or UPDATE?

Is there a way to create MySQL trigger which triggers on either UPDATE or INSERT?
Something like
CREATE TRIGGER t_apps_affected BEFORE INSERT OR UPDATE ...
Obviously, the above don't work. So, any workarounds without creating two separate triggers?
I need this in order to update running counter on another table.
Unfortunately, there is no shorthand form - you must create multiple triggers - one for each event.
The doc says:
trigger_event indicates the kind of statement that activates the trigger. The trigger_event can be one of the following:
INSERT: The trigger is activated whenever a new row is inserted into
the table; for example, through INSERT, LOAD DATA, and REPLACE
statements.
UPDATE: The trigger is activated whenever a row is modified; for
example, through UPDATE statements.
DELETE: The trigger is activated whenever a row is deleted from the
table; for example, through DELETE and REPLACE statements. However,
DROP TABLE and TRUNCATE TABLE statements on the table do not activate
this trigger, because they do not use DELETE. Dropping a partition
does not activate DELETE triggers, either. See Section 12.1.27,
“TRUNCATE TABLE Syntax”.
While it is impossible to put a trigger on multiple events, you can define the two triggers to merely call another stored procedure and, with that, cut down on the amount of code you need to commit. Just create the separate triggers to do nothing but, say,
CALL update_counter();
and put all of your actual work into the procedure. The triggers would then be a simple
CREATE TRIGGER t_apps_affected BEFORE INSERT ON table
FOR EACH ROW
BEGIN
CALL update_counter();
END;

how to activate trigger after all the bulk insert(s) in mysql

I am using mysql and there are bulk inserts that goes on to my table.
My doubt is if I create a trigger specifying after insert, then the trigger will get activated for every insert after, which I do not want to happen.
Is there any way to activate a trigger after all the bulk inserts are completed?
Any advice?
Thanks.
If your concern is performance, you can rest assured that the operation is pretty fast, even though it is performed on each inserted row separately. But if you really think this will lead to performance problems, you should profile.
The single alternative I can think of is dropping the trigger, performing the insert query, and re-adding the trigger, which is actually a horrible solution (unfortunately you cannot disable triggers for the session in mysql - actually you cannot disable them at all).
take a look here and see if you can implement this trick that basically wraps the trigger in an if statement controlled by a variable you can switch on and off
if (#DISABLE_TRIGER <> 1 ) then
#trigger body
end if;
and than if you want to disable triggers on import just:
SET #DISABLE_TRIGER = 1;
#do imports
SET #DISABLE_TRIGER = 0;
The trigger syntax (>>after insert on<<) inherently says that it will be done for every insert. I don't know of a way to switch that off. A possible workaround could be to have a flag (some magic value for one of the columns e.g id column of -200?) in your insert statement that is only true for the very last insert and then have an if statement iside your trigger that checks the flag. Feels a bit like a hack though. Perhaps you can give us a bit more context, there may be a better way of skinning this cat.