SQL Server Trigger Before Insert

In this SQL Server tutorial, will learn how SQL Server Trigger Before Insert statement works in this SQL Server. To help you comprehend the idea better, we will also talk about and learn various examples. The complete list of subjects we will address is provided below.

  • SQL Server Trigger Before Insert
  • SQL Server Trigger Before Insert Change Value
  • SQL Server Trigger Before Insert Example
  • SQL Server Trigger Before Insert or Update
  • SQL Server Trigger Before Insert Update Delete
  • SQL Server Trigger Before Insert For Each Row
  • SQL Server Trigger Before Insert RaiseError
  • SQL Server Trigger Instead of Insert

Also, check the related post on SQL Server Triggers: SQL Server Create Trigger If Not Exists

SQL Server Trigger Before Insert

Here we will learn how to use the SQL Server INSTEAD OF trigger on the table by the query and which will be explained with the help of an illustrated example.

In SQL Server, there is no trigger as BEFORE instead we use the INSTEAD OF trigger on the table by the query. The INSTEAD OF trigger is used to skip the DML statements such as (DELETE, INSERT and UPDATE) and execute the SQL statements which are used inside the CREATE TRIGGER statement.

Here’s the syntax of the FOR TRIGGER used in the CREATE TRIGGER statement by the following query:

SYNTAX:

CREATE TRIGGER YOUR_TRIGGER_NAME
ON YOUR_TABLE_NAME 
INSTEAD OF [ INSERT | DELETE | UPDATE] 
AS 
[BEGIN]
SET NOCOUNT [ ON | OFF ]
SQL STATEMENTS
[END];

/ **** FIRST METHOD ****/

INSERT INTO YOUR_TABLE_NAME (COLUMN_1, COLUMN_N)
VALUES( VALUE_1, VALUE_2, VALUE_N);

/**** SECOND METHOD ****/

DELETE FROM YOUR_TABLE_NAME
WHERE [CONDITIONS];

/**** THIRD_METHOD ****/

UPDATE YOUR_TABLENAME
SET COLUMN_NAME= NEW_VALUE
WHERE [CONDITIONS];

Here is the syntax explanation:

  • First, we need to specify the trigger_name and optionally the schema_name to which trigger is represented in the CREATE TRIGGER statement.
  • Second, specify the your_table_name with which trigger is associated.
  • Third, specify the event such as DELETEINSERT or UPDATE statement to which trigger is released in the INSTEAD OF clause. The trigger may be used to respond to multiple events.
  • Fourth, we will place the trigger after the AS keyword. A trigger body may contain more than a TRANSACT-SQL statement.

Let’s see a sample example of the SQL Server INSTEAD OF INSERT trigger on the table by the following query:

EXAMPLE:

USE SQLSERVERGUIDES;

CREATE TRIGGER INSTEADOF_USA
ON USA_STATES
INSTEAD OF INSERT
AS 
SELECT * FROM USA_STATES
WHERE STATE_ID>=5;

INSERT INTO USA_STATES VALUES(21,'TEXAS','57848','James Kelin','Male');

As we see in the above query, we have created a trigger called INSTEADOF_USA on the USA_STATES table. And we have used the INSTEAD OF trigger on the INSERT statement which means that it will skip the INSERT statement execution and execute the SQL STATEMENTS used inside the CREATE TRIGGER statement.

It will use the SELECT statement rather than the INSERT INTO statement once the INSTEAD OF trigger is fired. The SELECT statement will retrieve all records from the USA_STATES table based on the WHERE condition. In the WHERE condition, the STATE_ID column is used with the GREATER THAN OR EQUAL TO operator to find the value greater than or equal to 5 from the STATES_USA table.

If the WHERE condition turns out to be TRUE then the SELECT statement will retrieve all records from the USA_STATES table. But if the WHERE condition gets FALSE then the SELECT statement will retrieve empty records for the result set.

Generally, the procedure of the INSERT INTO statement is that the INSERT INTO statement will insert a new record into the USA_STATES table. As the execution of the INSERT INTO statement will be done but it will not bring any output because the INSTEAD OF trigger is fired on the INSERT INTO statement.

