create table t1 (name varchar(50),unit varchar(50),class varchar(50))
-- class 就只有3种(差,合格,优秀)
insert t1
select 'a' ,'1班','优秀' union all
select 'a','1班','合格' union all
select 'a','1班','较差' union all
select 'a','2班','优秀'union all
select 'a','2班','优秀'union all
select 'a','2班', '合格' union allselect 'b' ,'1班','优秀' union all
select 'b','1班','合格' union all
select 'b','1班','较差' union all
select 'b','2班','优秀'union all
select 'b','2班','优秀'union all
select 'b','2班', '合格'
表t2完全是根据表t1 得到的 以上会得到如下的t2 ,otherthing列除外create table t2 (name varchar(50),unit varchar(50),较差 int ,合格 int ,优秀 int ,otherthing varchar(50))
insert t2
select 'a','1班' ,1,1,1,'wwfefaafaf' union all
select 'a','2班',0,1,2 ,'fdasfdasfd' union all
select 'b','1班' ,1,1,1,'wwewew' union all
select 'b','2班',0,1,2 ,'eeeeee' 在表t1上定义个触发器,想得到一个汇总更新插入 在t2表也同时变化
比如 更新 t1的第一行数据 ,t2会相应的变化
在t1插入 'a' ,'1班','较差' ,t2会有变化
在t1中插入 'c' ,'4班','较差' ,t2会有增加一条 'c' ,'4班',1,0,0 ,null的行 这样的触发器有点复杂 不过理解起来不难
-- class 就只有3种(差,合格,优秀)
insert t1
select 'a' ,'1班','优秀' union all
select 'a','1班','合格' union all
select 'a','1班','较差' union all
select 'a','2班','优秀'union all
select 'a','2班','优秀'union all
select 'a','2班', '合格' union allselect 'b' ,'1班','优秀' union all
select 'b','1班','合格' union all
select 'b','1班','较差' union all
select 'b','2班','优秀'union all
select 'b','2班','优秀'union all
select 'b','2班', '合格'
表t2完全是根据表t1 得到的 以上会得到如下的t2 ,otherthing列除外create table t2 (name varchar(50),unit varchar(50),较差 int ,合格 int ,优秀 int ,otherthing varchar(50))
insert t2
select 'a','1班' ,1,1,1,'wwfefaafaf' union all
select 'a','2班',0,1,2 ,'fdasfdasfd' union all
select 'b','1班' ,1,1,1,'wwewew' union all
select 'b','2班',0,1,2 ,'eeeeee' 在表t1上定义个触发器,想得到一个汇总更新插入 在t2表也同时变化
比如 更新 t1的第一行数据 ,t2会相应的变化
在t1插入 'a' ,'1班','较差' ,t2会有变化
在t1中插入 'c' ,'4班','较差' ,t2会有增加一条 'c' ,'4班',1,0,0 ,null的行 这样的触发器有点复杂 不过理解起来不难
解决方案 »
- 如何把Image字段的数据转化成Text类型字段的数据
- Sql Server 全文索引的问题?
- 表 'SF_BRJSK' 是外联接子句的内部成员。如果该表同时也包含在常规联接子句中,那么这种情况是不允许的。
- 大哥我急!!!有一个急手的问题------如何把表1的记录查询结果变成表2 的效果
- 重装SQLserver2000失败
- 关于union的一个奇怪的问题
- 数据显示问题
- 临时表什么时间被删除??是自动删除还是需要自己手工删除,如何判断一个临时表有没有被删除??
- 如何更改DataBase的Collation Name
- 学习SQL Server是先学SQL语言还是先学SQL Server2000??
- int和varchar转换比较的问题
- 求一企业级应用存储过程,超有挑战性,只有三个表,字段极少!在线等
create table t2 (name varchar(10),unit varchar(10),较差 int ,合格 int ,优秀 int ,otherthing varchar(10))
go
create trigger tr_t1 on t1
for update,delete,insert
as
begin insert t2(name,unit,较差,合格,优秀)
select distinct name,unit,0,0,0 from inserted _i
where not exists(select 1 from t2 where _i.name = name and _i.unit = unit) update t2
set t2.较差 = t2.较差 + (select count(1) from inserted where name = t2.name and unit = t2.unit and class = '较差')
,t2.合格 = t2.合格 + (select count(1) from inserted where name = t2.name and unit = t2.unit and class = '合格')
,t2.优秀 = t2.优秀 + (select count(1) from inserted where name = t2.name and unit = t2.unit and class = '优秀') update t2
set t2.较差 = t2.较差 - (select count(1) from deleted where name = t2.name and unit = t2.unit and class = '较差')
,t2.合格 = t2.合格 - (select count(1) from deleted where name = t2.name and unit = t2.unit and class = '合格')
,t2.优秀 = t2.优秀 - (select count(1) from deleted where name = t2.name and unit = t2.unit and class = '优秀') delete t2
where 较差+合格+优秀 = 0
end
go
insert t1
select 'a' ,'1班','优秀' union all
select 'a','1班','合格' union all
select 'a','1班','较差' union all
select 'a','2班','优秀'union all
select 'a','2班','优秀'union all
select 'a','2班', '合格' union all
select 'b' ,'1班','优秀' union all
select 'b','1班','合格' union all
select 'b','1班','较差' union all
select 'b','2班','优秀'union all
select 'b','2班','优秀'union all
select 'b','2班', '合格' select * from t2
/*
name unit 较差 合格 优秀 otherthing
---------- ---------- ----------- ----------- ----------- ----------
a 1班 1 1 1 NULL
a 2班 0 1 2 NULL
b 1班 1 1 1 NULL
b 2班 0 1 2 NULL(所影响的行数为 4 行)
*/drop table t1,t2