USE [test] GO /****** Object: StoredProcedure [dbo].[wangchuang] Script Date: 2016/8/25 14:09:24 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO -- ============================================= -- Author: <Author,,Name> -- Create date: <Create Date,,> -- Description: <Description,,> -- ============================================= ALTER PROCEDURE [dbo].[wangchuang] AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON; declare @strsql varchar(255) declare kindcollection cursor --声明游标 for select kind from kind open kindcollection --打开 declare @noToUpdate varchar(50) --声明一个变量,用于读取游标中的值 fetch next from kindcollection into @noToUpdate while @@fetch_status=0 --循环读取 begin -- print @noToUpdate -- print GETDATE() set @strsql = 'delete from test.dbo.abc1_'+@noToUpdate+' where endtime< GETDATE()'; print @strsql execute(@strsql) fetch next from kindcollection into @noToUpdate end close kindcollection --关闭 deallocate kindcollection --删除 END