这个存储过程查询数据库中的所有的表是否包含要查询的字符串
2006-03-20 23:27
393 查看
--下面这个存储过程用来在某个数据库中的所有表中查找某一字符串
----使用: 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
相关文章推荐
- 这个存储过程查询数据库中的所有的表是否包含要查询的字符串
- 查询数据库中一个字段的值(该值为“,”分隔的字符串)是否包含一个字符串
- 判断在一个矩阵中是否存在一条包含某字符串所有字符的路径。路径可以从矩阵中的任意一个格子开始,每一步可以在矩阵中向左,向右,向上,向下移动一个格子。如果一条路径经过了矩阵中的某一个格子,则该路径不能再进
- asp.net 中查询字符串中是否包含指定字符/串
- 比较两个字符串A和B,确定A中是否包含B中所有的字符。
- java在指定路径下查找所有文件及其子文件名中,是否包含指定查询的关键字
- (非常小的技巧)利用范型List存储数据并通过Contains查询字符串是否包含在范型中
- 查询数据库存储过程中包含某字符串的所有存储过程名称
- C#中查询字符串中是否包含指定字符/串,使用IndexOf还是Contains?
- linux查看目录下所有文件内容中是否包含某个字符串
- 用模糊查询like语句时如果要查是否包含%字符串该如何写
- sql在所有存储过程中查询包含某字符串的执行语句
- 请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径。路径可以从矩阵中的任意一个格子开始,每一步可以在矩阵中向左,向右,向上,向下移动一个格子。
- List的All方法使用问题:我用List1的All方法来确保List1的所有每个数组元素中是否包含Arr1中的任意元素,这个部分很难实现。 因为,没有一个数组中是否包含另一个数组中的任意元素这个功能
- 查询postgresql数据库所有表中的所有字段包含一个字符串
- 查找目录下所有文件中是否包含某个字符串
- 快速查找所有存储过程中是否包含某个字符串
- linux利用C库查询配置文件是否包含特定字符串
- 算法4— 判断一个字符串里是否包含另一个字符串所有的字符
- 判断一个字符串里是否包含另一个字符串所有的字符