sqlserver 定时执行存储过程

a276620535 2013-10-30 02:56:02
如果我想插入一条记录 ,表A有一个bid是B表的外键,A表的sta 状态是1的时候
表B里有adate的时间的字段,当系统时间大于B表的adate时候并且A表的sta状态是1
就自动往C表里添加一条数据
...全文
713 26 打赏 收藏 转发到动态 举报
写回复
用AI写文章
26 条回复
切换为时间正序
请发表友善的回复…
发表回复
a276620535 2013-11-01
  • 打赏
  • 举报
回复
多谢诸位 已经搞定
刘兄弟 2013-10-31
  • 打赏
  • 举报
回复
A表添加插入标记
tcmakebest 2013-10-30
  • 打赏
  • 举报
回复
往C表插了记录后,那个条件还是成立的,所以死循环了吧
LongRui888 2013-10-30
  • 打赏
  • 举报
回复
就是这个:


--1.新建作业 --> 作业步骤 --> 作业调度 --> 作业服务器
declare @jobname sysname,@db_name sysname


select @jobname = 'auto_exec_proc',
       @db_name = db_name()


--1.1创建作业
exec msdb..sp_add_job 
	@job_name=@jobname


--1.2创建作业步骤
exec msdb..sp_add_jobstep 
	@job_name=@jobname,
	@step_name = N'数据处理',
    @subsystem = N'TSQL',
    @database_name=@db_name,
    @command = N'exec 你的存储过程;',
    @retry_attempts = 1, 
    @retry_interval = 1  


--1.3添加作业步骤
EXEC msdb..sp_add_jobschedule 
	@job_name=@jobname, 
    @name = N'自动执行存储过程',
    @freq_type=4 , 
    @freq_interval=1,
	@active_start_time=200000


--1.4添加目标服务器
EXEC msdb.dbo.sp_add_jobserver 
	@job_name = @jobname ,  
	@server_name = '你的服务器名称' -- select @@servername 这个函数的返回值不一定准确 

	


a276620535 2013-10-30
  • 打赏
  • 举报
回复
因为时间不确定 而且触发器好像在添加的时候执行一次
a276620535 2013-10-30
  • 打赏
  • 举报
回复
引用 19 楼 yupeigu 的回复:
[quote=引用 18 楼 a276620535 的回复:] [quote=引用 17 楼 yupeigu 的回复:] 你看看,是这样吗:

drop table a
drop table b
drop table c
go


create table b
(bid int identity(1,1) primary key,bdate datetime)


create table a
(aid int identity(1,1) primary key,
bid int foreign key references b(bid),
sta int,
adate datetime
)

create table c
(cid int identity(1,1) primary key,cdate datetime)

insert into b
select '2013-10-30 10:10:10' union all
select '2013-10-30 21:10:10'



insert into a
values(1,1,getdate())


create trigger dbo.trigger_a_insert
on dbo.a
for insert
as
   if exists(select 1 from inserted i
             inner join dbo.b 
                     on i.bid = b.bid
             where i.sta = 1 and b.bdate < getdate())
      
      insert into c values(getdate())   
go

--1.触发,c表插入1条记录
insert into a(bid,sta,adate) values(1,1,getdate())

select * from c
/*
cid	cdate
1	2013-10-30 16:02:20.943
*/

--2.不触发,c表不会插入记录,还是1条
insert into a(bid,sta,adate) values(1,0,getdate())

select * from c
/*
cid	cdate
1	2013-10-30 16:02:20.943
*/

--2.不触发,由于bid=2时,bdate大于当前时间,所以不插入,还是1条
insert into a(bid,sta,adate) values(2,1,getdate())

select * from c
/*
cid	cdate
1	2013-10-30 16:02:20.943
*/
多谢 大概就是这样了 再问下如果不用触发器 怎么实这个的现定时呢[/quote] 不用触发器,要实现定时 ,那就通过存储过程,然后建个定时作业,在定时作业中调用存储过程,就可以了哈[/quote]这个怎么写 没有用过
LongRui888 2013-10-30
  • 打赏
  • 举报
回复
引用 18 楼 a276620535 的回复:
[quote=引用 17 楼 yupeigu 的回复:] 你看看,是这样吗:

drop table a
drop table b
drop table c
go


create table b
(bid int identity(1,1) primary key,bdate datetime)


create table a
(aid int identity(1,1) primary key,
bid int foreign key references b(bid),
sta int,
adate datetime
)

create table c
(cid int identity(1,1) primary key,cdate datetime)

insert into b
select '2013-10-30 10:10:10' union all
select '2013-10-30 21:10:10'



insert into a
values(1,1,getdate())


create trigger dbo.trigger_a_insert
on dbo.a
for insert
as
   if exists(select 1 from inserted i
             inner join dbo.b 
                     on i.bid = b.bid
             where i.sta = 1 and b.bdate < getdate())
      
      insert into c values(getdate())   
go

--1.触发,c表插入1条记录
insert into a(bid,sta,adate) values(1,1,getdate())

