SQL Server 中“描述表”的等价物是啥?

Posted

技术标签:

【中文标题】SQL Server 中“描述表”的等价物是啥?【英文标题】:What is the equivalent of 'describe table' in SQL Server?SQL Server 中“描述表”的等价物是什么? 【发布时间】:2010-09-24 01:51:53 【问题描述】:

我有一个 SQL Server 数据库,我想知道它有哪些列和类型。我宁愿通过查询而不是使用像企业管理器这样的 GUI 来做到这一点。有没有办法做到这一点?

【问题讨论】:

另一个 *** [问题][1] 已回答 [1]:***.com/questions/11078106/… 我不明白为什么微软没有事先设置这个选项。这是必须具备的功能。 【参考方案1】:

您可以使用sp_columns 存储过程:

exec sp_columns MyTable

【讨论】:

谢谢 - 我正准备用 T-SQL 而不是 MSSQL 发布同样的问题。 请注意:不要将表名放在引号中,也不要使用 TableOwner.TableName 语法。 如果您在运行此查询时没有得到任何结果,请参阅 here。 如果不想使用存储过程,请使用 Select * From INFORMATION_SCHEMA.COLUMNS Where TABLE_NAME = 'TABLENAME' sp_columns 的输出在 sqlcmd 中是非常不可读的,即使使用巨大的终端宽度也是如此。下面使用select 的解决方案更适合 sqlcmd 用户。【参考方案2】:

有几种方法可以获取有关表的元数据:

EXEC sp_help tablename

将返回几个结果集,描述表、列和约束。

INFORMATION_SCHEMA 视图将为您提供所需的信息,但遗憾的是您必须查询视图并手动加入它们。

【讨论】:

