㈠ 如何查找資料庫中某一欄位值
1.查詢資料庫中的所有資料庫名:
SELECT Name FROM Master..SysDatabases ORDER BY Name
2.查詢某個資料庫中所有的表名:
SELECT Name FROM SysObjects Where XType='U' ORDER BY Name
3.查詢表結構信息:
1 SELECT (case when a.colorder=1 then d.name else null end) 表名,
2 a.colorder 欄位序號,a.name 欄位名,
3 (case when COLUMNPROPERTY( a.id,a.name,'IsIdentity')=1 then '√'else '' end) 標識,
4 (case when (SELECT count(*) FROM sysobjects
5 WHERE (name in (SELECT name FROM sysindexes
6 WHERE (id = a.id) AND (indid in
7 (SELECT indid FROM sysindexkeys
8 WHERE (id = a.id) AND (colid in
9 (SELECT colid FROM syscolumns WHERE (id = a.id) AND (name = a.name)))))))
10 AND (xtype = 'PK'))>0 then '√' else '' end) 主鍵,b.name 類型,a.length 佔用位元組數,
11 COLUMNPROPERTY(a.id,a.name,'PRECISION') as 長度,
12 isnull(COLUMNPROPERTY(a.id,a.name,'Scale'),0) as 小數位數,(case when a.isnullable=1 then '√'else '' end) 允許空,
13 isnull(e.text,'') 默認值,isnull(g.[value], ' ') AS [說明]
14 FROM syscolumns a
15 left join systypes b on a.xtype=b.xusertype
16 inner join sysobjects d on a.id=d.id and d.xtype='U' and d.name<>'dtproperties'
17 left join syscomments e on a.cdefault=e.id
18 left join sys.extended_properties g on a.id=g.major_id AND a.colid=g.minor_id
19 left join sys.extended_properties f on d.id=f.class and f.minor_id=0
20 where b.name is not null
21 --WHERE d.name='要查詢的表' --如果只查詢指定表,加上此條件
22 order by a.id,a.colorder
㈡ 如何使用sql server查詢整個資料庫某一個關鍵字
感覺你最好先搞個查詢,別直接替換,風險太大。
代碼如下(這個是查找庫里所有記錄中含「張三」的數據,替換里邊的張三那個字元串就行):
declare@cloumnsvarchar(40)
declare@tablenamevarchar(40)
declare@strvarchar(40)
declare@countsint
declare@sqlnvarchar(2000)
declareMyCursorCursorFor
Selecta.nameasColumns,b.,sysobjectsb,systypesc
wherea.id=b.id
andb.type='U'
anda.xtype=c.xtype
andc.namelike'%char%'
set@str='張三'
OpenMyCursor
FetchnextFromMyCursorInto@cloumns,@tablename
While(@@Fetch_Status=0)
Begin
set@sql='select@tmp_counts=count(*)from'+@tablename+'where'+@cloumns+'='''+@str+''''
executesp_executesql@sql,N'@tmp_countsintout',@countsout
if@counts>0
begin
print'表名為:'+@tablename+',欄位名為'+@cloumns
end
FetchnextFromMyCursorInto@cloumns,@tablename
End
CloseMyCursor
DeallocateMyCursor
㈢ mysql資料庫如何搜索關鍵詞
辦法如下:
select * from table1 where concat(`欄位`,`欄位`,`欄位` ) like '%關鍵字%' union
select * from table2 where concat(`欄位`,`欄位`,`欄位` ) like '%關鍵字%' union
select * from table3 where concat(`欄位`,`欄位`,`欄位` ) like '%關鍵字%' union
select * from table4 where concat(`欄位`,`欄位`,`欄位` ) like '%關鍵字%' union
select * from table5 where concat(`欄位`,`欄位`,`欄位` ) like '%關鍵字%'
前提是查詢出來的欄位個數要一樣,類型要對應好,至於如何得到正確且符合你需要的sql就要在程序類中處理了。
㈣ 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 @@_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
㈤ 如何查詢輸出資料庫中包含某欄位關鍵詞的全部條目
1.查詢資料庫中的所有資料庫名:
SELECT Name FROM Master..SysDatabases ORDER BY Name
2.查詢某個資料庫中所有的表名:
SELECT Name FROM SysObjects Where XType='U' ORDER BY Name
3.查詢表結構信息:
1 SELECT (case when a.colorder=1 then d.name else null end) 表名,
2 a.colorder 欄位序號,a.name 欄位名,
3 (case when COLUMNPROPERTY( a.id,a.name,'IsIdentity')=1 then '√'else '' end) 標識,
4 (case when (SELECT count(*) FROM sysobjects
5 WHERE (name in (SELECT name FROM sysindexes
6 WHERE (id = a.id) AND (indid in
7 (SELECT indid FROM sysindexkeys
8 WHERE (id = a.id) AND (colid in
9 (SELECT colid FROM syscolumns WHERE (id = a.id) AND (name = a.name)))))))
10 AND (xtype = 'PK'))>0 then '√' else '' end) 主鍵,b.name 類型,a.length 佔用位元組數,
11 COLUMNPROPERTY(a.id,a.name,'PRECISION') as 長度,
12 isnull(COLUMNPROPERTY(a.id,a.name,'Scale'),0) as 小數位數,(case when a.isnullable=1 then '√'else '' end) 允許空,
13 isnull(e.text,'') 默認值,isnull(g.[value], ' ') AS [說明]
14 FROM syscolumns a
15 left join systypes b on a.xtype=b.xusertype
16 inner join sysobjects d on a.id=d.id and d.xtype='U' and d.name<>'dtproperties'
17 left join syscomments e on a.cdefault=e.id
18 left join sys.extended_properties g on a.id=g.major_id AND a.colid=g.minor_id
19 left join sys.extended_properties f on d.id=f.class and f.minor_id=0
20 where b.name is not null
21 --WHERE d.name='要查詢的表' --如果只查詢指定表,加上此條件
22 order by a.id,a.colorder