//按照bb表里的单位来统计各个单位的数据到aa表.
CREATE PROCEDURE danweihz
AS
begin transaction
insert into aa
select
单位,
sum(养老缴费)养老缴费工资总额,
sum(医保缴费)医保缴费工资总额,
sum(失业缴费)失业工伤工资总额,
convert(varchar(10),getdate(),120)
from bb group by 单位
//如果发生错误,则跳回,停止执行这个存储过程
if @@error<>0 then
begin
rollback transaction
return
end
//在统计完各个单位的数据后,紧贴着在下一行对以上各个单位的数据再统计一次作为总公司的数据
//由于aa表里保存着很多日期的统计数据,所以需要以日期为条件来统计当天的数据,不让它统计到其他日期的数据.
insert into aa
select
总公司=单位,//好像这样写有问题
sum(养老缴费工资总额)养老缴费工资总额,
sum(医保缴费工资总额)医保缴费工资总额,
sum(失业工伤工资总额)失业工伤工资总额,
convert(varchar(10),getdate(),120)
from aa where 日期=convert(varchar(10),getdate(),120)//这里应该也有问题
GO
这个事务是我模仿着写的,可能不对,希望可以顺便帮我改一下.上一段的代码是正确的,主要问题是在下一段的代码.
总公司=单位
这个相当于: seelct select
单位 as 总公司
drop table 表a
go
if object_id('表b') is not null
drop table 表b
go
create table 表a(name varchar(20) not null)
create table 表b(name varchar(20))set xact_abort on
begin try
begin tran
insert into 表a select null
insert into 表b select 'c'
commit tran
end try
begin catch
rollback
end catch
--全部回滚select * from 表a
select * from 表b
where 日期=convert(varchar(10),getdate(),120)//这里应该也有问题是否有问题不好说!
如果是一个范围的话,可以用 between 开始日期 and 最后日期
也就是:
还有一般你同是日期计算的话,你看下是否不用转类型?
要不都转下!where convert(varchar(10),日期,120)=convert(varchar(10),getdate(),120)
2、全部成功后,应有事务结束语句。
应使用@日期变量,用传入参数解决查询条件。
列 'danweihuizong.单位' 在选择列表中无效,因为该列未包含在聚合函数中,并且没有 GROUP BY 子句。能私下指导一下我吗..
加到 GROUP BY字句后面去,
但是又没有看到danweihuizong你这个表,
估计你是中间改过~
aa =? danweihuizong