--这样试一下! --具体步骤: 企业管理器 --管理 --SQL Server代理 --右键作业 --新建作业 --"常规"项中输入作业名称 --"步骤"项 --新建 --"步骤名"中输入步骤名 --"类型"中选择"Transact-SQL 脚本(TSQL)" --"数据库"选择执行命令的数据库 --"命令"中输入要执行的语句: Declare @i Int Set @i=1 While @i<=10 Begin Exec (' Delete Table'+Rtrim(@I) +' Where DateDiff(Hour,时间字段,Getdate())=1') Set @i=@i+1 End
To liangpei2008(逍遥叹)那如果不存在Table10这样的表,会出错吗?有没有办法先从另一个别中读ID号了再执行呢?谢谢两位
把While循环部分改成Fetch_Next Into...的游标循环格式
TO LouisXIV(夜游神) Fetch_Next Into...这个语法怎么用?可否给个示例啊?谢谢
我改成这样怎么保存的时候总是无响应,而检查语法的时候又通过了CREATE PROCEDURE [dbo].[sp_DeleteInfo] ASdelete from tb_Online Where Datediff(n,fdLastActive,getDate())>20; GOdeclare @intRoomID int declare RoomList_cursor cursor for select fdRoomID From tb_RoomInfo open RoomList_cursor fetch next from RoomList_cursor into @intRoomID while @@fetch_status=0 begin print @intRoomID end close RoomList_cursor deallocate RoomList_cursor
declare @intRoomID int declare RoomList_cursor cursor for select fdRoomID From tb_RoomInfo open RoomList_cursor fetch next from RoomList_cursor into @intRoomID while @@fetch_status=0 begin print @intRoomID FETCH NEXT FROM RoomList_cursor INTO @intRoomID --缺少一句,指针没有向下走,进入死循环了 end close RoomList_cursor deallocate RoomList_cursor
--具体步骤:
企业管理器
--管理
--SQL Server代理
--右键作业
--新建作业
--"常规"项中输入作业名称
--"步骤"项
--新建
--"步骤名"中输入步骤名
--"类型"中选择"Transact-SQL 脚本(TSQL)"
--"数据库"选择执行命令的数据库
--"命令"中输入要执行的语句:
Declare @i Int
Set @i=1
While @i<=10
Begin
Exec ('
Delete Table'+Rtrim(@I) +'
Where DateDiff(Hour,时间字段,Getdate())=1')
Set @i=@i+1
End
--确定
--"调度"项
--新建调度
--"名称"中输入调度名称
--"调度类型"中选择你的作业执行安排
--如果选择"反复出现"
--点"更改"来设置你的时间安排
然后将SQL Agent服务启动,并设置为自动启动,否则你的作业不会被执行设置方法:
我的电脑--控制面板--管理工具--服务--右键 SQLSERVERAGENT--属性--启动类型--选择"自动启动"--确定.
GOdeclare @intRoomID int
declare RoomList_cursor cursor for
select fdRoomID From tb_RoomInfo
open RoomList_cursor
fetch next from RoomList_cursor into @intRoomID
while @@fetch_status=0
begin
print @intRoomID
end
close RoomList_cursor
deallocate RoomList_cursor
declare RoomList_cursor cursor for
select fdRoomID From tb_RoomInfo
open RoomList_cursor
fetch next from RoomList_cursor into @intRoomID
while @@fetch_status=0
begin
print @intRoomID
FETCH NEXT FROM RoomList_cursor INTO @intRoomID --缺少一句,指针没有向下走,进入死循环了
end
close RoomList_cursor
deallocate RoomList_cursor