对我来说它没有“@”字符 EXEC sp_help 'table_name' 这是 Viranja 答案的更正确版本。但是,@ 的语法不正确。 看起来@tablename 是示例中的变量。 sp 双向工作,使用字符串或“普通”表名(exec sp_help Employeesexec sp_help 'Employees' 仅供参考:架构(甚至数据库)可以在刻度范围内:execute sp_help 'db.sch.your_table 非常有帮助。谢谢。【参考方案3】:

如果你不想使用存储过程,这里有一个简单的查询版本

select * 
  from information_schema.columns 
 where table_name = 'aspnet_Membership'
 order by ordinal_position

【讨论】:

在我的情况下,如果由于某种原因这些存储的过程不可用,这也适用。 存储过程不灵活,对像我这样天真的 sqlcmd 用户非常不利 喜欢这个,因为它按名称订购:D【参考方案4】:

您可以使用以下内容:sp_help tablename

示例:sp_help Customer

或使用快捷键

Select 所需的表并按 ALT+F1

示例:客户按 ALT+F1

【讨论】:

请注意,如果您的表在用户定义的模式中,则应将其从查询中排除。所有包含同名表的架构都将出现在结果集中名为“TABLE_OWNER”的列中。 请注意,要使快捷方式起作用,必须完全选择表/视图/过程。 SSMS 不会自动扩展选择范围(我本以为会这样)。 Ctrl+W可用于扩大选择范围,选择全名。【参考方案5】:

使用此查询

Select * From INFORMATION_SCHEMA.COLUMNS Where TABLE_NAME = 'TABLENAME'

【讨论】:

如果你先说“使用 MyDatabase”,对我有用。 我喜欢这个,因为它也适用于 mysql,所以我不必在我的代码中更改我的 SQL 这个答案与 5 年前的***.com/a/319424/695671 没有任何不同。我不认为重复的答案有用。【参考方案6】:

除了其他答案中显示的方式,还可以使用

SELECT TOP 0 * FROM table_name

这将为您提供每列的名称,其中没有结果,并且几乎可以立即以最小的开销完成。

【讨论】:

TOP 1 也会给出示例数据,这可能更具说明性。【参考方案7】:

请使用以下sql查询;这适用于我的情况。

select * FROM   INFORMATION_SCHEMA.Columns where table_name = 'tablename';

【讨论】:

2008 年和 2014 年给出了相同的答案。我不认为重复的答案有用。【参考方案8】:

只需选择表格并按Alt+F1,

它将显示有关表的所有信息,如列名、数据类型、键等。

【讨论】:

OP 要求使用无 GUI 的方法 -1 因为the same advice 已在 2014 年被其他用户提供。请尊重他人的时间。【参考方案9】:

我在 t-sql 中写了一个类似 select 的 sql*plus DESC(RIBE)(也显示列 cmets):

USE YourDB
GO

DECLARE @objectName NVARCHAR(128) = 'YourTable';

SELECT
  a.[NAME]
 ,a.[TYPE]
 ,a.[CHARSET]
 ,a.[COLLATION]
 ,a.[NULLABLE]
 ,a.[DEFAULT]
 ,b.[COMMENTS]
-- ,a.[ORDINAL_POSITION]
FROM
  (
    SELECT
      COLUMN_NAME                                     AS [NAME]
     ,CASE DATA_TYPE
        WHEN 'char'       THEN DATA_TYPE  + '(' + CAST(CHARACTER_MAXIMUM_LENGTH AS VARCHAR) + ')'
        WHEN 'numeric'    THEN DATA_TYPE  + '(' + CAST(NUMERIC_PRECISION AS VARCHAR) + ', ' + CAST(NUMERIC_SCALE AS VARCHAR) + ')'
        WHEN 'nvarchar'   THEN DATA_TYPE  + '(' + CAST(CHARACTER_MAXIMUM_LENGTH AS VARCHAR) + ')'
        WHEN 'varbinary'  THEN DATA_TYPE + '(' + CAST(CHARACTER_MAXIMUM_LENGTH AS VARCHAR) + ')'
        WHEN 'varchar'    THEN DATA_TYPE   + '(' + CAST(CHARACTER_MAXIMUM_LENGTH AS VARCHAR) + ')'
        ELSE DATA_TYPE
      END                                             AS [TYPE]
     ,CHARACTER_SET_NAME                              AS [CHARSET]
     ,COLLATION_NAME                                  AS [COLLATION]
     ,IS_NULLABLE                                     AS [NULLABLE]
     ,COLUMN_DEFAULT                                  AS [DEFAULT]
     ,ORDINAL_POSITION
    FROM   
      INFORMATION_SCHEMA.COLUMNS
    WHERE
      TABLE_NAME = @objectName
  ) a
  FULL JOIN
  (
   SELECT
     CAST(value AS NVARCHAR)                        AS [COMMENTS]
    ,CAST(objname AS NVARCHAR)                      AS [NAME]
   FROM
     ::fn_listextendedproperty ('MS_Description', 'user', 'dbo', 'table', @objectName, 'column', default)
  ) b
  ON a.NAME COLLATE YourCollation = b.NAME COLLATE YourCollation
ORDER BY
  a.[ORDINAL_POSITION];

上面提到的 select 可以在系统标记的存储过程中使用,并且可以通过简单的方式从实例的任何数据库中调用:

USE master;
GO

IF OBJECT_ID('sp_desc', 'P') IS NOT NULL
  DROP PROCEDURE sp_desc
GO

CREATE PROCEDURE sp_desc (
  @tableName  nvarchar(128)
) AS
BEGIN
  DECLARE @dbName       sysname;
  DECLARE @schemaName   sysname;
  DECLARE @objectName   sysname;
  DECLARE @objectID     int;
  DECLARE @tmpTableName varchar(100);
  DECLARE @sqlCmd       nvarchar(4000);

  SELECT @dbName = PARSENAME(@tableName, 3);
  IF @dbName IS NULL SELECT @dbName = DB_NAME();

  SELECT @schemaName = PARSENAME(@tableName, 2);
  IF @schemaName IS NULL SELECT @schemaName = SCHEMA_NAME();

  SELECT @objectName = PARSENAME(@tableName, 1);
  IF @objectName IS NULL
    BEGIN
      PRINT 'Object is missing from your function call!';
      RETURN;
    END;

  SELECT @objectID = OBJECT_ID(@dbName + '.' + @schemaName + '.' + @objectName);
  IF @objectID IS NULL
    BEGIN
      PRINT 'Object [' + @dbName + '].[' + @schemaName + '].[' + @objectName + '] does not exist!';
      RETURN;
    END;

  SELECT @tmpTableName = '#tmp_DESC_' + CAST(@@SPID AS VARCHAR) + REPLACE(REPLACE(REPLACE(REPLACE(CAST(CONVERT(CHAR, GETDATE(), 121) AS VARCHAR), '-', ''), ' ', ''), ':', ''), '.', '');
  --PRINT @tmpTableName;
  SET @sqlCmd = '
    USE ' + @dbName + '
    CREATE TABLE ' + @tmpTableName + ' (
      [NAME]              nvarchar(128) NOT NULL
     ,[TYPE]              varchar(50)
     ,[CHARSET]           varchar(50)
     ,[COLLATION]         varchar(50)
     ,[NULLABLE]          varchar(3)
     ,[DEFAULT]           nvarchar(4000)
     ,[COMMENTS]          nvarchar(3750));

    INSERT INTO ' + @tmpTableName + '
    SELECT
      a.[NAME]
     ,a.[TYPE]
     ,a.[CHARSET]
     ,a.[COLLATION]
     ,a.[NULLABLE]
     ,a.[DEFAULT]
     ,b.[COMMENTS]
    FROM
      (
        SELECT
          COLUMN_NAME                                     AS [NAME]
         ,CASE DATA_TYPE
            WHEN ''char''      THEN DATA_TYPE + ''('' + CAST(CHARACTER_MAXIMUM_LENGTH AS VARCHAR) + '')''
            WHEN ''numeric''   THEN DATA_TYPE + ''('' + CAST(NUMERIC_PRECISION AS VARCHAR) + '', '' + CAST(NUMERIC_SCALE AS VARCHAR) + '')''
            WHEN ''nvarchar''  THEN DATA_TYPE + ''('' + CAST(CHARACTER_MAXIMUM_LENGTH AS VARCHAR) + '')''
            WHEN ''varbinary'' THEN DATA_TYPE + ''('' + CAST(CHARACTER_MAXIMUM_LENGTH AS VARCHAR) + '')''
            WHEN ''varchar''   THEN DATA_TYPE + ''('' + CAST(CHARACTER_MAXIMUM_LENGTH AS VARCHAR) + '')''
            ELSE DATA_TYPE
          END                                             AS [TYPE]
         ,CHARACTER_SET_NAME                              AS [CHARSET]
         ,COLLATION_NAME                                  AS [COLLATION]
         ,IS_NULLABLE                                     AS [NULLABLE]
         ,COLUMN_DEFAULT                                  AS [DEFAULT]
         ,ORDINAL_POSITION
        FROM   
          INFORMATION_SCHEMA.COLUMNS
        WHERE   
          TABLE_NAME = ''' + @objectName + '''
      ) a
      FULL JOIN
      (
         SELECT
           CAST(value AS NVARCHAR)                        AS [COMMENTS]
          ,CAST(objname AS NVARCHAR)                      AS [NAME]
         FROM
           ::fn_listextendedproperty (''MS_Description'', ''user'', ''' + @schemaName + ''', ''table'', ''' + @objectName + ''', ''column'', default)
      ) b
      ON a.NAME COLLATE Hungarian_CI_AS = b.NAME COLLATE Hungarian_CI_AS
    ORDER BY
      a.[ORDINAL_POSITION];

    SELECT * FROM ' + @tmpTableName + ';'

    --PRINT @sqlCmd;

    EXEC sp_executesql @sqlCmd;
    RETURN;
END;
GO

EXEC sys.sp_MS_marksystemobject sp_desc
GO

执行过程类型:

EXEC sp_desc 'YourDB.YourSchema.YourTable';

如果要获取当前数据库(和模式)简单类型的对象的描述:

EXEC sp_desc 'YourTable';

由于 sp_desc 是系统标记的过程,你甚至可以离开 exec 命令(反正不推荐):

sp_desc 'YourTable';

【讨论】:

【参考方案10】:

相当于Oracle的describe命令的SQL Server是存储过程sp_help

describe 命令为您提供有关列名、类型、长度等的信息。

在 SQL Server 中,假设您想在数据库 'mydb' 中的模式 'myschema' 中描述一个表 'mytable',您可以执行以下操作:

USE mydb;
exec sp_help 'myschema.mytable';

【讨论】:

【参考方案11】:

您可以使用 sp_help 'TableName'

【讨论】:

【参考方案12】:

试试看:

EXEC [ServerName].[DatabaseName].dbo.sp_columns 'TableName'

并且可以得到一些表结构信息,比如:

TABLE_QUALIFIER、TABLE_OWNER、TABLE_NAME、COLUMN_NAME、DATA_TYPE、TYPE_NAME...

【讨论】:

你能解释一下答案吗? 谢谢提醒,我加了一些解释。【参考方案13】:

除了上面的问题,如果我们在数据库中有db_name.dbo.table_name这样的表,我们可以使用以下步骤

    连接数据库

    USE db_name;

    使用EXEC sp_help,如果您有dbo 作为架构,请不要忘记将表名设置为'dbo.tablename'

    exec sp_help 'dbo.table_name'

这应该可行!

【讨论】:

【参考方案14】:

这些答案的问题在于您缺少关键信息。 虽然这有点乱,但这是我想出的一个快速版本,以确保它包含与 MySQL Describe 显示的相同信息。

Select SC.name AS 'Field', ISC.DATA_TYPE AS 'Type', ISC.CHARACTER_MAXIMUM_LENGTH AS 'Length', SC.IS_NULLABLE AS 'Null', I.is_primary_key AS 'Key', SC.is_identity AS 'Identity'
From sys.columns AS SC 
LEFT JOIN sys.index_columns AS IC
ON IC.object_id = OBJECT_ID('dbo.Expenses') AND 
IC.column_id = SC.column_id
LEFT JOIN sys.indexes AS I 
ON I.object_id = OBJECT_ID('dbo.Expenses') AND 
IC.index_id = I.index_id
LEFT JOIN information_schema.columns ISC
ON ISC.TABLE_NAME = 'Expenses'
AND ISC.COLUMN_NAME = SC.name
WHERE SC.object_id = OBJECT_ID('dbo.Expenses')

【讨论】:

is_primary_key 永远不会有值有什么原因吗?【参考方案15】:

这是我在EntityFramework Reverse POCO Generator 中使用的代码(可用here)

表 SQL:

SELECT  c.TABLE_SCHEMA AS SchemaName,
        c.TABLE_NAME AS TableName,
        t.TABLE_TYPE AS TableType,
        c.ORDINAL_POSITION AS Ordinal,
        c.COLUMN_NAME AS ColumnName,
        CAST(CASE WHEN IS_NULLABLE = 'YES' THEN 1
                  ELSE 0
             END AS BIT) AS IsNullable,
        DATA_TYPE AS TypeName,
        ISNULL(CHARACTER_MAXIMUM_LENGTH, 0) AS [MaxLength],
        CAST(ISNULL(NUMERIC_PRECISION, 0) AS INT) AS [Precision],
        ISNULL(COLUMN_DEFAULT, '') AS [Default],
        CAST(ISNULL(DATETIME_PRECISION, 0) AS INT) AS DateTimePrecision,
        ISNULL(NUMERIC_SCALE, 0) AS Scale,
        CAST(COLUMNPROPERTY(OBJECT_ID(QUOTENAME(c.TABLE_SCHEMA) + '.' + QUOTENAME(c.TABLE_NAME)), c.COLUMN_NAME, 'IsIdentity') AS BIT) AS IsIdentity,
        CAST(CASE WHEN COLUMNPROPERTY(OBJECT_ID(QUOTENAME(c.TABLE_SCHEMA) + '.' + QUOTENAME(c.TABLE_NAME)), c.COLUMN_NAME, 'IsIdentity') = 1 THEN 1
                  WHEN COLUMNPROPERTY(OBJECT_ID(QUOTENAME(c.TABLE_SCHEMA) + '.' + QUOTENAME(c.TABLE_NAME)), c.COLUMN_NAME, 'IsComputed') = 1 THEN 1
                  WHEN DATA_TYPE = 'TIMESTAMP' THEN 1
                  ELSE 0
             END AS BIT) AS IsStoreGenerated,
        CAST(CASE WHEN pk.ORDINAL_POSITION IS NULL THEN 0
                  ELSE 1
             END AS BIT) AS PrimaryKey,
        ISNULL(pk.ORDINAL_POSITION, 0) PrimaryKeyOrdinal,
        CAST(CASE WHEN fk.COLUMN_NAME IS NULL THEN 0
                  ELSE 1
             END AS BIT) AS IsForeignKey
FROM    INFORMATION_SCHEMA.COLUMNS c
        LEFT OUTER JOIN (SELECT u.TABLE_SCHEMA,
                                u.TABLE_NAME,
                                u.COLUMN_NAME,
                                u.ORDINAL_POSITION
                         FROM   INFORMATION_SCHEMA.KEY_COLUMN_USAGE u
                                INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc
                                    ON u.TABLE_SCHEMA = tc.CONSTRAINT_SCHEMA
                                       AND u.TABLE_NAME = tc.TABLE_NAME
                                       AND u.CONSTRAINT_NAME = tc.CONSTRAINT_NAME
                         WHERE  CONSTRAINT_TYPE = 'PRIMARY KEY') pk
            ON c.TABLE_SCHEMA = pk.TABLE_SCHEMA
               AND c.TABLE_NAME = pk.TABLE_NAME
               AND c.COLUMN_NAME = pk.COLUMN_NAME
        LEFT OUTER JOIN (SELECT DISTINCT
                                u.TABLE_SCHEMA,
                                u.TABLE_NAME,
                                u.COLUMN_NAME
                         FROM   INFORMATION_SCHEMA.KEY_COLUMN_USAGE u
                                INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc
                                    ON u.TABLE_SCHEMA = tc.CONSTRAINT_SCHEMA
                                       AND u.TABLE_NAME = tc.TABLE_NAME
                                       AND u.CONSTRAINT_NAME = tc.CONSTRAINT_NAME
                         WHERE  CONSTRAINT_TYPE = 'FOREIGN KEY') fk
            ON c.TABLE_SCHEMA = fk.TABLE_SCHEMA
               AND c.TABLE_NAME = fk.TABLE_NAME
               AND c.COLUMN_NAME = fk.COLUMN_NAME
        INNER JOIN INFORMATION_SCHEMA.TABLES t
            ON c.TABLE_SCHEMA = t.TABLE_SCHEMA
               AND c.TABLE_NAME = t.TABLE_NAME
WHERE c.TABLE_NAME NOT IN ('EdmMetadata', '__MigrationHistory')

外键SQL:

SELECT  FK.name AS FK_Table,
        FkCol.name AS FK_Column,
        PK.name AS PK_Table,
        PkCol.name AS PK_Column,
        OBJECT_NAME(f.object_id) AS Constraint_Name,
        SCHEMA_NAME(FK.schema_id) AS fkSchema,
        SCHEMA_NAME(PK.schema_id) AS pkSchema,
        PkCol.name AS primarykey,
        k.constraint_column_id AS ORDINAL_POSITION
FROM    sys.objects AS PK
        INNER JOIN sys.foreign_keys AS f
            INNER JOIN sys.foreign_key_columns AS k
                ON k.constraint_object_id = f.object_id
            INNER JOIN sys.indexes AS i
                ON f.referenced_object_id = i.object_id
                   AND f.key_index_id = i.index_id
            ON PK.object_id = f.referenced_object_id
        INNER JOIN sys.objects AS FK
            ON f.parent_object_id = FK.object_id
        INNER JOIN sys.columns AS PkCol
            ON f.referenced_object_id = PkCol.object_id
               AND k.referenced_column_id = PkCol.column_id
        INNER JOIN sys.columns AS FkCol
            ON f.parent_object_id = FkCol.object_id
               AND k.parent_column_id = FkCol.column_id
ORDER BY FK_Table, FK_Column

扩展属性:

SELECT  s.name AS [schema],
        t.name AS [table],
        c.name AS [column],
        value AS [property]
FROM    sys.extended_properties AS ep
        INNER JOIN sys.tables AS t
            ON ep.major_id = t.object_id
        INNER JOIN sys.schemas AS s
            ON s.schema_id = t.schema_id
        INNER JOIN sys.columns AS c
            ON ep.major_id = c.object_id
               AND ep.minor_id = c.column_id
WHERE   class = 1
ORDER BY t.name

【讨论】:

【参考方案16】:

我喜欢这种格式:

name     DataType      Collation             Constraints         PK  FK          Comment

id       int                                 NOT NULL IDENTITY   PK              Order Line Id
pid      int                                 NOT NULL                tbl_orders  Order Id
itemCode varchar(10)   Latin1_General_CI_AS  NOT NULL                            Product Code

所以我使用了这个:

DECLARE @tname varchar(100) = 'yourTableName';

SELECT  col.name,

        CASE typ.name
            WHEN 'nvarchar' THEN 'nvarchar('+CAST((col.max_length / 2) as varchar)+')'
            WHEN 'varchar' THEN 'varchar('+CAST(col.max_length as varchar)+')'
            WHEN 'char' THEN 'char('+CAST(col.max_length as varchar)+')'
            WHEN 'nchar' THEN 'nchar('+CAST((col.max_length / 2) as varchar)+')'
            WHEN 'binary' THEN 'binary('+CAST(col.max_length as varchar)+')'
            WHEN 'varbinary' THEN 'varbinary('+CAST(col.max_length as varchar)+')'
            WHEN 'numeric' THEN 'numeric('+CAST(col.precision as varchar)+(CASE WHEN col.scale = 0 THEN '' ELSE ','+CAST(col.scale as varchar) END) +')'
            WHEN 'decimal' THEN 'decimal('+CAST(col.precision as varchar)+(CASE WHEN col.scale = 0 THEN '' ELSE ','+CAST(col.scale as varchar) END) +')'
            ELSE typ.name
            END DataType,

        ISNULL(col.collation_name,'') Collation,

        CASE WHEN col.is_nullable = 0 THEN 'NOT NULL ' ELSE '' END + CASE WHEN col.is_identity = 1 THEN 'IDENTITY' ELSE '' END Constraints,

        ISNULL((SELECT 'PK'
                FROM    sys.key_constraints kc INNER JOIN
                        sys.tables tb ON tb.object_id = kc.parent_object_id INNER JOIN
                        sys.indexes si ON si.name = kc.name INNER JOIN
                        sys.index_columns sic ON sic.index_id = si.index_id AND sic.object_id = si.object_id
                WHERE kc.type = 'PK'
                  AND tb.name = @tname
                  AND sic.column_id = col.column_id),'') PK,

        ISNULL((SELECT (SELECT name FROM sys.tables st WHERE st.object_id = fkc.referenced_object_id)
                FROM    sys.foreign_key_columns fkc INNER JOIN
                        sys.columns c ON c.column_id = fkc.parent_column_id AND fkc.parent_object_id = c.object_id INNER JOIN
                        sys.tables t ON t.object_id = c.object_id
                WHERE t.name = tab.name
                  AND c.name = col.name),'') FK,

        ISNULL((SELECT value
                FROM sys.extended_properties
                WHERE major_id = tab.object_id
                  AND minor_id = col.column_id),'') Comment

FROM sys.columns col INNER JOIN
     sys.tables tab ON tab.object_id = col.object_id INNER JOIN
     sys.types typ ON typ.system_type_id = col.system_type_id
WHERE tab.name = @tname
  AND typ.name != 'sysname'
ORDER BY col.column_id;

【讨论】:

【参考方案17】:

使用

SELECT COL_LENGTH('tablename', 'colname')

其他解决方案都不适合我。

【讨论】:

这需要知道其他列是什么。还有一个错别字 修正了报价。【参考方案18】:
SELECT C.COLUMN_NAME, C.IS_NULLABLE, C.DATA_TYPE, TC.CONSTRAINT_TYPE, C.COLUMN_DEFAULT
    FROM INFORMATION_SCHEMA.COLUMNS AS C
    FULL JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE AS CC ON C.COLUMN_NAME = CC.COLUMN_NAME 
    FULL JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS TC ON CC.CONSTRAINT_NAME = TC.CONSTRAINT_NAME
WHERE C.TABLE_NAME = '<Table Name>';

Sample Output

【讨论】:

欢迎来到 SO!当你回答一个问题时,试着解释一下你的答案。就您而言,还有 16 个回复,因此您应该公开您的答案的优缺点【参考方案19】:

如果您使用的是 Brent Ozar 团队的 FirstResponderKit,您也可以运行此查询:

exec sp_blitzindex @tablename='MyTable'

它将返回有关表的所有信息:

索引及其使用统计信息(读取、写入、锁定等)、空间 二手和其他 缺少索引 列 外键 统计内容

当然,它不是一个系统,也不是像 sp_helpsp_columns 这样通用的 stp,但它会返回有关您的表的所有可能信息,我认为值得在您的环境并在此处提及。

【讨论】:

【参考方案20】:

只需双击表名并按 Alt+F1

【讨论】:

【参考方案21】:

我试过了,它对我有用

exec sp_help TABLE_NAME

【讨论】:

【参考方案22】:
CREATE PROCEDURE [dbo].[describe] 
( 
@SearchStr nvarchar(max) 
) 
AS 
BEGIN 
SELECT  
    CONCAT([COLUMN_NAME],' ',[DATA_TYPE],' ',[CHARACTER_MAXIMUM_LENGTH],' ', 
    (SELECT CASE [IS_NULLABLE] WHEN 'NO' THEN 'NOT NULL' ELSE 'NULL' END),
    (SELECT CASE WHEN [COLUMN_DEFAULT] IS NULL THEN '' ELSE CONCAT(' DEFAULT ',[COLUMN_DEFAULT]) END)
    ) AS DESCRIPTION
    FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME LIKE @SearchStr
END 

【讨论】:

【参考方案23】:

下面的查询将提供与 python、Pandas 库中的 info() 函数类似的输出。

USE [Database_Name]

IF OBJECT_ID('tempdo.dob.#primary_key', 'U') IS NOT NULL DROP TABLE #primary_key

SELECT 
CONS_T.TABLE_CATALOG,
CONS_T.TABLE_SCHEMA,
CONS_T.TABLE_NAME,
CONS_C.COLUMN_NAME,
CONS_T.CONSTRAINT_TYPE,
CONS_T.CONSTRAINT_NAME
INTO  #primary_key
FROM
INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS CONS_T 
JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE AS CONS_C ON CONS_C.CONSTRAINT_NAME= CONS_T.CONSTRAINT_NAME


SELECT
SMA.name AS [Schema Name],
ST.name AS [Table Name],
SC.column_id AS [Column Order],
SC.name AS [Column Name],
PKT.CONSTRAINT_TYPE, 
PKT.CONSTRAINT_NAME, 
SC.system_type_id,
STP.name AS [Data Type],
SC.max_length,
SC.precision, 
SC.scale, 
SC.is_nullable, 
SC.is_masked
FROM sys.tables  AS ST
JOIN sys.schemas AS SMA ON SMA.schema_id = ST.schema_id
JOIN sys.columns AS SC ON SC.object_id = ST.object_id 
JOIN sys.types AS STP ON STP.system_type_id = SC.system_type_id
LEFT JOIN #primary_key AS PKT ON PKT.TABLE_SCHEMA = SMA.name
                                 AND PKT.TABLE_NAME = ST.name
                                 AND PKT.COLUMN_NAME = SC.name
ORDER BY ST.name ASC, SMA.name ASC

【讨论】:

【参考方案24】:

select * from sysobjects where name='TABLENAME'

【讨论】:

以上是关于SQL Server 中“描述表”的等价物是啥?的主要内容,如果未能解决你的问题,请参考以下文章

C# long 类型的等效 SQL Server 类型是啥?

sql中GROUPING的作用是啥

ASP.NET Core 中的 Server.MapPath 等价物是啥?

在 Visual Studio C++ 中,用于组织控件的 delphi TPanel 或 alTop、alClient 的等价物是啥?

在 C# 中 if (condition) then apply function that return same type 的功能等价物是啥?

sql中firebase的集合和文档的等价物是啥?