1、首先在电脑中打开Microsoft SQL Server,查询所有数据库。
‘贰’ SQLsever中通过遍历的方式查找当前库中所有表里面的某个值
下面将为您介绍sql遍历所有表中某项值为已知数的查询语句写法,供您参考,如果您对sql遍历方面感兴趣的话,不妨一看,希望对您有所帮助。
CREATE proc Full_Search(@string varchar(50)) as begin declare @tbname varchar(50) declare tbroy cursor for select name from sysobjects where xtype= 'u ' --第一个游标遍历所有的表 open tbroy fetch next from tbroy into @tbname while @@fetch_status=0 begin declare @colname varchar(50) declare colroy cursor for select name from syscolumns where id=object_id(@tbname) and xtype in ( select xtype from systypes where name in ( 'varchar ', 'nvarchar ', 'char ', 'nchar ') --数据类型为字符型的字段 ) --第二个游标是第一个游标的嵌套游标,遍历某个表的所有字段 open colroy fetch next from colroy into @colname while @@fetch_status=0 begin declare @sql nvarchar(1000),@j int select @sql= 'select @i=count(1) from ' +@tbname + ' where '+ @colname+ ' like '+ '''%'+@string+ '%''' exec sp_executesql @sql,N'@i int output',@i=@j output --输出满足条件表的记录数 if @j> 0 BEGIN select 包含字串的表名=@tbname --exec( 'select distinct '+@colname+' from ' +@tbname + ' where '+ @colname+ ' like '+ '''%'+@string+ '%''') END fetch next from colroy into @colname end close colroy deallocate colroy fetch next from tbroy into @tbname end close tbroy deallocate tbroy end go exec Full_Search '123'
以上就是sql遍历所有表中某项值为已知数的查询方法。
‘叁’ SQL server 查询数据库中所有包含某值的表
use test
go
select a.Name as tableName from sysobjects a inner join syscolumns b on a.ID=b.ID
where b.Name='列名'
‘肆’ SQL 查询所有表中的某个值
select * from 表名 where 字段=值
* 表示该表中所有的字段,如果不想要所有的字段,可以将要显示的字段代替 * ,多个字段可以用逗号分隔;
表名 表示要查询表的表名称
字段 就是 你想要查询该表中字段的名称
例如:select ID,name ,age from student(表名) where age>16