复制代码代码如下:--会员表ifobject_id('userinfo','u')isnotnulldroptableuserinfogocreatetableuserinfo(useridintprimarykey,user_tegralint,levelint)insertintouserinfoselect1,0,0go--会员等级表ifobject_id('userlevel','u')isnotnulldroptableuserlevelgo--插入测试数据createtableuserlevel(levelintprimarykey,mlevel_pointdecimal(10,2))insertintouserlevelselect0,0insertintouserlevelselect1,100insertintouserlevelselect2,200insertintouserlevelselect3,300go--触发器createtriggerTR_userInforonuserinfoforupdateasbeginupdateaseta.level=b
--会员表
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
--会员等级表
if object_id('userlevel','u') is not null
drop table userlevel
go
--插入测试数据
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
--触发器
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
--测试
update userinfo
set user_tegral=100
where userid=1
select * from userinfo
--另外一个触发器
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
标签: sql 脚本 创建 sqlserver 数据库 触发器 范例 语句
声明:本文内容来源自网络,文字、图片等素材版权属于原作者,平台转载素材出于传递更多信息,文章内容仅供参考与学习,切勿作为商业目的使用。如果侵害了您的合法权益,请您及时与我们联系,我们会在第一时间进行处理!我们尊重版权,也致力于保护版权,站搜网感谢您的分享!