create table #myTable ( tablename varchar(50), tableNumname varchar(100) ) declare @delStr nvarchar(500) set @delStr='申通E物流' /*************这里是关键词*******************/ /****************************************/ /**********以下为操作实体************/ set nocount on declare @tableName nvarchar(100),@columnName nvarchar(100),@tbID int,@iRow int,@iResult int declare @sql nvarchar(500) set @iResult=0 declare cur cursor for select name,id from sysobjects where xtype='U'--查询出所有用户创建的表 --打开游标 open cur fetch next from cur into @tableName,@tbID --同时遍历这些表 while @@fetch_status=0 begin declare cur1 cursor for --xtype in (231,167,239,175) 为char,varchar,nchar,nvarchar类型 select name from syscolumns where xtype in (231,167,239,175) and id=@tbID --查询表内数据为这几个类型的列名 open cur1 --打开游标 fetch next from cur1 into @columnName --遍历查询结果 while @@fetch_status=0 begin --set @sql=' select '+@tableName+' from '+@tableName+' where '+@columnName+' like ''%'''+@delStr+'''%'' /* set @sql='update [' + @tableName + '] set ['+ @columnName +']= replace(['+@columnName+'],'''+@delStr+''','''') where ['+@columnName+'] like ''%'+@delStr+'%'''*/ --set @sql='select '''+@tableName+''' from ['+@tableName+'] where ['+@columnName+'] like ''%'+@delStr+'%''' set @sql='insert into #myTable select '''+@tableName+''','''+@columnName+''' from ['+@tableName+'] where ['+@columnName+'] like ''%'+@delStr+'%''' --select @sql exec sp_executesql @sql --查询出里面含有这个数据的表明 set @iRow=@@rowcount-- set @iResult=@iResult+@iRow if @iRow>0 begin--输出相关的数据 print 'Table: '+@tableName+', Column:'+@columnName+' has '+convert(varchar(10),@iRow)+' record(s);' end fetch next from cur1 into @columnName--把下一个@columnName 放到游标中 end close cur1--关闭游标 deallocate cur1--关闭游标 fetch next from cur into @tableName,@tbID--把下一个@tableName,@tbID放到游标中 end print 'The database has '+convert(varchar(10),@iResult)+' record(s) .' close cur deallocate cur set nocount off select * from #myTable drop table #myTable
查询某个数据库含有某个字符的存储过程
最新推荐文章于 2022-07-19 14:18:10 发布