Interview Questions
Interview Questions
- A trigger in SQL Server is a special type of stored procedure that automatically executes in response to
certain database events, such as INSERT, UPDATE, or DELETE operations occurring on a table.
2. What is the maximum depth level for nested triggers?
- By default, SQL Server allows up to 32 levels of nested triggers. However, this limit can be configured
using the nested triggers server configuration option.
3. What are the special tables used by Triggers in SQL Server?
- Triggers in SQL Server have access to two special tables: `inserted` and `deleted`. The `inserted` table
contains copies of the affected rows after an INSERT or UPDATE operation, while the `deleted` table
contains copies of the affected rows before a DELETE or UPDATE operation.
4. What is a DML Trigger in SQL Server?
- DML (Data Manipulation Language) triggers in SQL Server are triggers that fire in response to
INSERT, UPDATE, or DELETE operations on a table.
5. What are DDL triggers in SQL Server?
- DDL (Data Definition Language) triggers in SQL Server are triggers that fire in response to Data
Definition Language events, such as CREATE, ALTER, or DROP statements on a database or table.
6. If a trigger aborts due to a run-time error and no exception handler exists, what will occur?
- If a trigger aborts due to a run-time error and no exception handler exists, the entire transaction will be
rolled back.
7. Which T-SQL statement can you include in the code of the trigger to indicate a normal exit from the
trigger?
- The `RETURN` statement can be used to indicate a normal exit from a trigger.
8. If there is an AFTER INSERT trigger on a table, how many times will that trigger fire if you insert 50
rows using a single INSERT?
- The trigger will fire once for each operation performed, so if 50 rows are inserted using a single
INSERT statement, the trigger will fire once.
9. If a DML trigger fires and executes another DML that also contains a trigger, what will happen to that
second trigger, will it fire or not?
- Yes, the second trigger will fire. Triggers can cascade, meaning that if one trigger fires as a result of a
DML operation, and that trigger in turn performs another DML operation that has a trigger, the second
trigger will also fire.
10. What command explicitly fires a trigger, means can we fire a trigger forcefully?
- There is no specific command to forcefully fire a trigger. Triggers are designed to automatically fire in
response to specific events.
11. How many triggers are possible per table?
- In SQL Server, you can define multiple triggers per table for different events like INSERT, UPDATE,
DELETE, etc. However, there can be only one trigger defined per specific event per table.
12. 12. How can you use @@ROWCOUNT to improve the performance of a trigger?
- You can use @@ROWCOUNT to determine the number of rows affected by the triggering action and
take appropriate action based on that count, potentially improving performance by avoiding
unnecessary processing.
13. 13. What is the difference between Trigger and Stored Procedure?
- Triggers are automatically executed in response to certain events, such as INSERT, UPDATE, or
DELETE operations on a table, while stored procedures are explicitly called by users or applications to
perform specific tasks.
- Stored procedures can be used for various purposes including data manipulation, business logic
implementation, batch processing, security enforcement, and improving performance through
precompiled execution plans.
14. What are the advantages and disadvantages of using a stored procedure?
- Advantages of stored procedures include improved performance, code reusability, better security
controls, and encapsulation of complex logic. Disadvantages may include increased development time,
potential for code redundancy, and limited portability across database platforms.
15. What do you understand by recursive stored procedures?
- Recursive stored procedures are procedures that call themselves either directly or indirectly. They are
used to solve problems that can be broken down into smaller instances of the same problem.
16. How will you optimize a stored procedure optimization?
- You can optimize a stored procedure by ensuring proper indexing, minimizing the use of temporary
tables, reducing the number of database calls, using appropriate data types, and optimizing query
logic.
17. What does SET NOCOUNT ON do in Stored Procedure?
- SET NOCOUNT ON is a statement used in stored procedures to suppress the message indicating the
number of rows affected by a Transact-SQL statement, which can reduce network traffic and improve
performance.
18. How will you execute stored procedures as a different user?
- You can execute stored procedures as a different user by using the `EXECUTE AS` clause when
defining the procedure, specifying the desired user or context under which the procedure should
execute. Alternatively, you can use the `EXECUTE AS` statement to execute the procedure within a
specific context for a particular execution.