34,837
社区成员




create table 客户 (
客户名 varchar(10) not null,
级别名 varchar(10) null,
交易量 int not null,
交易额 decimal(10,2) not null,
constraint PK_客户 primary key (客户名)
)
create index 属于_FK on 客户 (
级别名 ASC
)
create table 级别 (
级别名 varchar(10) not null,
折扣率 decimal(3,2) not null,
交易量标准 int not null,
交易额标准 float not null,
constraint PK_级别 primary key (级别名)
)
alter table 客户
add constraint FK_客户_属于_级别 foreign key (级别名)
references 级别 (级别名)
insert 级别 select '普通会员',1,0,0
insert 级别 select 'VIP',0.98,5,1000
insert 级别 select '黄金会员',0.95,15,8000
insert 级别 select '白金会员',0.92,50,20000
insert 客户 select '张三','VIP',8,1000
insert 客户 select '林清','普通会员',2,1500
insert 客户 select '王风','VIP',10,16000
go
create trigger trigger_update
on 客户
for update
as
if Update(交易量) or Update(交易额)
begin
update 客户
set 级别名 = (select top 1 级别名 from 级别 where i.交易量 >= 交易量标准 and i.交易额 >= 交易额标准 order by 交易量标准 desc)
from 客户 a
right join inserted i
on a.客户名 = i.客户名
end
go
select * from 级别
select * from 客户
update 客户
set 交易量 = 4
where 客户名 = '张三'
select * from 客户
update 客户
set 交易额 = 20100
where 客户名 = '张三'
select * from 客户
update 客户
set 交易量 = 51
where 客户名 = '张三'
select * from 客户
drop table 客户,级别
/*
客户名 级别名 交易量 交易额
---------- ---------- ----------- ------------
林清 普通会员 2 1500.00
王风 VIP 10 16000.00
张三 VIP 8 1000.00
(所影响的行数为 3 行)
(所影响的行数为 1 行)
(所影响的行数为 1 行)
客户名 级别名 交易量 交易额
---------- ---------- ----------- ------------
林清 普通会员 2 1500.00
王风 VIP 10 16000.00
张三 普通会员 4 1000.00
(所影响的行数为 3 行)
(所影响的行数为 1 行)
(所影响的行数为 1 行)
客户名 级别名 交易量 交易额
---------- ---------- ----------- ------------
林清 普通会员 2 1500.00
王风 VIP 10 16000.00
张三 普通会员 4 20100.00
(所影响的行数为 3 行)
(所影响的行数为 1 行)
(所影响的行数为 1 行)
客户名 级别名 交易量 交易额
---------- ---------- ----------- ------------
林清 普通会员 2 1500.00
王风 VIP 10 16000.00
张三 白金会员 51 20100.00
(所影响的行数为 3 行)
*/
create table 客户 (
客户名 varchar(10) not null,
级别名 varchar(10) null,
交易量 int not null,
交易额 decimal(10,2) not null,
constraint PK_客户 primary key (客户名)
)
create index 属于_FK on 客户 (
级别名 ASC
)
create table 级别 (
级别名 varchar(10) not null,
折扣率 decimal(3,2) not null,
交易量标准 int not null,
交易额标准 float not null,
constraint PK_级别 primary key (级别名)
)
alter table 客户
add constraint FK_客户_属于_级别 foreign key (级别名)
references 级别 (级别名)
insert 级别 select '普通会员',1,0,0
insert 级别 select 'VIP',0.98,5,1000
insert 级别 select '黄金会员',0.95,15,8000
insert 级别 select '白金会员',0.92,50,20000
insert 客户 select '张三','VIP',8,1000
insert 客户 select '林清','普通会员',2,1500
insert 客户 select '王风','VIP',10,16000
go
create trigger trigger_update
on 客户
for update
as
if Update(交易量)
begin
update 客户
set 级别名 = (select top 1 级别名 from 级别 where i.交易量 > 交易量标准)
from 客户 a
left join inserted i
on a.客户名 = i.客户名
end
else if Update(交易额)
begin
update 客户
set 级别名 = (select top 1 级别名 from 级别 where i.交易额 > 交易额标准)
from 客户 a
left join inserted i
on a.客户名 = i.客户名
end
go
select * from 级别
select * from 客户
update 客户
set 交易量 = 4
where 客户名 = '张三'
select * from 客户
update 客户
set 交易额 = 1100
where 客户名 = '张三'
select * from 客户
drop table 客户,级别
/*
客户名 级别名 交易量 交易额
---------- ---------- ----------- ------------
林清 普通会员 2 1500.00
王风 VIP 10 16000.00
张三 VIP 8 1000.00
(所影响的行数为 3 行)
(所影响的行数为 3 行)
(所影响的行数为 1 行)
客户名 级别名 交易量 交易额
---------- ---------- ----------- ------------
林清 NULL 2 1500.00
王风 NULL 10 16000.00
张三 普通会员 4 1000.00
(所影响的行数为 3 行)
(所影响的行数为 3 行)
(所影响的行数为 1 行)
客户名 级别名 交易量 交易额
---------- ---------- ----------- ------------
林清 NULL 2 1500.00
王风 NULL 10 16000.00
张三 VIP 4 1100.00
(所影响的行数为 3 行)
*/
create table 客户 (
客户名 varchar(10) not null,
级别名 varchar(10) null,
交易量 int not null,
交易额 decimal(10,2) not null,
constraint PK_客户 primary key (客户名)
)
create index 属于_FK on 客户 (
级别名 ASC
)
create table 级别 (
级别名 varchar(10) not null,
折扣率 decimal(3,2) not null,
交易量标准 int not null,
交易额标准 float not null,
constraint PK_级别 primary key (级别名)
)
alter table 客户
add constraint FK_客户_属于_级别 foreign key (级别名)
references 级别 (级别名)
insert 级别 select '普通会员',1,0,0
insert 级别 select 'VIP',0.98,5,1000
insert 级别 select '黄金会员',0.95,15,8000
insert 级别 select '白金会员',0.92,50,20000
insert 客户 select '张三','VIP',8,1000
insert 客户 select '林清','普通会员',2,1500
insert 客户 select '王风','VIP',10,16000
go
create trigger trigger_update
on 客户
for update
as
if Update(交易量)
begin
update 客户
set 级别名 = (select top 1 级别名 from 级别 where i.交易量 > 交易量标准)
from 客户 a
left join inserted i
on a.客户名 = i.客户名
end
go
select * from 级别
select * from 客户
update 客户
set 交易量 = 4
where 客户名 = '张三'
select * from 客户
drop table 客户,级别
/*
客户名 级别名 交易量 交易额
---------- ---------- ----------- ------------
林清 普通会员 2 1500.00
王风 VIP 10 16000.00
张三 VIP 8 1000.00
(所影响的行数为 3 行)
(所影响的行数为 3 行)
(所影响的行数为 1 行)
客户名 级别名 交易量 交易额
---------- ---------- ----------- ------------
林清 NULL 2 1500.00
王风 NULL 10 16000.00
张三 普通会员 4 1000.00
*/
create table 级别 (
级别名 varchar(10) not null,
折扣率 float not null,
交易量标准 int null,
交易额标准 float null
)
create table 客户 (
客户名 varchar(10) not null,
交易额 float null,
交易量 int null,
级别名 varchar(10)
)
go
insert 级别 select 'a',10,100,1000
insert 级别 select 'b',10,1000,100000
insert 客户 select 'asdf',0,0,NULL
go
create trigger trigger_update
on 客户
for update
as
if Update(交易额)
begin
update 客户
set 级别名 = (select top 1 级别名 from 级别 where i.交易量<交易量标准 and i.交易额<交易额标准)
from 客户 a
left join inserted i
on a.客户名 = i.客户名
end
go
update 客户 set 交易额 = 1000,交易量 = 100
select * from 客户
/*
客户名 交易额 交易量 级别名
---------- ----------------------------------------------------- ----------- ----------
asdf 1000.0 100 b
(所影响的行数为 1 行)
*/
drop table 客户,级别