go --创建用户表 create table 用户表(id int identity,username varchar(6),userstate varchar(20)) insert into 用户表(username,userstate) select '张三','银牌代理商' union all select '李四','银牌代理商' union all select '王五','银牌代理商'go --创建消费表 create table 消费表(id int identity,消费金额 int,消费用户id int) insert into 消费表 select 499,1 union all select 200,2 union all select 200,3 union all select 500,2go --创建添加消费的存储过程 alter proc addfy(@userid int,@fvalue float) as begin --插入数据 insert into 消费表 select @fvalue,@userid --判断是不是银牌代理商 if((select userstate from 用户表 where id=1)='银牌代理商') begin --得到消费总额 declare @i float select @i=sum(@fvalue) from 消费表 where 消费用户id=@userid --判断是否可以升级了 if @i>1000 update 用户表 set userstate='金牌代理商' where id=@userid end endgo --1号消费300 exec addfy 1,300 --查看未升级 select * from 用户表 /* id username userstate ----------- -------- -------------------- 1 张三 银牌代理商 2 李四 银牌代理商 3 王五 银牌代理商 */ --1号再消费300 exec addfy 1,300 --查看已升级 select * from 用户表 /* id username userstate ----------- -------- -------------------- 1 张三 金牌代理商 2 李四 银牌代理商 3 王五 银牌代理商 */
go
--创建用户表
create table 用户表(id int identity,username varchar(6),userstate varchar(20))
insert into 用户表(username,userstate)
select '张三','银牌代理商' union all
select '李四','银牌代理商' union all
select '王五','银牌代理商'go
--创建消费表
create table 消费表(id int identity,消费金额 int,消费用户id int)
insert into 消费表
select 499,1 union all
select 200,2 union all
select 200,3 union all
select 500,2go
--创建添加消费的存储过程
alter proc addfy(@userid int,@fvalue float)
as
begin
--插入数据
insert into 消费表 select @fvalue,@userid
--判断是不是银牌代理商
if((select userstate from 用户表 where id=1)='银牌代理商')
begin
--得到消费总额
declare @i float
select @i=sum(@fvalue) from 消费表 where 消费用户id=@userid
--判断是否可以升级了
if @i>1000
update 用户表 set userstate='金牌代理商' where id=@userid
end
endgo
--1号消费300
exec addfy 1,300
--查看未升级
select * from 用户表
/*
id username userstate
----------- -------- --------------------
1 张三 银牌代理商
2 李四 银牌代理商
3 王五 银牌代理商
*/
--1号再消费300
exec addfy 1,300
--查看已升级
select * from 用户表
/*
id username userstate
----------- -------- --------------------
1 张三 金牌代理商
2 李四 银牌代理商
3 王五 银牌代理商
*/