SQL Server trigger example

Source: Internet
Author: User
Insert trigger example createtriggertri_insertonstudentforinsertasdeclare @ student_idchar (10) select @ student_0000000001beginraiserro

Insert trigger example create trigger tri_insert on student for insert as declare @ student_id char (10) select @ student_id = s. student_id from student s inner join inserted I on s. student_id = I. student_id if @ student_id = '2013' begin raiserro

InsertTriggerExample

Create trigger tri_insert

On student

For insert

As

Declare @ student_id char (10)

Select @ student_id = s. student_id from

Student s inner join inserted I

On s. student_id = I. student_id

If @ student_id = '123'

Begin

Raiserror ('the student ID of 1 cannot be inserted! ', 16, 8)

Rollback tran

End

Go

Update Trigger Example

Create trigger tri_update

On student

For update

As

If update (student_id)

Begin

Raiserror ('the student ID cannot be modified! ', 16, 8)

Rollback tran

End

Go

Delete Trigger Example

Create trigger tri_delete

On student

For delete

As

Declare @ student_id varchar (10)

Select @ student_id = student_id from deleted

If @ student_id = 'admin'

Begin

Raiserror ('error', 16, 8)

Rollback tran

End

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.