Get the SQL query from a Trigger in SQL Server 2008 - sql-server-2008

In SQL Server 2008, let's say I have a table with a view and INSTEAD OF triggers on it.
When I insert or update, is there a way inside the trigger to determine the SQL query that was used to invoke the trigger?
For example, if I do the following:
UPDATE MyView SET Address = '123 Main St'
Is there a way inside the trigger to get the actual SQL query that was used, i.e.,
UPDATE MyView SET Address = '123 Main St'
I hope this makes sense.
Thanks.

No, the trigger isn't invoked by that statement - the triggers is fired from the RDBMS because the condition it's defined for (INSERT, UPDATE or DELETE) has occurred.
The trigger is totally asynchronous from the actual statement, and no, you cannot get the T-SQL query that caused the trigger to fire. All you can rely on are the Inserted and Deleted pseudo tables that are available inside the trigger which tell you what rows were inserted, deleted, updated.

Related

What is proper way to set and compare variable inside an sql trigger

Am populating a table using a trigger after an insert event occurs on another table and that worked fine. However i then noticed that the trigger would still insert a new row for existing records. To fix this, I want to create the trigger again but this time it would only fire if a condition is met...but not having previously used triggers in the past am getting a syntax error and not able to identify what am doing wrong. Kindly have a look and help me fix this
CREATE TRIGGER `students_gen_insert`
AFTER INSERT ON `students` FOR EACH ROW
BEGIN
INSERT INTO records (student_id, subject_id)
SELECT new.student_id, subjects.subject_id
FROM subjects
WHERE category = new.class;
END;
Am currently using MySql 5.6.17 version.
It is generally not a good idea to SELECT from the table the trigger is on, and forbidden to UPDATE or INSERT (not that you are doing those). Assuming you are trying to get the values for the row just inserted, the first SET ... SELECT you have is needless; just use NEW.fieldname to get the fields of the inserted row.
The second SET ... SELECT and following condition are a bit confusing. If referential integrity is being maintained, I would think it would be impossible for the records table to refer to that particular student_id of the students table at the point the trigger is executed. Perhaps this was to avoid the duplicate inserts from the trigger's previous code? If so, it might help for you to post that so we can pinpoint the actual source of redundant inserts.

SQL Server trigger not working while inserting values

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.

SQL Server - After Insert/ For Insert - Rollback

I have the below trigger:
CREATE Trigger instructor_expertise on CourseSections
After Insert
As Begin
......
If (Not Exists(Select AreaName From AreasOfInstructor Where (InstructorNo = #InstructorNo AND AreaName = #AreaName)))
Begin
RAISERROR('Course not in instructors expertise', 16, 1)
rollback transaction
End
GO
My question is, does 'rollback transaction' remove the row?
What if it's 'For Insert' instead, does 'rollback transaction' remove the row in that case?
Thanks!!!
Your INSERT statement always runs in a transaction - either you've explicitly defined one, or if not, then SQL Server will use an implicit transaction.
You're inserting one (or multiple) row into your table. Then - still inside the transaction - the AFTER INSERT trigger runs and checks certain conditions - typically using the Inserted pseudo table available inside the trigger, which contains the rows that have been inserted.
If you call ROLLBACK TRANSACTION in your trigger, then yes - your transaction, with everything it's been doing, is rolled back and it's as if that INSERT never happened - nothing shows up in your database table.
Also: FOR INSERT is the same as AFTER INSERT in SQL Server - the trigger is executed after the INSERT statement has done its job.
One thing to keep in mind (which a lot of programmers get wrong): the trigger is fired once per statement - NOT once per row! So if you insert 20 rows at once, the trigger is fired once and the Inserted pseudo table inside the trigger contains 20 rows. You need to take that into account when writing the trigger - you're not always dealing with just a single row being inserted!
no it is not possible because when their is no row exist then it will go in begin block ...

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.

SQL Server trigger latency?

I have created a trigger for a table in SQL Server 2008
CREATE TRIGGER trigger_mytable
ON dbo.mytable
FOR INSERT, DELETE, UPDATE
AS
EXTERNAL NAME mycode.[trig.mytable].myfn
The code for mytable.myfn is in C#. It opens a connection to the database and queries the inserted or deleted table based on the event to get the trigger data. Can it be possible that an entry is inserted and immediately deleted (in like a fraction of a second or say ms) from the table and the trigger function is never called? In short can there be a latency or absence of trigger? My understanding is that triggers use interrupt sort of mechanism rather than polling. I am very new to SQL Server and triggers.
No: there is no latency or lag
The trigger is part of the INSERT and DELETE statement. When the trigger completes, then SQL Server reports the INSERT or DELETE as complete
Note: you'd typically keep triggers short and concise and not use external code or calls