T-SQL query to show table definition?

后端 未结 16 1207
爱一瞬间的悲伤
爱一瞬间的悲伤 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:31

    Try the sp_help stored procedure.

    sp_help <>

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

    Use this little Windows command-line app that gets the CREATE TABLE script (with constraints) for any table. I've written it in C#. Just compile it and carry it on a memory stick. Perhaps someone can port it to 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);
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-02 06:34

    "Note that I really do want the DDL that defines the table."

    use pg_dump:

    pg_dump -s -t tablename dbname
    

    It gives you the table definition (-s is schema only, no data) of a certain table ( -t tablename ) in database "dbname" in plain SQL. Additionally you will get sequence, primary key and constraint info. The output you can -maybe after checking and editing according to your needs- be fed again into (the same or another) Postgres database:

    pg_dump -s -t tablename dbname1  > /tmp/foo.sql
    psql -e dbname2 < /tmp/foo.sql
    

    This is for UNIX/Linux, but I'm sure, that a pg_dump also exists for Windows.

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

    sp_help 'YourTableName'
    
    0 讨论(0)
  • 2020-12-02 06:39

    Simply type the table name and select it and press ATL + F1

    Say your table name is Customer then open a new query window, type and select the table name and press ALT + F1

    It will show the complete definition of table.

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

    Visit http://www.stormrage.com/SQLStuff/sp_GetDDL_Latest.txt.

    You will find the code of sp_getddl procedure for SQL Server. The purpose of the procedure is script any table, temp table or object.

    USAGE:

    exec sp_GetDDL GMACT
    

    or

    exec sp_GetDDL 'bob.example'
    

    or

    exec sp_GetDDL '[schemaname].[tablename]'
    

    or

    exec sp_GetDDL #temp
    

    I tested it on SQL Server 2012, and it does an excellent job.

    I'm not the author of the procedure. Any improvement you make to it send to Lowell Izaguirre (scripts@stormrage.com).

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