是否有可以返回表中列类型的 SQL 语句?
26 回答
使用 SQL Server:
SELECT DATA_TYPE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE
TABLE_SCHEMA = 'yourSchemaName' AND
TABLE_NAME = 'yourTableName' AND
COLUMN_NAME = 'yourColumnName'
TSQL 中最简单的方法是:
SELECT COLUMN_NAME, DATA_TYPE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'yourTableName'
对于 SQL Server,此系统存储过程将返回所有表信息,包括列数据类型:
exec sp_help YOURTABLENAME
在 TSQL/MSSQL 中,它看起来像:
SELECT t.name, c.name
FROM sys.tables t
JOIN sys.columns c ON t.object_id = c.object_id
JOIN sys.types y ON y.user_type_id = c.user_type_id
WHERE t.name = ''
如果您使用的是 MySQL,您可以尝试
SHOW COLUMNS FROM `tbl_name`;
否则你应该可以做
DESCRIBE `tbl_name`;
在 oracle SQL 中,您可以这样做:
SELECT
DATA_TYPE
FROM
all_tab_columns
WHERE
table_name = 'TABLE NAME' -- in uppercase
AND column_name = 'COLUMN NAME' -- in uppercase
为了建立上面的答案,以您需要声明列的相同格式获取列数据类型通常很有用。
例如,varchar(50)
,varchar(max)
,decimal(p, s)
。
这允许您这样做:
SELECT
[Name] = c.[name]
, [Type] =
CASE
WHEN tp.[name] IN ('varchar', 'char') THEN tp.[name] + '(' + IIF(c.max_length = -1, 'max', CAST(c.max_length AS VARCHAR(25))) + ')'
WHEN tp.[name] IN ('nvarchar','nchar') THEN tp.[name] + '(' + IIF(c.max_length = -1, 'max', CAST(c.max_length / 2 AS VARCHAR(25)))+ ')'
WHEN tp.[name] IN ('decimal', 'numeric') THEN tp.[name] + '(' + CAST(c.[precision] AS VARCHAR(25)) + ', ' + CAST(c.[scale] AS VARCHAR(25)) + ')'
WHEN tp.[name] IN ('datetime2') THEN tp.[name] + '(' + CAST(c.[scale] AS VARCHAR(25)) + ')'
ELSE tp.[name]
END
, [RawType] = tp.[name]
, [MaxLength] = c.max_length
, [Precision] = c.[precision]
, [Scale] = c.scale
FROM sys.tables t
JOIN sys.schemas s ON t.schema_id = s.schema_id
JOIN sys.columns c ON t.object_id = c.object_id
JOIN sys.types tp ON c.user_type_id = tp.user_type_id
WHERE s.[name] = 'dbo' AND t.[name] = 'MyTable'
使用 MS SQL 的另一个变体:
SELECT TYPE_NAME(system_type_id)
FROM sys.columns
WHERE name = 'column_name'
AND [object_id] = OBJECT_ID('[dbo].[table_name]');
使用 TSQL/MSSQL
此查询将为您提供:表名、列名、数据类型、数据类型长度和允许的空值
SELECT TABLE_NAME,COLUMN_NAME, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH, IS_NULLABLE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'your_table_name'
唯一需要更改的是 your_table_name。
这也适用,因为它只选择列名及其各自的字符类型
SELECT COLUMN_NAME ,DATA_TYPE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'Items';
要检索实际声明的数据类型,例如用于动态 SQL 到 ALTER COLUMN,可以使用以下内容:
SELECT
TABLE_NAME,
COLUMN_NAME,
DATA_TYPE
+ CASE WHEN DATA_TYPE IN ('char','nchar','varchar','nvarchar','binary','varbinary')
AND CHARACTER_MAXIMUM_LENGTH > 0 THEN
COALESCE('('+CONVERT(varchar,CHARACTER_MAXIMUM_LENGTH)+')','')
ELSE '' END
+ CASE WHEN DATA_TYPE IN ('decimal','numeric') THEN
COALESCE('('+CONVERT(varchar,NUMERIC_PRECISION)+','+CONVERT(varchar,NUMERIC_SCALE)+')','')
ELSE '' END
AS Declaration_Type,
CASE WHEN IS_NULLABLE='NO' THEN 'NOT ' ELSE '' END + 'NULL' AS Nullable
FROM INFORMATION_SCHEMA.COLUMNS
ORDER BY 1,2
对于IBM DB2:
SELECT TYPENAME FROM SYSCAT.COLUMNS WHERE TABSCHEMA='your_schema_name' AND TABNAME='your_table_name' AND COLNAME='your_column_name'
USE [YourDatabaseName]
GO
SELECT column_name 'Column Name',
data_type 'Data Type'
FROM information_schema.columns
WHERE table_name = 'YourTableName'
GO
这将返回值 Column Name,向您显示列的名称以及这些列的数据类型(ints、varchars 等)。
使用 TSQL/MSSQL
您可以使用INTO
关键字。
结果SELECT
变成了一个真实的TABLE
例子:select .... INTO real_table_name
后
sp_help real_table_name
从 SQL Server 2012 开始:
SELECT * FROM sys.dm_exec_describe_first_result_set( N'SELECT * FROM [my].[Table]', NULL, 0 );
使用这个查询
SELECT * FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'TABLE_NAME'
ORDER BY ORDINAL_POSITION
MS SQL 的另一个选项是将select
此处的查询替换为您想要其类型的查询:
declare @sql varchar(4000);
set @sql = 'select ''hi'' as greeting';
select * from master.sys.dm_exec_describe_first_result_set (@sql, Null, 0);
就我而言,我需要获取动态 SQL 的数据类型(颤抖!)无论如何,这是我创建的一个函数,它返回完整的数据类型。例如,它不会返回“十进制”,而是返回 DECIMAL(18,4):dbo.GetLiteralDataType
使用此查询获取 Schema、Table、Column、Type、max_length、is_nullable
SELECT QUOTENAME(SCHEMA_NAME(tb.[schema_id])) AS 'Schema'
,QUOTENAME(OBJECT_NAME(tb.[OBJECT_ID])) AS 'Table'
,C.NAME as 'Column'
,T.name AS 'Type'
,C.max_length
,C.is_nullable
FROM SYS.COLUMNS C INNER JOIN SYS.TABLES tb ON tb.[object_id] = C.[object_id]
INNER JOIN SYS.TYPES T ON C.system_type_id = T.user_type_id
WHERE tb.[is_ms_shipped] = 0
ORDER BY tb.[Name]
SHOW COLUMNS FROM //table_name// ;
它将为您提供有关表中所有列的信息。
对于Spark SQL:
DESCRIBE [db_name.]table_name column_name
由于有些人同时要求数据类型的精度,我想分享我为此目的创建的脚本。
SELECT TABLE_NAME As 'TableName'
COLUMN_NAME As 'ColumnName'
CONCAT(DATA_TYPE, '(', COALESCE(CHARACTER_MAXIMUM_LENGTH, NUMERIC_PRECISION, DATETIME_PRECISION, ''), IIF(NUMERIC_SCALE <> 0, CONCAT(', ', NUMERIC_SCALE), ''), ')', IIF(IS_NULLABLE = 'YES', ', null', ', not null')) As 'ColumnType'
FROM INFORMATION_SCHEMA.COLUMNS
WHERE -- ...
ORDER BY 'TableName', 'ColumnName'
它并不完美,但在大多数情况下都有效。
使用Sql-Server
在 vb60 中,您可以这样做:
Public Cn As ADODB.Connection
'open connection
Dim Rs As ADODB.Recordset
Set Rs = Cn.OpenSchema(adSchemaColumns, Array(Empty, Empty, UCase("Table"), UCase("field")))
'和样本(valRs 是我的函数 rs.fields("CHARACTER_MAXIMUM_LENGTH").value):
RT_Charactar_Maximum_Length = (ValRS(Rs, "CHARACTER_MAXIMUM_LENGTH"))
rt_Tipo = (ValRS(Rs, "DATA_TYPE"))
只要有人觉得这很有用。在 SQL Server 中:
sp_columns 'yourtablename'
这将提供所有列的详细信息。它为您提供 column_name、data_type、type_name、precision、length、iss_nullable 等。
我发现这对查询很有用,特别是如果它们使用聚合或多个表(SQL Server)
DECLARE @query nvarchar(max) = 'select * from yourtable';
EXEC sp_describe_first_result_set @query, null, 0;
对于Apache Derby ,如此答案所示:
select columndatatype from sys.syscolumns
where referenceid = (
select tableid from sys.systables
where tablename = 'YOUR_TABEL_NAME'
and columnname= 'YOUR_COLUMN_NAME')