Home All Groups Group Topic Archive Search About

How to generate a table script in T-SQL?

Author
8 Jun 2006 3:27 PM
Edmund
Hello,

Is there a way to generate a CREATE TABLE script from an existing table in
T-SQL?  For stored procedures I can use sp_helptext in Query Analyzer to get
the procedure definition, make changes, test and then issue the ALTER.  I
would like to get the table script in Query Analyzer as well.  I know you
can do it through the Enterprise Manager.  Can it be done in Query Analyzer?

Thanks,

Edmund

Author
8 Jun 2006 3:40 PM
Martin C K Poon
To enable the Object Browser: Tools | Object Browser | Show/hide
Within the Object Browser, highlight your desired user table, and right
click | Script object to new window as | Create

--
Martin C K Poon
Senior Analyst Programmer
====================================
Show quote
"Edmund" <die_spambot@hatespam.com> ¦b¶l¥ó
news:uOS$cAxiGHA.1640@TK2MSFTNGP02.phx.gbl ¤¤¼¶¼g...
> Hello,
>
> Is there a way to generate a CREATE TABLE script from an existing table in
> T-SQL?  For stored procedures I can use sp_helptext in Query Analyzer to
get
> the procedure definition, make changes, test and then issue the ALTER.  I
> would like to get the table script in Query Analyzer as well.  I know you
> can do it through the Enterprise Manager.  Can it be done in Query
Analyzer?
>
> Thanks,
>
> Edmund
>
>
Author
8 Jun 2006 3:49 PM
Narayana Vyas Kondreddi
You can instantiate the SQL DMO objects from within T-SQL using sp_OACreate
and other related stored procedures. Search the net for examples on these
procedures.
--
HTH,
Vyas, MVP (SQL Server)
SQL Server Articles and Code Samples @ http://vyaskn.tripod.com/


"Edmund" <die_spambot@hatespam.com> wrote in message
news:uOS$cAxiGHA.1640@TK2MSFTNGP02.phx.gbl...
Hello,

Is there a way to generate a CREATE TABLE script from an existing table in
T-SQL?  For stored procedures I can use sp_helptext in Query Analyzer to get
the procedure definition, make changes, test and then issue the ALTER.  I
would like to get the table script in Query Analyzer as well.  I know you
can do it through the Enterprise Manager.  Can it be done in Query Analyzer?

Thanks,

Edmund
Author
8 Jun 2006 4:02 PM
Edmund
Thanks, both these suggestions are great!


Show quote
"Narayana Vyas Kondreddi" <answer***@hotmail.com> wrote in message
news:OrpqvMxiGHA.4716@TK2MSFTNGP03.phx.gbl...
> You can instantiate the SQL DMO objects from within T-SQL using
> sp_OACreate
> and other related stored procedures. Search the net for examples on these
> procedures.
> --
> HTH,
> Vyas, MVP (SQL Server)
> SQL Server Articles and Code Samples @ http://vyaskn.tripod.com/
>
>
> "Edmund" <die_spambot@hatespam.com> wrote in message
> news:uOS$cAxiGHA.1640@TK2MSFTNGP02.phx.gbl...
> Hello,
>
> Is there a way to generate a CREATE TABLE script from an existing table in
> T-SQL?  For stored procedures I can use sp_helptext in Query Analyzer to
> get
> the procedure definition, make changes, test and then issue the ALTER.  I
> would like to get the table script in Query Analyzer as well.  I know you
> can do it through the Enterprise Manager.  Can it be done in Query
> Analyzer?
>
> Thanks,
>
> Edmund
>
>
>
Author
8 Jun 2006 4:40 PM
Greg Larsen
Here is an article that should provide you with some examples of how to
script out out objects:

http://www.dbazine.com/sql/sql-articles/larsen4

Show quote
"Edmund" wrote:

> Hello,
>
> Is there a way to generate a CREATE TABLE script from an existing table in
> T-SQL?  For stored procedures I can use sp_helptext in Query Analyzer to get
> the procedure definition, make changes, test and then issue the ALTER.  I
> would like to get the table script in Query Analyzer as well.  I know you
> can do it through the Enterprise Manager.  Can it be done in Query Analyzer?
>
> Thanks,
>
> Edmund
>
>
>
Author
10 Jun 2006 1:10 AM
Farmer
You can also use

select *
from information_schema.columns and others and just create concatenation
script from field values to get your final script.
+ CHAR(13) + CHAR(10) , CHAR(9)  can be used to format it nicely.
I use this very often and can get output exactly to my liking.


Show quote
"Edmund" <die_spambot@hatespam.com> wrote in message
news:uOS$cAxiGHA.1640@TK2MSFTNGP02.phx.gbl...
> Hello,
>
> Is there a way to generate a CREATE TABLE script from an existing table in
> T-SQL?  For stored procedures I can use sp_helptext in Query Analyzer to
> get the procedure definition, make changes, test and then issue the ALTER.
> I would like to get the table script in Query Analyzer as well.  I know
> you can do it through the Enterprise Manager.  Can it be done in Query
> Analyzer?
>
> Thanks,
>
> Edmund
>

AddThis Social Bookmark Button