SQL Server定时作业job的设置方法
我写了个仓库管理系统,希望定时每个月的某一天执行月报,数据从出入库明细表里取得数据,但最近却无法执行,不知是那有问题
我的存储过过程如下:
CREATE procedure updataYueJie
as
declare @DT smallDatetime
declare @M int
set @Dt = Getdate()--取当前时间
set @M = Month(@dt)--取当前月
if (@M=1) --1 月
begin
update yuejie set yuejie.zcrk01=cryd.fx from yuejie,(select liaohao,banbenhao,sum( churushuliang)as fx from cryd where datediff(m, churushijian,getdate())<=1 and churuleixing in ('验收入库','制令入库')group by liaohao,banbenhao) as cryd where yuejie.liaohao=cryd.liaohao and yuejie.banbenhao=cryd.banbenhao
end
if (@M=2) --2 月
begin
update yuejie set yuejie.zcrk02=cryd.fx from yuejie,(select liaohao,banbenhao,sum( churushuliang)as fx from cryd where datediff(m, churushijian,getdate())<=1 and churuleixing in ('验收入库','制令入库')group by liaohao,banbenhao) as cryd where yuejie.liaohao=cryd.liaohao and yuejie.banbenhao=cryd.banbenhao
end
.
.
.
if (@M=12) --12 月
begin
update yuejie set yuejie.zcrk12=cryd.fx from yuejie,(select liaohao,banbenhao,sum( churushuliang)as fx from cryd where datediff(m, churushijian,getdate())<=1 and churuleixing in ('验收入库','制令入库')group by liaohao,banbenhao) as cryd where yuejie.liaohao=cryd.liaohao and yuejie.banbenhao=cryd.banbenhao
end
GO
SQL语句没有问题
SQL Server代理->作业中执行存储过程:
use ck
exec updataYueJie
go
操作步骤:
--管理
--SQL Server代理
--右键作业
--新建作业
--"常规"项中输入作业名称
--"步骤"项
--新建
--"步骤名"中输入步骤名
--"类型"中选择"Transact-SQL 脚本(TSQL)"
--"数据库"选择执行命令的数据库
--"命令"中输入要执行的语句:
加你的过程 ---
--确定
--"调度"项
--新建调度
--"名称"中输入调度名称
--"调度类型"中选择你的作业执行安排
--如果选择"反复出现"
--点"更改"来设置你的时间安排
然后将SQL Agent服务启动,并设置为自动启动,否则你的作业不会被执行