Sql Server trigger before insert example
Example of SQL Server INSTEAD OF INSERT Trigger

We hope that you have understood how to use the “SQL Server Trigger Before Insert” by using the INSTEAD OF trigger on the table by the query. For a better exposition, we have used an example and explained it in the depth.

Read: SQL Server Drop Trigger If Exists

SQL Server Trigger Before Insert Change Value

Here we will learn and understand how to use the SQL Server INSTEAD OF trigger with the INSERT statement on the table by the following query:

EXAMPLE:

USE SQLSERVERGUIDES;

CREATE TRIGGER TR_INSTEADOF_HARVARD
ON HARVARD_UNIVERSITY
INSTEAD OF INSERT
AS 
UPDATE HARVARD_UNIVERSITY
SET STUDENT_FIRSTNAME='Chris'
WHERE STUDENT_ID=13;

INSERT INTO HARVARD_UNIVERSITY
VALUES (16,'Emma','Watson','emma.watson@aol.com','Male','2021-04-03','2022-08-03');

As we see in the overhead query, we have created a trigger called INSTEADOF_HARVARD for the HARVARD_UNIVERSITY table by using the CREATE TRIGGER statement. Then we used the INSTEAD OF trigger on the INSERT statement which means that it will skip the execution of the INSERT statement once the INSTEAD OF trigger is fired. And instead, it will run the SQL STATEMENTS used inside the CREATE TRIGGER statement.

The INSERT INTO statement has been used to insert a new record into the HARVARD_UNIVERSITY table.

In the AS clause, the UPDATE statement has been used to update and set a new string_value as Chris on the STUDENT_FIRSTNAME column which is based on the WHERE condition. In the WHERE condition, the STUDENT_ID column is used with the EQUAL TO operator to find a value equal to 13 from the HARVARD_UNIVERSITY table.

The UPDATE statement will update the new value of that column only when the WHERE condition turns out to be TRUE. If the WHERE condition turns out to be FALSE then the UPDATE statement will update and set no value for the HARVARD_UNIVERSITY table.

Sql Server trigger before insert change value example
Example of SQL Server INSTEAD OF trigger on the INSERT statement which will change the value of the table.

We hope that you have understood the subtopic “SQL Server Trigger Before Insert Change Value” by using the SQL Server INSTEAD OF trigger on the table by the query. For a better explanation, we have used an example and explained it in depth.

Read: SQL Server String_agg

SQL Server Trigger Before Insert Example

Here we will learn and understand how to use the SQL Server INSTEAD OF INSERT trigger by the following query:

EXAMPLE:

USE SQLSERVERGUIDES;

CREATE TRIGGER INSTEADOF
ON USA_STATES
INSTEAD OF INSERT
AS 
SELECT * FROM USA_STATES
WHERE STATE_ID>=10;

INSERT INTO USA_STATES VALUES(22,'BROOKLYN','57848','Steve Roger','Male');

As can be seen from the aforementioned query, we have set up a trigger on the USA_STATES table called INSTEADOF_USA. Additionally, we used the INSTEAD OF trigger on the INSERT statement, which implies that it will execute the SQL STATEMENTS included in the CREATE TRIGGER statement instead of the INSERT statement.

Once the INSTEAD OF trigger is triggered, the SELECT statement will be used instead of the INSERT INTO command. Based on the WHERE criteria, the SELECT query will extract every record from the USA_STATES table. When searching for values larger than or equal to 10 in the USA_STATES table, the WHERE condition uses the STATE_ID column and the GREATER THAN OR EQUAL TO operator.

The SELECT query will obtain all data from the USA_STATES table if the WHERE condition is TRUE. However, the SELECT statement will obtain empty records for the result set if the WHERE condition is FALSE.

Generally, the procedure of the INSERT INTO statement is that the INSERT INTO statement will insert a new record into the USA_STATES table. As the execution of the INSERT INTO statement will be done but it will not bring any output because the INSTEAD OF trigger is fired on the INSERT INTO statement.

Sql Server trigger before insert
SQL Server INSTEAD OF INSERT Trigger Example

