Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

How to get Create script of an existing table Expand / Collapse
Author
Message
Posted Wednesday, July 18, 2012 6:06 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, June 17, 2014 11:52 AM
Points: 26, Visits: 24
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
Post #1331398
Posted Wednesday, July 18, 2012 6:09 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: 2 days ago @ 8:55 AM
Points: 5,216, Visits: 5,064
Easiest way is

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




Want an answer fast? Try here
How to post data/code for the best help - Jeff Moden
Need a string splitter, try this - Jeff Moden
How to post performance problems - Gail Shaw
CrossTabs-Part1 & Part2 - Jeff Moden
SQL Server Backup, Integrity Check, and Index and Statistics Maintenance - Ola Hallengren
Managing Transaction Logs - Gail Shaw
Troubleshooting SQL Server: A Guide for the Accidental DBA - Jonathan Kehayias and Ted Krueger

Post #1331399
Posted Wednesday, July 18, 2012 6:18 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 12:53 PM
Points: 12,903, Visits: 31,966
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

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #1331408
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse