SQL语句来获取列类型SQL语句来获取列类型(SQL statement to get column

2019-05-12 15:42发布

是否有一个SQL语句可以在表中返回列的类型?

Answer 1:

使用SQL Server:

SELECT DATA_TYPE 
FROM INFORMATION_SCHEMA.COLUMNS
WHERE 
     TABLE_NAME = 'yourTableName' AND 
     COLUMN_NAME = 'yourColumnName'


Answer 2:

在TSQL最简单的方法是:

SELECT COLUMN_NAME, DATA_TYPE 
FROM INFORMATION_SCHEMA.COLUMNS 
WHERE TABLE_NAME = 'yourTableName'


Answer 3:

对于SQL Server,该系统存储过程将返回所有表信息,包括列数据类型:

exec sp_help YOURTABLENAME


Answer 4:

在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.system_type_id = c.system_type_id
WHERE t.name = ''


Answer 5:

在Oracle SQL你可以这样做:

SELECT
    DATA_TYPE
FROM
    all_tab_columns 
WHERE
    table_name = 'TABLE NAME' -- in uppercase
AND column_name = 'COLUMN NAME' -- in uppercase


Answer 6:

如果你使用MySQL,你可以尝试

SHOW COLUMNS FROM `tbl_name`;

在dev.mysql.com SHOW柱

否则,你应该能够做到

DESCRIBE `tbl_name`;


Answer 7:

使用MS SQL另一个变种

SELECT TYPE_NAME(system_type_id) 
FROM sys.columns 
WHERE name = 'column_name'
AND [object_id] = OBJECT_ID('[dbo].[table_name]');


Answer 8:

USE [YourDatabaseName]
GO

SELECT column_name 'Column Name',
data_type 'Data Type'
FROM information_schema.columns
WHERE table_name = 'YourTableName'
GO

这将返回值的列名,显示你的列的名称,以及这些列的数据类型(整型,VARCHAR处理等)。



Answer 9:

对于IBM DB2:

SELECT TYPENAME FROM SYSCAT.COLUMNS WHERE TABSCHEMA='your_schema_name' AND TABNAME='your_table_name' AND COLNAME='your_column_name'


Answer 10:

使用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。



Answer 11:

在我来说,我需要得到动态SQL数据类型(不寒而栗!)反正这里是我创建的返回完整的数据类型的函数。 例如,而不是返回“小数”,将返回DECIMAL(18,4): dbo.GetLiteralDataType



Answer 12:

使用此查询来获取模式,表,列,类型,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]


Answer 13:

使用TSQL / MSSQL

您可以使用INTO关键字。

结果SELECT成一个真正的表

例如: select .... INTO real_table_name

sp_help real_table_name


Answer 14:

对于星火SQL :

DESCRIBE [db_name.]table_name column_name


Answer 15:

对于Apache Derby进行如图这样的回答 :

select columndatatype from sys.syscolumns
  where referenceid = (
    select tableid from sys.systables
    where tablename = 'YOUR_TABEL_NAME'
    and columnname= 'YOUR_COLUMN_NAME')


Answer 16:

在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’)值):

 RT_Charactar_Maximum_Length = (ValRS(Rs, "CHARACTER_MAXIMUM_LENGTH"))
        rt_Tipo = (ValRS(Rs, "DATA_TYPE"))


Answer 17:

检索实际声明的数据类型,例如用于动态SQL中使用ALTER给列,这样的事情,可以用:

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


Answer 18:

由于一些人要求的精确度,以及与数据类型,我想和大家分享,我已经为这样的目的而创建我的脚本。

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



Answer 19:

SHOW COLUMNS FROM //table_name// ;

它会给你有关从该表中的所有列。



文章来源: SQL statement to get column type
标签: sql schema