By utilising the INSTEAD OF INSERT trigger on the table by the query, we hope you have grasped the subtopic “SQL Server Trigger Before Insert Example.” We used an example and went into great detail to provide a better explanation.

Read: Instead of Trigger In SQL Server

SQL Server Trigger Before Insert or Update

Here we will learn and understand how to use the SQL Server INSTEAD OF trigger on the INSERT and UPDATE statements on the table by the following query:

EXAMPLE:

USE SQLSERVERGUIDES;

CREATE TRIGGER INSTEADOF_USA
ON USA_STATES
INSTEAD OF INSERT, UPDATE
AS 
SELECT * FROM USA_STATES
WHERE STATE_ID>15;

INSERT INTO USA_STATES VALUES(23 ,'NEW YORK','57486','Peter Parker','Male');

UPDATE USA_STATES FULL_NAME= 'Jake Daniel';
WHERE SET STATE_ID=5

First, we have created a trigger called INSTEADOF_USA on the USA_STATES table. Then we have used the INSTEAD OF trigger on the INSERT and UPDATE statements. So, the INSTEAD OF trigger will skip the execution of the INSERT and UPDATE statement. In the AS clause, the SELECT statement will retrieve all records from the USA_STATES tables based on the WHERE condition.

In the WHERE condition, the STATE_ID column is used with the GREATER THAN operator to find the greater value than 15 from the USA_STATES table. If the WHERE condition turns out to be FALSE then the SELECT statement will execute an empty result set for the output.

The INSERT and UPDATE statements will not be able to execute because the INSTEAD OF trigger has been fired by using the CREATE TRIGGER statement. Here is what the execution of the INSERT and UPDATE statements says below:

  • The INSERT INTO statement has been used the insert a new record into the USA_STATES table.
  • The UPDATE statement has been used to update and set the string value of the FULL_NAME column as Jake Daniel with the WHERE condition. In the WHERE condition, the STATE_ID column is used with the EQUAL TO operator to find an equal value as 5 from the USA_STATES table.
    • If the WHERE condition turns out to be TRUE, the UPDATE statement will update the USA_STATES table.
    • If the WHERE condition gets a FALSE value then the UPDATE statement will not be able to update the USA_STATES table.
Sql Server trigger insert or update example
Example of SQL Server INSTEAD OF INSERT, UPDATE Trigger

We hope that you have understood the subtopic “SQL Server Trigger Before Insert Or Update” by using the INSTEAD OF INSERT, UPDATE statement on the table by the query. For a better experience, we have used an illustration and presented it in deepness.

Read: Introduction to SQL Server Trigger

SQL Server Trigger Before Insert Update Delete

We will learn and understand how to use the SQL Server INSTEAD OF trigger the INSERT, DELETE and UPDATE statements on the table by the following query:

EXAMPLE:

USE SQLSERVERGUIDES;

CREATE TRIGGER INSTEADOFDML
ON USA_STATES
INSTEAD OF INSERT, DELETE , UPDATE
AS 
SELECT * FROM USA_STATES
WHERE STATE_ID> 15;

INSERT INTO USA_STATES VALUES(21,'Florida',65248,'Tom Hank','Male');

DELETE FROM USA_STATES 
WHERE STATE_ID=9;

UPDATE USA_STATES SET STATE_NAME='Georgia'
WHERE STATE_ID=21;

As we see in the above query, we have created a trigger called INSTEADOFDML on the USA_STATES table. Then we have used the INSTEAD OF trigger on the DML statements i.e; (INSERT, DELETE and UPDATE) statements and this trigger will skip these statements and execute the SQL STATEMENTS which are used inside the AS clause.

In the SELECT statement, it will retrieve all records from the USA_STATES table which is used with the WHERE condition. In the WHERE condition, the STATE_ID column is used with the GREATER THAN operator to find a greater value than 15 from the USA_STATES table. The SELECT statement will retrieve all records of the USA_STATES table if the WHERE condition turns out to be TRUE.

