我有两个表Table1 Table2
Table1字段为id (int) 性别(varchar) 这个表没有数据
Table2字段为性别(varchar) 人数
男 10
女 20
当在第一个表里添加一条数据后 在第二个表里加一
我要统计 每次添加完数据后 表2的男女总人数
这是我现在的代码 该怎么改 大侠帮帮忙吧
declare @xx varchar(50)
set @xx ='女'
insert into Table1 (性别) values (@xx)UPDATE Table2 SET 人数=人数+1 where 性别=@xx
Table1字段为id (int) 性别(varchar) 这个表没有数据
Table2字段为性别(varchar) 人数
男 10
女 20
当在第一个表里添加一条数据后 在第二个表里加一
我要统计 每次添加完数据后 表2的男女总人数
这是我现在的代码 该怎么改 大侠帮帮忙吧
declare @xx varchar(50)
set @xx ='女'
insert into Table1 (性别) values (@xx)UPDATE Table2 SET 人数=人数+1 where 性别=@xx
是不是在实训啊 我们今天的作业也要做触发器啊
create table t1
(id int ,
xb varchar(2)
)create table t2
(xb varchar(2),
rs int
)create trigger tt
on t1
for insert
as
declare @m varchar(2)
select @m=xb from insertd
update t2
set rs=rs+1
where xb=@m 不知道这样行吗? 我很菜的
set @xx ='女'
insert into Table1 (性别) values (@xx)UPDATE Table2 SET 人数=人数+1 where 性别=@xx我最后还要统计一下 男生+女生的总数 可是我这段代码不是啊
if object_id('[table1]') is not null drop table [table1]
create table [table1] (id int identity(1,1),性别 varchar(2))--> 测试数据: [table2]
if object_id('[table2]') is not null drop table [table2]
create table [table2] (性别 varchar(2),人数 int)
insert into [table2]
select '男',10 union all
select '女',20
go
--创建触发器
create trigger tri_a on table1 for insert
as
update table2 set 人数=人数+1 from table2 a,inserted b where a.性别=b.性别
godeclare @xx varchar(50)
set @xx ='女'
insert into Table1 (性别) values (@xx)--统计总人数
select 总人数=sum(人数) from table2/*
结果:
总人数
-----------
31
*/
你运行的是什么代码?是你之前的。如果在你的代码基础上改的话。就应该是:declare @xx varchar(50)
set @xx ='女'
insert into Table1 (性别) values (@xx)UPDATE Table2 SET 人数=人数+1 where 性别=@xx
--统计总人数
select 总人数=sum(人数) from table2