select * from c
/*
cid	cdate
1	2013-10-30 16:02:20.943
*/

--2.不触发,c表不会插入记录,还是1条
insert into a(bid,sta,adate) values(1,0,getdate())

select * from c
/*
cid	cdate
1	2013-10-30 16:02:20.943
*/

--2.不触发,由于bid=2时,bdate大于当前时间,所以不插入,还是1条
insert into a(bid,sta,adate) values(2,1,getdate())

select * from c
/*
cid	cdate
1	2013-10-30 16:02:20.943
*/
多谢 大概就是这样了 再问下如果不用触发器 怎么实这个的现定时呢[/quote] 不用触发器,要实现定时 ,那就通过存储过程,然后建个定时作业,在定时作业中调用存储过程,就可以了哈
a276620535 2013-10-30
  • 打赏
  • 举报
回复
引用 17 楼 yupeigu 的回复:
你看看,是这样吗:

drop table a
drop table b
drop table c
go


create table b
(bid int identity(1,1) primary key,bdate datetime)


create table a
(aid int identity(1,1) primary key,
bid int foreign key references b(bid),
sta int,
adate datetime
)

create table c
(cid int identity(1,1) primary key,cdate datetime)

insert into b
select '2013-10-30 10:10:10' union all
select '2013-10-30 21:10:10'



insert into a
values(1,1,getdate())


create trigger dbo.trigger_a_insert
on dbo.a
for insert
as
   if exists(select 1 from inserted i
             inner join dbo.b 
                     on i.bid = b.bid
             where i.sta = 1 and b.bdate < getdate())
      
      insert into c values(getdate())   
go

--1.触发,c表插入1条记录
insert into a(bid,sta,adate) values(1,1,getdate())

select * from c
/*
cid	cdate
1	2013-10-30 16:02:20.943
*/

--2.不触发,c表不会插入记录,还是1条
insert into a(bid,sta,adate) values(1,0,getdate())

select * from c
/*
cid	cdate
1	2013-10-30 16:02:20.943
*/

--2.不触发,由于bid=2时,bdate大于当前时间,所以不插入,还是1条
insert into a(bid,sta,adate) values(2,1,getdate())

select * from c
/*
cid	cdate
1	2013-10-30 16:02:20.943
*/
多谢 大概就是这样了 再问下如果不用触发器 怎么实这个的现定时呢
LongRui888 2013-10-30
  • 打赏
  • 举报
回复
你看看,是这样吗:

drop table a
drop table b
drop table c
go


create table b
(bid int identity(1,1) primary key,bdate datetime)


create table a
(aid int identity(1,1) primary key,
bid int foreign key references b(bid),
sta int,
adate datetime
)

create table c
(cid int identity(1,1) primary key,cdate datetime)

insert into b
select '2013-10-30 10:10:10' union all
select '2013-10-30 21:10:10'



insert into a
values(1,1,getdate())


create trigger dbo.trigger_a_insert
on dbo.a
for insert
as
   if exists(select 1 from inserted i
             inner join dbo.b 
                     on i.bid = b.bid
             where i.sta = 1 and b.bdate < getdate())
      
      insert into c values(getdate())   
go

--1.触发,c表插入1条记录
insert into a(bid,sta,adate) values(1,1,getdate())

select * from c
/*
cid	cdate
1	2013-10-30 16:02:20.943
*/

--2.不触发,c表不会插入记录,还是1条
insert into a(bid,sta,adate) values(1,0,getdate())

select * from c
/*
cid	cdate
1	2013-10-30 16:02:20.943
*/

--2.不触发,由于bid=2时,bdate大于当前时间,所以不插入,还是1条
insert into a(bid,sta,adate) values(2,1,getdate())

select * from c
/*
cid	cdate
1	2013-10-30 16:02:20.943
*/
a276620535 2013-10-30
  • 打赏
  • 举报
回复
引用 15 楼 yupeigu 的回复:
引用 14 楼 a276620535 的回复:
[quote=引用 13 楼 yupeigu 的回复:] [quote=引用 12 楼 a276620535 的回复:] [quote=引用 11 楼 yupeigu 的回复:] [quote=引用 10 楼 a276620535 的回复:] [quote=引用 9 楼 yupeigu 的回复:] [quote=引用 8 楼 a276620535 的回复:] [quote=引用 6 楼 rockyljt 的回复:] 贴出来表结构出来才好写了
往c里面添加的记录,cid是bid吗,还有cdate字段是什么值 [/quote]随便往c表添加数据 带data的都是时间 [/quote] 哦,那这个cdate字段是存储当前系统给的时间吗,[/quote]嗯 求代码[/quote] 那插入到c表的字段cid是自增列吗[/quote]cid相当于流水号[/quote] 你是想当向表a插入记录的时候,触发,然后如果符合条件,就插入到表c吗[/quote]是的
LongRui888 2013-10-30
  • 打赏
  • 举报
