Skip to content
Advertisement

Trigger for INSERT to validate input data in SQL Server?

My problem:

table dbo.student has StudentID like SV001.

How can I create a trigger to check data inserted into dbo.student has a StudentID that begins with SV and the numbers in the range 000 to 100?

Example: SV099 is valid id to insert, while SV101 is not valid

Advertisement

Answer

Use SQL constraints:

Example :

Demo in db<>fiddle

Or if you want to use a trigger:

Note: you must use the inserted keyword to access the record that has just been added

Demo in db<>fiddle.

Or you can use the following trigger

Demo in db<>fiddle.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement