Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server 2008 - Does a trigger run with the same permissions as the login/user?

Just a quick question:

Say I put an insert trigger on a table in my database.

If data is inserted into that table through a login/user "foobar".

Does the trigger execute with the same access rights / permissions as "foobar"?

Many thanks.

like image 440
Chris Cannon Avatar asked Apr 05 '12 15:04

Chris Cannon


People also ask

Why you shouldn't use database triggers?

In my experience, they are not a good idea because they can result in surprising side effects, and are difficult to debug (especially when one trigger fires another). Often developers do not even think of looking if there is a trigger.

What is the trigger for login?

Logon triggers fire stored procedures in response to a LOGON event. This event is raised when a user session is established with an instance of SQL Server. Logon triggers fire after the authentication phase of logging in finishes, but before the user session is actually established.

Which of the following specifies when the trigger will be executed?

Row-level trigger is identified by the FOR EACH ROW clause in the CREATE TRIGGER command. Statement-level triggers execute once for each transaction. For example, if a single transaction inserted 500 rows into the Customer table, then a statement-level trigger on that table would only be executed once.

How a trigger is executed?

To invoke a trigger, a user must have the privilege to execute the data change statement associated with the trigger event. Similarly, to successfully invoke an SQL routine or dynamic compound statement a user must have the EXECUTE privilege on the routine.


1 Answers

Yes.

You can control this behaviour with the

EXECUTE AS

clause of the create statement, as explained here.

The default for triggers is

EXECUTE AS CALLER

where we find

CALLER

Specifies the statements inside the module are executed in the context of the caller of the module. The user executing the module must have appropriate permissions not only on the module itself, but also on any database objects that are referenced by the module. CALLER is the default for all modules except queues, and is the same as SQL Server 2005 behavior.

like image 198
Matten Avatar answered Sep 29 '22 00:09

Matten