T-SQL query to show table definition?

后端 未结 16 1254
爱一瞬间的悲伤
爱一瞬间的悲伤 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: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 = ""
    $database = ""
    $table = ""
    $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 = ""
    $user = ""
    $password = ""
    $database = ""
    $table = "
    " $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"}

    提交回复
    热议问题