日期:2014-05-18  浏览次数:20380 次

求一简单的查询插入存储过程
有1张统计表 A ()
time 日期
total 总数
vis0 状态0
vis1 状态1
vis2 状态2
type 表名

7张信息表 B,C,D,E,F,G,H
结构如下

vis 状态 
date 日期

想实现如下功能在每天晚上0点统计 表B,C,D,E,F,G,H 里当天发布了多少条信息 每种状的信息是多少条然后插入到A表做查询用
请各位大哥帮忙!!!
例如

  time total vis0 vis1  vis2 type 
2008-4-8 100 20 30 50 表A
2008-4-8 100 20 30 50 表B



------解决方案--------------------
需要使用作业.
------解决方案--------------------
企业管理器
--管理
--SQL Server代理
--右键作业
--新建作业
--"常规"项中输入作业名称
--"步骤"项
--新建
--"步骤名"中输入步骤名
--"类型"中选择"Transact-SQL 脚本(TSQL)"
--"数据库"选择执行命令的数据库
--"命令"中输入要执行的语句:
EXEC 存储过程名 ...

--确定
--"调度"项
--新建调度
--"名称"中输入调度名称
--"调度类型"中选择你的作业执行安排
--如果选择"反复出现"
--点"更改"来设置你的时间安排


然后将SQL Agent服务启动,并设置为自动启动,否则你的作业不会被执行

设置方法:
我的电脑--控制面板--管理工具--服务--右键 SQLSERVERAGENT--属性--启动类型--选择"自动启动"--确定.


------解决方案--------------------
SQL code
insert a(time,total,vis0,vis1,vis2,type)
select convert(varchar(10),[date],120) as Time,
   count(1) as total,
   sum(case vis when 0 then 1 else 0 end) as vis0,
   sum(case vis when 0 then 1 else 0 end) as vis1,
   sum(case vis when 0 then 1 else 0 end) as vis2,
   'B' as type
from B
where [Date]>=convert(varchar(10),getdate(),120)
and [Date]<dateadd(day,1,convert(varchar(10),getdate(),120))
union all 
select convert(varchar(10),[date],120) as Time,
   count(1) as total,
   sum(case vis when 0 then 1 else 0 end) as vis0,
   sum(case vis when 0 then 1 else 0 end) as vis1,
   sum(case vis when 0 then 1 else 0 end) as vis2,
   'C' as type
from C
where [Date]>=convert(varchar(10),getdate(),120)
and [Date]<dateadd(day,1,convert(varchar(10),getdate(),120))
union all 
select convert(varchar(10),[date],120) as Time,
   count(1) as total,
   sum(case vis when 0 then 1 else 0 end) as vis0,
   sum(case vis when 0 then 1 else 0 end) as vis1,
   sum(case vis when 0 then 1 else 0 end) as vis2,
   'D' as type
from D
where [Date]>=convert(varchar(10),getdate(),120)
and [Date]<dateadd(day,1,convert(varchar(10),getdate(),120))
union all 
select convert(varchar(10),[date],120) as Time,
   count(1) as total,
   sum(case vis when 0 then 1 else 0 end) as vis0,
   sum(case vis when 0 then 1 else 0 end) as vis1,
   sum(case vis when 0 then 1 else 0 end) as vis2,
   'E' as type
from E
where [Date]>=convert(varchar(10),getdate(),120)
and [Date]<dateadd(day,1,convert(varchar(10),getdate(),120))
union all 
select convert(varchar(10),[date],120) as Time,
   count(1) as total,
   sum(case vis when 0 then 1 else 0 end) as vis0,
   sum(case vis when 0 then 1 else 0 end) as vis1,
   sum(case vis when 0 then 1 else 0 end) as vis2,
   'F' as type
from F
where [Date]>=convert(varchar(10),getdate(),120)
and [Date]<dateadd(day,1,convert(varchar(10),getdate(),120))
union all 
select convert(varchar(10),[date],120) as Time,
   count(1) as total,
   sum(case vis when 0 then 1 else 0 end) as vis0,
   sum(case vis when 0 then 1 else 0 end) as vis1,
   sum(case vis when 0 then 1 else 0 end) as vis2,
   'G' as type
from G
where [Date]>=convert(varchar(10),getdate(),120)
and [Date]<dateadd(day,1,convert(varchar(10),getdate(),120))
union all 
select convert(varchar(10),[date],120) as Time,
   count(1) as total,
   sum(case vis when 0 then 1 else 0 end) as vis0,
   sum(case vis when 0 then 1 else 0 end) as vis1,
   sum(case vis when 0 then 1 else 0 end) as vis2,
   'H' as type
from H
where [Date]>=convert(varchar(10),getdate(),120)
and [Date]<da