试试这个:
select
c.COLUMN_NAME ,c.DATA_TYPE ,c.CHARACTER_MAXIMUM_LENGTH,c.NUMERIC_PRECISION,c.NUMERIC_PRECISION_RADIX ,c.NUMERIC_PRECISION_RADIX,c.NUMERIC_SCALE,c.DATETIME_PRECISION
--t.TABLE_CATALOG,t.TABLE_SCHEMA,t.TABLE_NAME
from INFORMATION_SCHEMA.Tables t
INNER JOIN INFORMATION_SCHEMA.Columns c ON t.TABLE_CATALOG=c.TABLE_CATALOG AND t.TABLE_SCHEMA=c.TABLE_SCHEMA AND t.TABLE_NAME=c.TABLE_NAME
WHERE t.TABLE_NAME='YourTableName' --<<<<
ORDER BY --t.TABLE_CATALOG,t.TABLE_SCHEMA,t.TABLE_NAME,
c.ORDINAL_POSITION
我不确定你是否想要not的数据类型,如果你想要一个表还是全部,你可以根据需要进行修改。
OP评论后编辑:
这是过程,如果您需要 table_catalog 和 table_schema 作为参数取消注释是必要的:
CREATE PROCEDURE GetColumnNames
(
--@TABLE_CATALOG nvarchar(128),
--@TABLE_SCHEMA nvarchar(128),
@TableName sysname
)
AS
select
c.COLUMN_NAME
,CASE
WHEN DATA_TYPE IN ('int','smalldatetime','datetime','smallint','bigint')THEN DATA_TYPE
WHEN DATA_TYPE='char' THEN 'char('+CONVERT(varchar(5),CHARACTER_MAXIMUM_LENGTH)+')'
WHEN DATA_TYPE='varchar' THEN 'varchar('+CONVERT(varchar(5),CHARACTER_MAXIMUM_LENGTH)+')'
WHEN DATA_TYPE='numeric' THEN 'numeric('+CONVERT(varchar(5),NUMERIC_PRECISION)+','+CONVERT(varchar(5),NUMERIC_SCALE)+')'
WHEN DATA_TYPE='decimal' THEN 'decimal('+CONVERT(varchar(5),NUMERIC_PRECISION)+','+CONVERT(varchar(5),NUMERIC_SCALE)+')'
ELSE DATA_TYPE
END AS DataType
,c.DATA_TYPE ,c.CHARACTER_MAXIMUM_LENGTH,c.NUMERIC_PRECISION,c.NUMERIC_PRECISION_RADIX ,c.NUMERIC_PRECISION_RADIX,c.NUMERIC_SCALE,c.DATETIME_PRECISION
from INFORMATION_SCHEMA.Columns c
WHERE c.TABLE_NAME=@TableName --AND c.TABLE_CATALOG=@TABLE_CATALOG AND c.TABLE_SCHEMA=@TABLE_SCHEMA
ORDER BY c.ORDINAL_POSITION
RETURN 0
GO
像这样使用它:
exec GetColumnNames 'yourtablename'
INFORMATION_SCHEMA.Columns有几个您可能感兴趣的列,因此请阅读文档并将查询修改为您需要的内容。