How to get Create script of an existing table

  • Hi,

    Is there any function which gives the table creation script.

    something like below

    create table <table>

    (

    id number,

    name varchar(100))

    Please help me.

    I have found some approaches in google, where we need to create our function to achieve this

    http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=120447

    http://sqlblogcasts.com/blogs/madhivanan/archive/2007/08/27/generate-sql-script.aspx

    Is there any predefined function which got introduced by MS SQL server itself

    Thanks in advance

  • Easiest way is

    In SSMS, expand database, expand tables, right click table, expand script table as, expand create to, click new query editor window.

  • if you've gotta do it via TSQL, instead of stepping out via SMO to get it , i've spent a lot of time refining a few stored procedures to do that via TSQL.

    sp_GetDDLa_Latest.txt returns a multi row table with the definitiion of any table, temp table, proc function or trigger.

    sp_GetDDL_Latest.txt returns a single varchar(max) with the definitiion of any table, temp table, proc function or trigger.

    both methods serve differnet functions; the first makes it REALLY readable.

    the second is great when you want to stuff a CREATE TABLe definition into a field when auditing your scripts.

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply