获取基本的 SQL Server 表结构信息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14819994/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 13:39:07  来源:igfitidea点击:

get basic SQL Server table structure information

sqlsql-server

提问by 1252748

I can get the number of columns in an SQL Server database with this:

我可以通过以下方式获取 SQL Server 数据库中的列数:

SELECT COUNT(*)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'Address'

But is there any way (for an unknown number of columns) I can get the name and datatype and length of each column?

但是有什么方法(对于未知数量的列)我可以获得每列的名称和数据类型以及长度?

回答by Taryn

Instead of using count(*)you can SELECT *and you will return all of the details that you want including data_type:

而不是使用count(*)您可以SELECT *,您将返回您想要的所有详细信息,包括data_type

SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'Address'

MSDN Docs on INFORMATION_SCHEMA.COLUMNS

MSDN 上的文档 INFORMATION_SCHEMA.COLUMNS

回答by Roarster

sp_helpwill give you a whole bunch of information about a table including the columns, keys and constraints. For example, running

sp_help将为您提供有关表的大量信息,包括列、键和约束。例如,运行

exec sp_help 'Address' 

will give you information about Address.

将为您提供有关地址的信息。

回答by Kprof

Name and datatype:

名称和数据类型:

USE OurDatabaseName
GO

SELECT 
    sc.name AS [Columne Name], 
    st1.name AS [User Type],
    st2.name AS [Base Type]
FROM dbo.syscolumns sc
    INNER JOIN dbo.systypes st1 ON st1.xusertype = sc.xusertype
    INNER JOIN dbo.systypes st2 ON st2.xusertype = sc.xtype
-- STEP TWO: Change OurTableName to the table name
WHERE sc.id = OBJECT_ID('OurTableName')
ORDER BY sc.colid

Or:

或者:

SELECT COLUMN_NAME AS ColumnName, DATA_TYPE AS DataType, CHARACTER_MAXIMUM_LENGTH AS CharacterLength
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'OurTableName'

回答by Abu Zafor

Write the table name in the query editor select the name and press Alt+F1 and it will bring all the information of the table.

在查询编辑器中写入表名选择名称并按Alt+F1,它将带来该表的所有信息。

回答by satya

For total columns information use below syntax : Use "DBName" go Exec SP_Columns "TableName"

对于总列信息,请使用以下语法:使用“DBName” go Exec SP_Columns “TableName”

For total table information use below syntax : Use "DBName" go Exec SP_help "Table Name"

对于总表信息,请使用以下语法:使用“DBName” go Exec SP_help“Table Name”

回答by Shubham Bhangale

You could use these functions:

您可以使用这些功能:

sp_help TableName
sp_helptext ProcedureName