遍历数据库,删除包含指定列的表的行数据-
IF OBJECT_ID ('dbo.spDeleteRowDate_In_DB') IS NOT NULL DROP PROCEDURE dbo.spDeleteRowDate_In_DB go create proc spDeleteRowDate_In_DB ( @compid int --公司ID ) AS DECLARE @tableName varchar(50) DECLARE My_Cursor CURSOR --定义游标 FOR (select OBJECT_NAME(id) from syscolumns where id in( select id from sysobjects where type='U') and lower(name)='compid') --查出需要的集合放到游标中 OPEN My_Cursor; --打开游标 FETCH NEXT FROM My_Cursor INTO @tableName; --读取第一行数据(将表名放到@tableName变量中) WHILE @@FETCH_STATUS = 0 BEGIN declare @sql nvarchar(1000) set @sql = ' delete from '+@tableName+' where compid= '+Convert(varchar(50),@compid ) print @sql exec (@sql) FETCH NEXT FROM My_Cursor INTO @tableName; --读取下一行数据 END CLOSE My_Cursor; --关闭游标 DEALLOCATE My_Cursor; --释放游标 GO --exec spDeleteRowDate_In_DB 1
所有问题都会有一定程度的抽象和假设