Fra*_*s P 403
使用SQL Server:
SELECT DATA_TYPE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE
TABLE_NAME = 'yourTableName' AND
COLUMN_NAME = 'yourColumnName'
Run Code Online (Sandbox Code Playgroud)
Hac*_*ack 65
TSQL中最简单的方法是:
SELECT COLUMN_NAME, DATA_TYPE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'yourTableName'
Run Code Online (Sandbox Code Playgroud)
小智 37
对于SQL Server,此系统存储过程将返回所有表信息,包括列数据类型:
exec sp_help YOURTABLENAME
Run Code Online (Sandbox Code Playgroud)
jTC*_*jTC 14
在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 = ''
Run Code Online (Sandbox Code Playgroud)
Ass*_*saf 12
在oracle SQL中你会这样做:
SELECT
DATA_TYPE
FROM
all_tab_columns
WHERE
table_name = 'TABLE NAME' -- in uppercase
AND column_name = 'COLUMN NAME' -- in uppercase
Run Code Online (Sandbox Code Playgroud)
如果你使用MySQL,你可以试试
SHOW COLUMNS FROM `tbl_name`;
Run Code Online (Sandbox Code Playgroud)
否则你应该可以做到
DESCRIBE `tbl_name`;
Run Code Online (Sandbox Code Playgroud)
为了建立在上面的答案的基础上,以声明列所需的相同格式获取列数据类型通常很有用。
例如,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'
Run Code Online (Sandbox Code Playgroud)
小智 9
这也有效,因为它仅选择列名称及其各自的字符类型
SELECT COLUMN_NAME ,DATA_TYPE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'Items';
Run Code Online (Sandbox Code Playgroud)
使用 TSQL/MSSQL
此查询将获得:表名、列名、数据类型、数据类型长度和允许的空值
SELECT TABLE_NAME,COLUMN_NAME, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH, IS_NULLABLE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'your_table_name'
Run Code Online (Sandbox Code Playgroud)
唯一需要更改的是your_table_name。
要检索实际声明的数据类型,例如在动态 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
Run Code Online (Sandbox Code Playgroud)
使用MS SQL的另一个变体:
SELECT TYPE_NAME(system_type_id)
FROM sys.columns
WHERE name = 'column_name'
AND [object_id] = OBJECT_ID('[dbo].[table_name]');
Run Code Online (Sandbox Code Playgroud)
小智 5
从 SQL Server 2012 开始:
SELECT * FROM sys.dm_exec_describe_first_result_set( N'SELECT * FROM [my].[Table]', NULL, 0 );
Run Code Online (Sandbox Code Playgroud)
只要有人觉得这有用。在 SQL Server 中:
sp_columns 'yourtablename'
Run Code Online (Sandbox Code Playgroud)
这将提供所有列的详细信息。它为您提供列名称、数据类型、类型名称、精度、长度、iss_nullable 等。
| 归档时间: |
|
| 查看次数: |
494334 次 |
| 最近记录: |