㈠ 如何查找数据库中某一字段值
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