欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

用sql脚本创建sqlserver数据库触发器范例语句

程序员文章站 2023-12-16 13:57:40
复制代码 代码如下: --会员表 if object_id('userinfo','u') is not null drop table userinfo go creat...
复制代码 代码如下:

--会员表
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

上一篇:

下一篇: