SQLServer存储过程--在数据库中所有表中查找某一字符串
2016-01-14 14:57
501 查看
--下面的存储过程用于在某个数据库中的所有表中查找某一字符串 ----使用: EXEC Search '阿三' CREATE PROCEDURE Search @Str varchar(100),@SearchFlag int=0,@TableFlag int=1 /** **@Str 要搜索的字符串 **@TableFlag 1: 只在用户表中查找;2:只在系统表中查找;其他:在所有表中查找 **@SearchFlag 1: 精确查询;其他:模糊查询 **/ As begin CREATE table #TableList(tablename sysname,colname sysname) declare @table sysname declare @col sysname set nocount on if @TableFlag=1 declare curTab scroll cursor for select name from sysobjects where xtype='U' and status>0 else if @TableFlag=2 declare curTab scroll cursor for select name from sysobjects where xtype='S' else declare curTab scroll cursor for select name from sysobjects where xtype='S' or xtype='U' open curTab fetch next from curTab into @table while @@FETCH_STATUS=0 begin declare curCol scroll cursor for select name from syscolumns where (xtype=175 or xtype=167 or xtype=239 or xtype=231) and (id in (select id from sysobjects where name=@table)) open curCol fetch next from curCol into @col while @@FETCH_STATUS=0 begin if @SearchFlag=1 execute('insert into #TableList select '''+@table+''','''+@col+''' from '+@table+' where '+@col+'='''+@str+'''') else execute('insert into #TableList select '''+@table+''','''+@col+''' from '+@table+' where '+@col+' like '''+ '%'+@str+ '%'+'''') fetch next from curCol into @col end close curCol deallocate curCol fetch next from curTab into @table end close curTab deallocate curTab set nocount off select distinct * from #TableList drop table #tablelist end GO
相关文章推荐
- 下面这个存储过程用来在某个数据库中的所有表中查找某一字符串
- 存储过程用来在某个数据库中的所有表中查找某一字符串
- 查找 数据库中包含某一字段的所有表的表名的mysql语句
- 在一个数据库中的所有表里查找一个字符串
- sql server 2012 数据库所有表里查找某字符串的方法
- 查找整个数据库里面包含某一字段的所有表
- 在当前数据库的所有表,所有字段查找字符串
- 如何在某一数据库的所有表的所有列上搜索一个字符串?
- 分享:sp_get_object_definition查找所有数据库下面哪些对象定义语句中包括想要查找的字符串
- sql server 2012 数据库所有表里查找某字符串的方法
- 数据库:读取指定表的所有列名&读取指定表某一列的所有数据
- js字符串替换所有的某一字符
- 【数据库SQL实践】查找最晚入职员工的所有信息
- 查找含有某字符串的所有文件(grep)
- 查找所有有某一列的表
- Linux查找含有某字符串的所有文件
- Linux查找含有某字符串的所有文件
- 查找目录下的所有文件中是否含有某个字符串 linux
- linux下查找某目录下所有文件包含某字符串的命令
- Linux查找含有某字符串的所有文件