什么是查询将向我显示完整定义,包括SQL Server表的索引和键?我想要一个纯粹的查询 - 并且知道SQL Studio可以给我这个,但我经常在"狂野"的计算机上,只有最简单的应用程序,我没有权利安装工作室.但SQLCMD始终是一种选择.
更新:我尝试过sp_help,但只生成一条显示Name,Owner,Type和Created_Datetime的记录.sp_help还有其他我缺少的东西吗?
这就是我所说的:
sp_help机场
请注意,我确实需要定义表的DDL.
Ant*_*ull 114
没有简单的方法来返回DDL.但是,您可以从信息架构视图和系统视图中获取大部分详细信息.
SELECT ORDINAL_POSITION, COLUMN_NAME, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH
, IS_NULLABLE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'Customers'
SELECT CONSTRAINT_NAME
FROM INFORMATION_SCHEMA.CONSTRAINT_TABLE_USAGE
WHERE TABLE_NAME = 'Customers'
SELECT name, type_desc, is_unique, is_primary_key
FROM sys.indexes
WHERE [object_id] = OBJECT_ID('dbo.Customers')
Run Code Online (Sandbox Code Playgroud)
SQL*_*ace 67
你试过sp_help吗?
sp_help 'TableName'
Run Code Online (Sandbox Code Playgroud)
小智 23
访问http://www.stormrage.com/SQLStuff/sp_GetDDL_Latest.txt.
您将找到sp_getddlSQL Server 的过程代码.该过程的目的是脚本任何表,临时表或对象.
用法:
exec sp_GetDDL GMACT
Run Code Online (Sandbox Code Playgroud)
要么
exec sp_GetDDL 'bob.example'
Run Code Online (Sandbox Code Playgroud)
要么
exec sp_GetDDL '[schemaname].[tablename]'
Run Code Online (Sandbox Code Playgroud)
要么
exec sp_GetDDL #temp
Run Code Online (Sandbox Code Playgroud)
我在SQL Server 2012上测试过它,它做得很好.
我不是程序的作者.您对它所做的任何改进都会发送给Lowell Izaguirre(scripts@stormrage.com).
Ant*_*ull 19
使用这个小的Windows命令行应用程序获取CREATE TABLE任何表的脚本(带有约束).我用C#写的.只需将其编译并将其放在记忆棒上即可.也许有人可以把它移植到Powershell.
using System;
using System.Linq;
using Microsoft.SqlServer.Management.Common;
using Microsoft.SqlServer.Management.Smo;
namespace ViewSource
{
public class ViewSource
{
public static void Main(string[] args)
{
if (args.Length != 6)
{
Console.Error.WriteLine("Syntax: ViewSource.exe <server>" +
" <user> <password> <database> <schema> <table>");
}
Script(args[0], args[1], args[2], args[3], args[4], args[5]);
}
private static void Script(string server, string user,
string password, string database, string schema, string table)
{
new Server(new ServerConnection(server, user, password))
.Databases[database]
.Tables[table, schema]
.Script(new ScriptingOptions { SchemaQualify = true,
DriAll = true })
.Cast<string>()
.Select(s => s + "\n" + "GO")
.ToList()
.ForEach(Console.WriteLine);
}
}
}
Run Code Online (Sandbox Code Playgroud)
小智 8
从 SQL 2012 开始,您可以运行以下语句:
Exec sp_describe_first_result_set @tsql= N'Select * from <yourtable>'
Run Code Online (Sandbox Code Playgroud)
如果您输入复杂的选择语句(连接、子选择等),它将为您提供结果集的定义。这非常方便,如果您需要创建一个新表(或临时表)并且您不想手动检查每个字段定义。
这将返回表中定义的列,数据类型和索引:
--List all tables in DB
select * from sysobjects where xtype = 'U'
--Table Definition
sp_help TableName
Run Code Online (Sandbox Code Playgroud)
这将返回表中定义的触发器:
--Triggers in SQL Table
select * from sys.triggers where parent_id = object_id(N'SQLTableName')
Run Code Online (Sandbox Code Playgroud)
我知道这是一个老问题,但正是我想要的。因为我想对一些表进行批处理,所以我为 PowerShell 重写了 Anthony Faull 的 C# 代码。
这是使用集成安全性:
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"}
Run Code Online (Sandbox Code Playgroud)
在这里使用用户名和密码:
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"}
Run Code Online (Sandbox Code Playgroud)
只需键入表名称并选择它,然后按 ATL + F1
假设您的表名称是,Customer然后打开一个新的查询窗口,键入并选择表名称,然后按 ALT + F1
它将显示表的完整定义。