Re: Quicked way to create a copy of a table with all default values in

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Toby wrote:
I know that using something like this is the quickest way to create a
table: SELECT * INTO newtablename FROM oldtablename WHERE 1>2

However I need for the new table to have all the default values that
have been declared in the old table and the select statement above
does not do that. How can I keep the default values without having
to run an alter statement that adds them back in.

I'm not really sure that is the quicest method ... maybe for the
developer, but probably not for the database engine.

The reason this might be the "quickest" is because there is no attempt
made to determine and duplicate the constraints, indexes and defaults in
the source table.
You need to use ALTER TABLE to add constraints and indexes to the new
table. There is no getting around that.

The good news is that you can use SSMS to generate the script needed to
create a table and all its constraints. So do that, change the table and
associated object names to avoid duplication, and you're good to go. If
you need to figure out how to generate this script programmatically,
turn on SQL Profiler while SSMS generates the script to see what you
have to do in your program code.

--
HTH,
Bob Barrows


.



Relevant Pages

  • Re: Add columns to table, but in an order
    ... in the order that SQL Server mentions them, with no way for the end user ... existing constraints and recreate them when the new table is in place. ... be very critical of the script ... Make sure to test the conversion on a test server first. ...
    (microsoft.public.sqlserver.server)
  • Re: Turning Off Foreign Keys during DB coping
    ... will disable and then reenable the constraints. ... > Do you suggest me to write the script for each table to disable keys? ... >> CREATE TABLE Test2 ...
    (microsoft.public.sqlserver.dts)
  • Re: cannot ad hoc on system tables on SQL server 2005
    ... identical so the constraints are not identical,this is the reason i'm ... If you choose to script all tables and set the option to script constraints as well, Management Studio will put all DEFAULT, CHECK and FOREIGN KEY constraints at the end of the script. ... Changing schemas will block data operations, which can be a problem for large tables. ... Creating a new database and importing the data is a more attractive option if you're changing many fields. ...
    (microsoft.public.sqlserver.programming)
  • Re: cannot ad hoc on system tables on SQL server 2005
    ... identical so the constraints are not identical,this is the reason i'm ... You can lighten your load a bit by using Management Studio to script the ... Changing schemas will block data operations, ... Creating a new database and importing the data (using Integration Services ...
    (microsoft.public.sqlserver.programming)
  • Re: Database Publishing
    ... Tools that are free if you are using SQL-Server Express: ... Other free tools that can be useful: ... Some of those free tools to work with SSMS (not necessarily to compare two ... How I will run generated script and will I get a permission from admistrator? ...
    (microsoft.public.sqlserver.programming)