Use SQL scripts to create SQL Server database trigger example statements

Source: Internet
Author: User

Copy codeThe Code is as follows:
-- 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
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.