T-SQL query to show table definition?

后端 未结 16 1206
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-02 06:16

What is a query that will show me the full definition, including indexes and keys for a SQL Server table? I want a pure query - and know that SQL Studio can give this to me

相关标签:
16条回答
  • 2020-12-02 06:23

    The easiest and quickest way I can think of would be to use sp_help

    sp_help 'TableName'

    0 讨论(0)
  • 2020-12-02 06:25

    Another way is to execute sp_columns procedure.

    EXEC sys.sp_columns @TABLE_NAME = 'YourTableName'
    
    0 讨论(0)
  • 2020-12-02 06:28

    I know it's an old question, but exactly what I was looking for. Because I want to batch script some tables, I rewrote the C# code from Anthony Faull for PowerShell.

    This one is uses Integrated Security:

    Import-Module sqlps
    
    $serverInstance = "<server>"
    $database = "<database>"
    $table = "<table>"
    $schema = "<schema>"
    
    $options = New-Object -TypeName Microsoft.SqlServer.Management.Smo.ScriptingOptions
    $options.DriAll = $true
    $options.SchemaQualify = $true
    
    $connection = New-Object -TypeName Microsoft.SqlServer.Management.Common.ServerConnection `
        -ArgumentList $serverInstance
    $server = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Server `
        -ArgumentList $connection
    
    $server.Databases.Item($database).Tables.Item($table, $schema).Script($options) `
        | ForEach-Object -Process { $_ + "`nGO"}
    

    And here with username and password:

    Import-Module sqlps
    
    $serverInstance = "<server>"
    $user = "<user>"
    $password = "<pasword>"
    $database = "<database>"
    $table = "<table>"
    $schema = "<schema>"
    
    $options = New-Object -TypeName Microsoft.SqlServer.Management.Smo.ScriptingOptions
    $options.DriAll = $true
    $options.SchemaQualify = $true
    
    $connection = New-Object -TypeName Microsoft.SqlServer.Management.Common.ServerConnection `
        -ArgumentList $serverInstance
    $connection.LoginSecure = $false
    $connection.Login = $user
    $connection.Password = $password
    $server = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Server `
        -ArgumentList $connection
    
    $server.Databases.Item($database).Tables.Item($table, $schema).Script($options) `
        | ForEach-Object -Process { $_ + "`nGO"}
    
    0 讨论(0)
  • 2020-12-02 06:29

    A variation of @Anthony Faull's answer for those using LINQPad:

    new Server(new ServerConnection(this.Connection.DataSource))
        .Databases[this.Connection.Database]
        .Tables["<table>", "dbo"]
        ?.Script(new ScriptingOptions {
            SchemaQualify = true,
            DriAll = true,
        })
    

    You'll need to reference 2 assemblies:

    • Microsoft.SqlServer.ConnectionInfo.dll
    • Microsoft.SqlServer.Smo.dll

    And add namespace references as mentioned in Anthony's snippet.

    0 讨论(0)
  • 2020-12-02 06:30

    As an addition to Barry's answer. The sp_help can also be used by itself to iterate all of the objects in a particular database. You also have sp_helptext for your arsenal, which scripts out programmatic elements, like stored procedures.

    0 讨论(0)
  • 2020-12-02 06:31

    Since SQL 2012 you can run the following statement:

    Exec sp_describe_first_result_set @tsql= N'Select * from <yourtable>'
    

    If you enter a complex select statement (joins, subselects, etc), it will give you the definition of the result set. This is very handy, if you need to create a new table (or temp table) and you don't want to check every single field definition manually.

    https://docs.microsoft.com/en-us/sql/relational-databases/system-stored-procedures/sp-describe-first-result-set-transact-sql

    0 讨论(0)
提交回复
热议问题