回复
引用 14 楼 a276620535 的回复:
引用 13 楼 yupeigu 的回复:
[quote=引用 12 楼 a276620535 的回复:] [quote=引用 11 楼 yupeigu 的回复:] [quote=引用 10 楼 a276620535 的回复:] [quote=引用 9 楼 yupeigu 的回复:] [quote=引用 8 楼 a276620535 的回复:] [quote=引用 6 楼 rockyljt 的回复:] 贴出来表结构出来才好写了
往c里面添加的记录,cid是bid吗,还有cdate字段是什么值 [/quote]随便往c表添加数据 带data的都是时间 [/quote] 哦,那这个cdate字段是存储当前系统给的时间吗,[/quote]嗯 求代码[/quote] 那插入到c表的字段cid是自增列吗[/quote]cid相当于流水号[/quote] 你是想当向表a插入记录的时候,触发,然后如果符合条件,就插入到表c吗
a276620535 2013-10-30
  • 打赏
  • 举报
回复
引用 13 楼 yupeigu 的回复:
引用 12 楼 a276620535 的回复:
[quote=引用 11 楼 yupeigu 的回复:] [quote=引用 10 楼 a276620535 的回复:] [quote=引用 9 楼 yupeigu 的回复:] [quote=引用 8 楼 a276620535 的回复:] [quote=引用 6 楼 rockyljt 的回复:] 贴出来表结构出来才好写了
往c里面添加的记录,cid是bid吗,还有cdate字段是什么值 [/quote]随便往c表添加数据 带data的都是时间 [/quote] 哦,那这个cdate字段是存储当前系统给的时间吗,[/quote]嗯 求代码[/quote] 那插入到c表的字段cid是自增列吗[/quote]cid相当于流水号
LongRui888 2013-10-30
  • 打赏
  • 举报
回复
引用 12 楼 a276620535 的回复:
引用 11 楼 yupeigu 的回复:
[quote=引用 10 楼 a276620535 的回复:] [quote=引用 9 楼 yupeigu 的回复:] [quote=引用 8 楼 a276620535 的回复:] [quote=引用 6 楼 rockyljt 的回复:] 贴出来表结构出来才好写了
往c里面添加的记录,cid是bid吗,还有cdate字段是什么值 [/quote]随便往c表添加数据 带data的都是时间 [/quote] 哦,那这个cdate字段是存储当前系统给的时间吗,[/quote]嗯 求代码[/quote] 那插入到c表的字段cid是自增列吗
a276620535 2013-10-30
  • 打赏
  • 举报
回复
引用 11 楼 yupeigu 的回复:
引用 10 楼 a276620535 的回复:
[quote=引用 9 楼 yupeigu 的回复:] [quote=引用 8 楼 a276620535 的回复:] [quote=引用 6 楼 rockyljt 的回复:] 贴出来表结构出来才好写了
往c里面添加的记录,cid是bid吗,还有cdate字段是什么值 [/quote]随便往c表添加数据 带data的都是时间 [/quote] 哦,那这个cdate字段是存储当前系统给的时间吗,[/quote]嗯 求代码
LongRui888 2013-10-30
  • 打赏
  • 举报
回复
引用 10 楼 a276620535 的回复:
引用 9 楼 yupeigu 的回复:
[quote=引用 8 楼 a276620535 的回复:] [quote=引用 6 楼 rockyljt 的回复:] 贴出来表结构出来才好写了
往c里面添加的记录,cid是bid吗,还有cdate字段是什么值 [/quote]随便往c表添加数据 带data的都是时间 [/quote] 哦,那这个cdate字段是存储当前系统给的时间吗,
a276620535 2013-10-30
  • 打赏
  • 举报
回复
引用 9 楼 yupeigu 的回复:
引用 8 楼 a276620535 的回复:
[quote=引用 6 楼 rockyljt 的回复:]
贴出来表结构出来才好写了


往c里面添加的记录,cid是bid吗,还有cdate字段是什么值
[/quote]随便往c表添加数据 带data的都是时间
LongRui888 2013-10-30
  • 打赏
  • 举报
回复
引用 8 楼 a276620535 的回复:
引用 6 楼 rockyljt 的回复:
贴出来表结构出来才好写了
往c里面添加的记录,cid是bid吗,还有cdate字段是什么值
a276620535 2013-10-30
  • 打赏
  • 举报
回复
引用 6 楼 rockyljt 的回复:
贴出来表结构出来才好写了
a276620535 2013-10-30
  • 打赏
  • 举报
回复
引用 6 楼 rockyljt 的回复:
贴出来表结构出来才好写了
我刚刚建的几个测试表
如果我想插入一条记录 ,表A有一个bid是B表的外键,A表的sta 状态是1的时候
表B里有bdate的时间的字段,当系统时间大于B表的bdate时候并且A表的sta状态是1
就自动往C表里添加一条数据
---涛声依旧--- 2013-10-30
  • 打赏
  • 举报
回复
贴出来表结构出来才好写了
加载更多回复(5)

27,580

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