如何编写可以列出具有特定数据类型的所有表的查询,它应该列出所有表而不是视图,我有一个脚本列出具有特定数据类型的所有表以及所有视图,我怎样才能构建这样的东西,它可以给我所有的表而不是视图。
当前查询: -
SELECT
s.name AS 'schema',
ts.name AS TableName,
c.name AS column_name,
c.column_id,
SCHEMA_NAME(t.schema_id) AS DatatypeSchema,
t.name AS Datatypename,
t.is_user_defined,
t.is_assembly_type,
c.is_nullable,
c.max_length,
c.PRECISION,
c.scale
FROM sys.columns AS c
INNER JOIN sys.types AS t
ON c.user_type_id = t.user_type_id
INNER JOIN sys.tables ts
ON ts.OBJECT_ID = c.OBJECT_ID
INNER JOIN sys.schemas s
ON s.schema_id = ts.schema_id
ORDER BY s.name, ts.name, c.column_id
答案 0 :(得分:1)
使用此查询
SELECT
*
FROM INFORMATION_SCHEMA.TABLES T
WHERE TABLE_TYPE = 'BASE TABLE'--Give VIEW for Views
AND EXISTS
(
SELECT
1
FROM INFORMATION_SCHEMA.COLUMNS C
WHERE C.TABLE_NAME = T.TABLE_NAME
AND C.TABLE_SCHEMA = T.TABLE_SCHEMA
AND C.DATA_TYPE = 'INT'
)
这将返回带有一个整数列的所有表
答案 1 :(得分:0)
您可以执行以下操作:
SELECT DISTINCT
t.name AS 'Table_Name'
,c.name AS 'Column_Name'
,I.DATA_TYPE
FROM sys.columns c
JOIN sys.tables t ON c.object_id = t.object_id
JOIN INFORMATION_SCHEMA.COLUMNS I on I.COLUMN_NAME = c.name
Where I.Data_Type = 'INT'
以上内容包括列,但您可以将其删除。
查询的工作原理是查看sys.columns
,sys.tables
和information_schema.columns
。另外,通过查看这些表格,您可以根据需要调整查询。
答案 2 :(得分:0)
根据数据类型,您可以使用。
select * from INFORMATION_SCHEMA.COLUMNS
where DATA_TYPE = 'INT'
答案 3 :(得分:0)
尝试以下查询
select c.TABLE_CATALOG ,c.TABLE_SCHEMA, c.TABLE_NAME , c.COLUMN_NAME
from INFORMATION_SCHEMA.COLUMNS c
Join INFORMATION_SCHEMA.TABLES t
on c.TABLE_CATALOG =t.TABLE_CATALOG
and c.TABLE_SCHEMA =t.TABLE_SCHEMA
and c.TABLE_NAME =t.TABLE_NAME
where t.TABLE_TYPE ='BASE TABLE'
and c.DATA_TYPE ='int'
注意:int是数据类型。