这个存储过程查询数据库中的所有的表是否包含要查询的字符串
2004-08-20 16:55
211 查看
--下面这个存储过程用来在某个数据库中的所有表中查找某一字符串
----使用: EXEC Search '张三'
CREATE PROCEDURE Search @Str varchar(100),@SearchFlag int=1,@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
----使用: EXEC Search '张三'
CREATE PROCEDURE Search @Str varchar(100),@SearchFlag int=1,@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
相关文章推荐
- 这个存储过程查询数据库中的所有的表是否包含要查询的字符串
- js查询字符串是否包含指定字符的函数indexOf
- sql在所有存储过程中查询包含某字符串的执行语句
- C#中查询字符串中是否包含指定字符/串,使用IndexOf还是Contains?
- linux查看目录下所有文件内容中是否包含某个字符串
- 请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径。路径可以从矩阵中的任意一个格子开始,每一步可以在矩阵中向左,向右,向上,向下移动一个格子。
- linux查看目录下所有文件内容中是否包含某个字符串
- Replace关键字的妙用查询是否包含某个特定字符串
- java在指定路径下查找所有文件及其子文件名中,是否包含指定查询的关键字
- 用模糊查询like语句时如果要查是否包含%字符串该如何写
- 快速查找所有存储过程中是否包含某个字符串
- C#中查询字符串中是否包含指定字符/串,使用IndexOf还是Contains?
- linux利用C库查询配置文件是否包含特定字符串
- 判断一个字符串里是否包含另一个字符串所有的字符
- 哈希表:一个字符串是否包含另一个字符串的所有字符
- 判断一个字符串里是否包含另一个字符串所有的字符
- ACM273 给你一个乱序的字符串,里面包含有小写字母(a--z)以及一些特殊符号,请你找出所给字符串里面所有的小写字母的个数, 拿这个数对26取余,输出取余后的数字在子母表中对应的小写字母(0对应z
- 请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径。路径可以从矩阵中的任意一个格子开始,每一步可以在矩阵中向左,向右,向上,向下移动一个格子。如果一条路径经过了矩阵中的某一个格子,则该路径不能再进入该格子。 例如 a b c e s f c s a d e e 矩阵中包含一条字符串"bccced"的路径,但是矩阵中不包含"abcb"路径,因为字符串的第一个字符b占据了矩阵中
- 比较两个字符串A和B,确定A中是否包含B中所有的字符。
- 算法4— 判断一个字符串里是否包含另一个字符串所有的字符