Answers:
使用SQL Server:
SELECT DATA_TYPE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE
TABLE_NAME = 'yourTableName' AND
COLUMN_NAME = 'yourColumnName'
AND TABLE_SCHEMA = 'yourSchema'
varchar(255)
代替varchar
和int(11)
代替int
?
CHARACTER_MAXIMUM_LENGTH
在INFORMATION_SCHEMA.COLUMNS
。只需执行一次SELECT * FROM INFORMATION_SCHEMA.COLUMNS
即可查看所有可用列。
TSQL中最简单的方法是:
SELECT COLUMN_NAME, DATA_TYPE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = '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 = ''
JOIN sys.types y ON y.user_type_id = c.user_type_id
system_type_id不是唯一的。sys.columns doc
DESCRIBE
语法在Oracle中也有效,但是MsSQL将不接受此语法。
information_schema.COLUMNS
。
为了建立以上答案,通常以与声明列相同的格式获取列数据类型通常很有用。
例如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'
要检索实际声明的数据类型(例如,在动态SQL中使用以更改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
就我而言,无论如何,我都需要获取Dynamic SQL的数据类型(Shudder!),这是我创建的返回完整数据类型的函数。例如,不是返回“十进制”,而是返回DECIMAL(18,4):dbo.GetLiteralDataType
使用TSQL / MSSQL
您可以使用INTO
关键字。
SELECT
转换成真实表的结果
例: select .... INTO real_table_name
后
sp_help real_table_name
使用此查询获取架构,表,列,类型,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// ;
它将为您提供有关表中所有列的信息。
对于Apache Derby,如以下答案所示:
select columndatatype from sys.syscolumns
where referenceid = (
select tableid from sys.systables
where tablename = 'YOUR_TABEL_NAME'
and columnname= 'YOUR_COLUMN_NAME')
在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"))
由于有些人也在要求数据类型的精度,所以我想分享我为此目的创建的脚本。
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
sys.syscolumns
例如,SQL Server有该表。