Example of creating SQL Server database trigger script

Source: Internet
Author: User
  --  Member table  
If Object_id ( ' Userinfo ' , ' U ' ) Is Not Null
Drop Table Userinfo
Go
Create Table Userinfo (userid Int Primary Key , User_tegral Int , Level Int )
Insert Into Userinfo Select 1 , 0 , 0
Go
-- Member level table
If Object_id ( ' Userlevel ' , ' U ' ) Is Not Null
Drop Table Userlevel
Go

-- Insert Test Data
Create Table Userlevel ( Level Int Primary Key , Mlevel_point Decimal ( 10 , 2 ))
Insert Into Userlevel Select 0 , 0
Insert Into Userlevel Select 1 , 100
Insert Into Userlevel Select 2 , 200
Insert Into Userlevel Select 3 , 300
Go



-- Trigger
Create Trigger Tr_userinfor
On Userinfo For Update
As
Begin

Update A
Set A. Level = B. Level
From Userinfo A, userlevel B
Where A. userid In ( Select Userid From Inserted) And A. user_tegral > = B. mlevel_point And
A. user_tegral < ( Select Min (Mlevel_point)
From Userlevel Where Mlevel_point > B. mlevel_point)
End

-- Test
Update Userinfo
Set User_tegral = 100
Where Userid = 1

Select * From Userinfo


-- Another trigger
Set Ansi_nulls On
Set Quoted_identifier On
Go
Alter Trigger [ Altername ]
On [ DBO ] . [ Fs_user ]
For Insert
As
Begin
Set Nocount On ;
Update DBO. fs_user Set Uname = ( Select Uname From Inserted) + ' @ ML '
Where ID In ( Select ID From Inserted)
End

 

Related Article

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.