① 如何通過關鍵字查詢資料庫中所有表
1、sql Server語法如下(如查詢包含spt的表)
selectnamefromsys.objectswheretype='U'andnamelike'%spt%';
2、Oracle語法如下(如查詢包含test的表)
selecttable_namefromuser_tableswheretable_namelike'%TEST%';
注意:Oracle中,表名處的字母一定要大寫
② sql 查詢所有欄位只要包含某關鍵字的記錄
OR 的連接必須加括弧,要不然數據是錯誤的
③ sql如何查詢整個資料庫包含abc關鍵字的數據
先把資料庫中所擾尺有表名稱找知察到 ,然後緩猛高逐個表查詢
declare @name varchar(50),@tt varchar(10)
set @tt = '%abc%'
declare cc cursor for
select name from sysobjects where xtype='U'
open cc
fetch cc into @name
while @@fetch_status = 0
begin
exec('selec *from '+@name +' where abc like '+@tt)
fetch cc into @name
end
④ sql 資料庫中所有表中查詢關鍵字
用游標循環以下,可能效率有點低,以下是我寫的一個存儲過程語句供參考
--exec searchkeyword '物理'
Create proc searchkeyword(@keyword varchar(100))
as
begin
declare @tablename varchar(100),@colname varchar(100),@sql nvarchar(2000),@cou int
create table #t
(
tablename varchar(100),
colname varchar(100)
)
declare c1 cursor for
select a.name as TableName,b.name as ColName From sysobjects a
inner join syscolumns b on (a.id=b.id)
where a.xtype='U'
open c1
fetch next from c1 into @tablename,@colname
WHILE @@FETCH_STATUS = 0
begin
set @sql='select @count=count(*) from '+@tablename+' where '+@colname+'=@keyword'
begin try
exec sp_executesql @sql, N'@count int out,@keyword varchar(20)', @cou out ,@keyword
end try
begin catch
set @cou=0
end catch
if @cou>0
begin
insert into #t values(@tablename,@colname)
end
fetch next from c1 into @tablename,@colname
end
CLOSE c1
DEALLOCATE c1
select * from #t
end
⑤ 怎樣用SQL語句查詢一個資料庫中的所有表
1、打開Microsoft SQL Server 2012,選中需要查詢所有表的資料庫。
⑥ SQL SERVER 2008如何查詢所有表中的一個關鍵詞
查詢一個資料庫中所有的數據中是否包含指定字元串
declare @str varchar(100)
set @str='動軟' --要搜索的字元串
declare @s varchar(8000)
declare tb cursor local for
select s='if exists(select 1 from ['+b.name+'] where convert(varchar,['+a.name+']) like ''%'+@str+'%'')
print ''select top 5 ['+a.name+'],* from ['+b.name+']'''
from syscolumns a join sysobjects b on a.id=b.id
where b.xtype='U' and a.status>=0--所查列的欄位類型
and a.xusertype in(175,239,231,167,56,60,108,106)
open tb
fetch next from tb into @s
while @@fetch_status=0
begin
exec(@s)
fetch next from tb into @s
end
close tb
deallocate tb