经典SQL
获取数据库中包含指定列的 表
select object_name(id) ,Name as colName
from syscolumns
where (name ='列名')
and id in(select id from sysobjects where xtype='u')
order by object_name(id)
sql - 查询所有表中包含指定值
可以直接创建sql语句:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
CREATE TABLE qResults (tName nvarchar(370), cname nvarchar(3630),[ count ] int ) declare @tname nvarchar(200) declare @cname nvarchar(200) declare @countOut nvarchar(200) declare @sql nvarchar( max ) declare c_search cursor for select t. name ,c. name from sysobjects t inner join syscolumns c on t.id=c.id where t.type= 'u' and c.xtype in (56,167,175,231,239) order by t. name --xtype 这个地方限制类型为int varchar char nvarchar nchar 五种类型 open c_search fetch next from c_search into @tname,@cname while @@FETCH_STATUS=0 begin set @sql=N 'select @countx=COUNT(*) from ' +@tname + ' where [' +@cname + '] =' '44' ' ' print @sql EXECUTE sp_executesql @sql,N '@countx nvarchar(200) out ' , @countx=@countOUT OUT insert into qResults values (@tname,@cname,@countOUT) fetch next from c_search into @tname,@cname end select tName 'Table' ,cname 'Field' from qResults where [ count ]>0 order by tname close c_search deallocate c_search drop table qResults |
或者先创建存储过程:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
ALTER 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 |
然后execute,输入指定值即可.
--快速查看表结构(比较全面的)
SELECT CASE WHEN col.colorder = 1 THEN obj.name
ELSE ''
END AS 表名,
col.colorder AS 序号 ,
col.name AS 列名 ,
ISNULL(ep.[value], '') AS 列说明 ,
t.name AS 数据类型 ,
col.length AS 长度 ,
ISNULL(COLUMNPROPERTY(col.id, col.name, 'Scale'), 0) AS 小数位数 ,
CASE WHEN COLUMNPROPERTY(col.id, col.name, 'IsIdentity') = 1 THEN '√'
ELSE ''
END AS 标识 ,
CASE WHEN EXISTS ( SELECT 1
FROM dbo.sysindexes si
INNER JOIN dbo.sysindexkeys sik ON si.id = sik.id
AND si.indid = sik.indid
INNER JOIN dbo.syscolumns sc ON sc.id = sik.id
AND sc.colid = sik.colid
INNER JOIN dbo.sysobjects so ON so.name = si.name
AND so.xtype = 'PK'
WHERE sc.id = col.id
AND sc.colid = col.colid ) THEN '√'
ELSE ''
END AS 主键 ,
CASE WHEN col.isnullable = 1 THEN '√'
ELSE ''
END AS 允许空 ,
ISNULL(comm.text, '') AS 默认值
FROM dbo.syscolumns col
LEFT JOIN dbo.systypes t ON col.xtype = t.xusertype
inner JOIN dbo.sysobjects obj ON col.id = obj.id
AND obj.xtype = 'U'
AND obj.status >= 0
LEFT JOIN dbo.syscomments comm ON col.cdefault = comm.id
LEFT JOIN sys.extended_properties ep ON col.id = ep.major_id
AND col.colid = ep.minor_id
AND ep.name = 'MS_Description'
LEFT JOIN sys.extended_properties epTwo ON obj.id = epTwo.major_id
AND epTwo.minor_id = 0
AND epTwo.name = 'MS_Description'
WHERE obj.name = 'AutoBillGetAndReturnBack'--表名
ORDER BY col.colorder ;