The INSERT, DELETE and UPDATE statements will not be able to execute because the INSTEAD OF trigger has been fired by using the CREATE TRIGGER statement. Here is what the execution of the INSERT and UPDATE statements says below:

  • The INSERT statement has been used on the STATES_USA table to insert new records.
  • In the DELETE statement, it will delete some records from the STATES_USA table which is used with the WHERE condition. In the WHERE condition, the STATE_ID column is used with the EQUAL TO operator to find a value which is equal to 9 in the STATES_USA table.
    • If the WHERE condition turns out to be TRUE, the SELECT statement is used to retrieve all records from the STATES_USA table.
    • The SELECT statement will retrieve empty records set for the result set if the WHERE condition turns out to be TRUE.
  • In the UPDATE statement, it will update and set a new record to the STATE_NAME column as GEORGIA with the WHERE condition. In the WHERE condition, the STATE_ID column is used with the EQUAL TO operator to find a value which is equalto21 from the STATES_USA table.
    • The SELECT statement will retrieve all records from the STATES_USA table if the WHERE condition turns out to be TRUE. If the WHERE condition is FALSE then the SELECT statement will retrieve an empty record set for the output.
Sql Server trigger before insert delete update example
Example of SQL Server INSTEAD OF INSERT, DELETE, UPDATE Trigger

We hope that you have understood the subtopic “SQL Server Trigger Insert Update Delete” by using the INSTEAD OF trigger on the table by the query. For a better illustration, we have used an example and explained it in depth.

Read: Drop stored procedure SQL Server

SQL Server Trigger Before Insert For Each Row

Here we will learn and understand how to use the SQL Server INSTEAD OF trigger on the query’s INSERT statement of the table. And which will be explained with the help of an illustrated example.

In SQL Server, it doesn’t provide BEFORE INSERT and FOR EACH ROW trigger. So we have to use either INSTEAD OF INSERT or AFTER INSERT trigger to set the value. Here is an illustrated example of the INSTEAD OF trigger by the following query:

EXAMPLE:

USE SQLSERVERGUIDES;

CREATE TRIGGER INSTEADOF_USA
ON USA_STATES
INSTEAD OF INSERT 
AS 
INSERT USA_STATES
SELECT STATE_ID,STATE_NAME,STATE_ZIPCODE,
FULL_NAME,GENDER FROM INSERTED;

INSERT INTO USA_STATES VALUES
(21,'RCalifornia',56784,'Robert Downey Jr','Male');

In this preceding query, a trigger is created called INSTEADOF_USA on the USA_STATES table. We have used the INSTEAD OF trigger on the INSERT statement which means that it will skip the execution of the INSERT statement and will execute the SQL STATEMENT inside the AS clause.

In the AS clause, the INSERT SELECT statement has been used to insert a new record into the USA_STATES table by using the INSERTED pseudo table. So, when we insert one record into the USA_STATES table by using the INSERT INTO statement. The execution of the INSERT INTO statement will be done.

Sql Server trigger before insert for each row example
Example of SQL Server Trigger Before Insert For Each Row

We hope that you have understood the subtopic “SQL Server Trigger Before Insert Each Row” by using the INSTEAD OF trigger on the table by the query. For a better explanation, we have used an example and explained it in depth.

Read: Alter view in SQL Server

SQL Server Trigger Before Insert RaiseError

In this SQL Server section, we will learn and understand SQL Server INSTEAD OF trigger with the RAISERROR statement of the table by the query. And which will be explained with the help of syntax and an illustrated example.

The RAISERROR statement is used to generate your error message and it returns these messages to the application by using the same format as a system error or warning message which is generated by the SQL Server Database Engine.

In addition, the RAISERROR statement allows you to set a specific message-id, level of severity and state for the error messages. Here is the syntax of the SQL Server INSTEAD OF trigger with the RAISERROR statement of the table by the following query:

SYNTAX:

USE YOUR_DATABASE_NAME;

CREATE TRIGGER YOUR_TRIGGER_NAME
ON YOUR_TABLE_NAME
INSTEAD OF INSERT
AS 
BEGIN
{ SQL STATEMENTS}
RAISERROR ({MESSAGE_ID | MESSAGE_TEXT | @LOCAL_VARIABLE});

