码迷,mamicode.com
首页 > 其他好文 > 详细

存储过程写法

时间:2018-08-08 13:42:46      阅读:117      评论:0      收藏:0      [点我收藏+]

标签:begin   rtti   cat   oca   ted   dea   fetch   open   diff   

--创建存储过程执行删除操作
ALTER PROCEDURE [dbo].[ad_preempted_timer]
AS
DECLARE @pid VARCHAR(32)
DECLARE @times date
DECLARE @nowtime date
DECLARE @difftime INT

--定义一个游标
declare preempted_timer cursor for SELECT pid,inserttime from ad_preempted
--打开游标
open preempted_timer
--开始遍历,将下一行的数据存入两个变量中
fetch next from preempted_timer INTO @pid,@times
while @@fetch_status=0 --如果下一行还有数据
begin
--读取游标
set @nowtime=GETDATE()
SET @difftime=DATEDIFF("hh",@times,@nowtime)
if(@difftime>72)
BEGIN
DELETE from ad_preempted where pid=@pid
END
fetch next from preempted_timer INTO @pid,@times

end
close preempted_timer
--摧毁游标
deallocate preempted_timer

存储过程写法

标签:begin   rtti   cat   oca   ted   dea   fetch   open   diff   

原文地址:https://www.cnblogs.com/94LH-shuai/p/9441816.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!