我需要在SQL server上编写一个查询,以获得特定表中的列列表,其相关数据类型(长度)以及它们是否不为空。我已经做到了这么多。

但现在我还需要得到,在同一个表中,对一个列- TRUE,如果该列是一个主键。

我该怎么做呢?

我的期望输出是:

Column name | Data type | Length | isnull | Pk

当前回答

在SQL 2012中,你可以使用:

EXEC sp_describe_first_result_set N'SELECT * FROM [TableName]'

这将为您提供列名及其属性。

其他回答

IF EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.TABLES 
     WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_NAME = 'Table')
      BEGIN
        SELECT COLS.COLUMN_NAME, COLS.DATA_TYPE, COLS.CHARACTER_MAXIMUM_LENGTH, 
              (SELECT 'Yes' FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS TC JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE KCU
                              ON COLS.TABLE_NAME = TC.TABLE_NAME 
                             AND TC.CONSTRAINT_TYPE = 'PRIMARY KEY'
                             AND KCU.TABLE_NAME = TC.TABLE_NAME
                             AND KCU.CONSTRAINT_NAME = TC.CONSTRAINT_NAME
                             AND KCU.COLUMN_NAME = COLS.COLUMN_NAME) AS KeyX
        FROM INFORMATION_SCHEMA.COLUMNS COLS WHERE TABLE_NAME = 'Table' ORDER BY KeyX DESC, COLUMN_NAME
      END

抛出了另一种在SQL server中解决问题的方法。 我的小脚本在这里应该返回列名,数据类型,是空的,约束和索引名称。 您还可以包括任何额外的列,如精度,比例… (你需要用你的数据库名、Schema name和Table name替换数据库名) .返回列的顺序与从'select * from table'中返回列的顺序相同

USE DBA -- Replace Database Name with yours

DECLARE @SCHEMA VARCHAR(MAX)
DECLARE @TABLE_NAME VARCHAR(MAX)
DECLARE @SCHEMA_TABLE_NAME VARCHAR(MAX)

SET @SCHEMA = REPLACE(REPLACE('[SCHEMA NAME]', '[', ''), ']', '')--Replace Schema Name with yours
SET @TABLE_NAME = REPLACE(REPLACE('[TABLE NAME]', '[', ''), ']', '') --' Replace Table  Name with yours
SET @SCHEMA_TABLE_NAME = @SCHEMA + '.' + @TABLE_NAME;


WITH SchemaColumns
AS (
    SELECT C.COLUMN_NAME,
        IS_NULLABLE,
        DATA_TYPE,
        CHARACTER_MAXIMUM_LENGTH,
        C.ORDINAL_POSITION
    FROM INFORMATION_SCHEMA.COLUMNS AS C
    WHERE C.TABLE_SCHEMA = @SCHEMA
        AND C.TABLE_NAME = @TABLE_NAME
    ),
SchemaConstraints
AS (
    SELECT CN.COLUMN_NAME,
        CC.CONSTRAINT_TYPE
    FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS CC
    INNER JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE AS CN ON CC.CONSTRAINT_NAME = CC.CONSTRAINT_NAME
    WHERE CC.TABLE_SCHEMA = @SCHEMA
        AND CC.TABLE_NAME = @TABLE_NAME
    ),
SchemaIndex
AS (
    SELECT I.name AS index_name,
        COL_NAME(IC.object_id, IC.column_id) AS column_name,
        IC.index_column_id,
        IC.key_ordinal,
        IC.is_included_column
    FROM sys.indexes AS i
    INNER JOIN sys.index_columns AS IC ON I.object_id = IC.object_id
        AND I.index_id = IC.index_id
    WHERE I.object_id = OBJECT_ID(@SCHEMA_TABLE_NAME)
    )
SELECT ISNULL(SchemaColumns.COLUMN_NAME, '') "Column Name",
    CASE 
        WHEN SchemaColumns.CHARACTER_MAXIMUM_LENGTH IS NULL
            THEN UPPER(ISNULL(SchemaColumns.DATA_TYPE, ''))
        ELSE CONCAT (
                UPPER(ISNULL(SchemaColumns.DATA_TYPE, '')),
                '(',
                CAST(SchemaColumns.CHARACTER_MAXIMUM_LENGTH AS VARCHAR(50)),
                ')'
                )
        END "Data Type",
    SchemaColumns.IS_NULLABLE "Is Nullable",
    ISNULL(SchemaConstraints.CONSTRAINT_TYPE, '-') "Constraints",
    ISNULL(STRING_AGG(CONVERT(NVARCHAR(max), SchemaIndex.INDEX_NAME), CHAR(13)), '-') "Indexes Names"
FROM SchemaColumns
LEFT JOIN SchemaConstraints ON SchemaConstraints.COLUMN_NAME = SchemaColumns.COLUMN_NAME
LEFT JOIN SchemaIndex ON SchemaColumns.COLUMN_NAME = SchemaIndex.COLUMN_NAME
GROUP BY SchemaColumns.COLUMN_NAME,
    SchemaColumns.DATA_TYPE,
    SchemaColumns.CHARACTER_MAXIMUM_LENGTH,
    SchemaColumns.IS_NULLABLE,
    SchemaConstraints.CONSTRAINT_TYPE,
    SchemaColumns.ORDINAL_POSITION
ORDER BY SchemaColumns.ORDINAL_POSITION

我有点惊讶居然没人提

sp_help 'mytable'
SELECT COLUMN_NAME, IS_NULLABLE, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH FROM information_schema.columns WHERE table_name = '<name_of_table_or_view>'

在上面的语句中运行SELECT *,以查看information_schema. schema是什么。列的回报。

这个问题之前已经回答过了- https://stackoverflow.com/a/11268456/6169225

在SQL 2012中,你可以使用:

EXEC sp_describe_first_result_set N'SELECT * FROM [TableName]'

这将为您提供列名及其属性。