Here is the syntax of the RAISERROR statement for better understanding:

  • MESSAGE_ID: It is a user-defined error message number stored in the sys. message record view. To add a user-defined error message, we can use the store procedure sp_addmessage. A user-defined error message should be greater than 50,000.
    • By default, the RAISERROR message uses the message_id 50000 which will help to raise an error.

Let’s see an illustrated example of the SQL Server INSTEAD OF trigger with the RAISERROR statement on the table by the following query:

EXAMPLE:

USE SQLSERVERGUIDES;

CREATE TRIGGER TR_INSTEADOFUSA
ON USA_STATES
INSTEAD OF INSERT 
AS 
BEGIN 
RAISERROR ('Cannot insert data,table is already full',1,1)
END;

INSERT INTO USA_STATES VALUES
(21,'RCalifornia',56784,'Robert Downey Jr','Male');

In the preceding query, we have created a trigger called TR_INSTEADOFUSA on the USA_STATES table. Then we have used the INSTEAD OF trigger on the INSERT statement to skip the execution of the INSERT statement and else execute the SQL STATEMENT used between the BEGIN and END statement.

In the BEGIN statement, we have used the RAISERROR function to provide the user-defined error message on the table. As the message says “Cannot insert data, the table is already full” and complete the statement with the END keyword.

So, once the trigger is fired and used the RAISERROR function then the INSERT statement will not be able to execute and it will then provide the user-defined error in the output. The trigger is set over in this case, so running the INSERT INTO commands won’t produce any results; instead, it will, as promised, add one new record to the USA_STATES table.

Sql Server trigger before insert raiserror example
Example of SQL Server INSTEAD OF Trigger with the RAISERROR statement

We hope that you have understood how to use the SQL Server INSTEAD OF trigger with the RAISERROR statement on the table by the query. For a better answer, we have used a sample and presented it in depth.

Read: Indexed views in SQL Server

SQL Server Trigger Instead of Insert

In this SQL Server, we will learn and understand how to use the SQL Server INSTEAD OF trigger on the INSERT statement of the table by the following query:

EXAMPLE:

USE SQLSERVERGUIDES;

CREATE TRIGGER INSTEADOF
ON MIT_UNIVERSITY
INSTEAD OF INSERT
AS 
SELECT * FROM MIT_UNIVERSITY
WHERE STUDENT_ID> 999;

INSERT INTO MIT_UNIVERSITY VALUES
(1002,'Tom','Hank','hank.tom67@gmail.com','Male');

In the above query, we have created a table called INSTEADOF on the MIT_UNIVERSITY table. Then we have used the INSTEAD OF trigger on the INSERT statement which will help to skip this statement and execute the SQL STATEMENTS which are used inside the CREATE TRIGGER statement.

In the AS clause, the SELECT statement has been used to retrieve all records from the MIT_UNIVERSITY table with the WHERE condition. In the WHERE condition, the STUDENT_ID column has been used with the GREATER THAN operator to find a greater value than 999 from the MIT_UNIVERSITY table.

if the WHERE turns out to be TRUE then the SELECT statement will retrieve all records from the MIT_UNIVERSITY table. As we see that the INSTEAD trigger is fired which means that the execution of the INSERT INTO statement will be executed but it will not put a new record inside the MIT_UNIVERSITY table.

Here the INSERT INTO statement says that it will insert a new record inside the MIT_UNIVERSITY table.

Sql Server trigger instead of insert example
Example of SQL Server INSTEAD OF INSERT Trigger

We hope that you have understood how to use the INSTEAD OF trigger on the INSERT statement on the table by the query. For a better explanation, we have used an example and explained it in depth.

Also, take a look at some more SQL Server tutorials.

As a result, we discovered how to use the “SQL Server Trigger Before Insert” statements in this post. We also discussed and learnt about a range of instances to help you better understand the concept. The whole list of topics we’ll cover is given below.

  • SQL Server Trigger Before Insert
  • SQL Server Trigger Before Insert Change Value
  • SQL Server Trigger Before Insert Example
  • SQL Server Trigger Before Insert or Update
  • SQL Server Trigger Before Insert Update Delete
  • SQL Server Trigger Before Insert For Each Row
  • SQL Server Trigger Before Insert RaiseError
  • SQL Server Trigger Instead of Insert