Hi folks
Trying to copy a single table from database (contains many tables) to
another database (new DB) with just that one table. Trying to do this with
sql statements if possible?
Any help would be appreciated.
Dan
Danny,
You can create a table with all of its contents by the following command:
SELECT * INTO NewDatabase.dbo.NewTable FROM OldDatabase.dbo.OldTable
However, this will not recreate indexes, constraints, and so forth. So, a
more complete method would be to:
First, in the Object Explorer, right click on the table in question and
Script Table as Create. This will give you a create script wtih indexes,
constraints, etc. (If you do not get all the components you need, check
your scripting options to make sure that nothing you need is turned off.)
Second, use that script to create the table in your new database.
Third, run code like this:
INSERT INTO NewDatabase.dbo.NewTable (Column1, Column2, ... etc )
SELECT Column1, Column2, ... etc
FROM OldDatabase.dbo.OldTable
If you have an IDENTITY column defined, then consider whether you want to
keep the old values (SET IDENTITY_INSERT ON) or generate new values (leave
the identity column our of the column lists).
RLF
"Danny" <lane.dj@.gmail.com> wrote in message
news:uUWaCxHJIHA.4584@.TK2MSFTNGP03.phx.gbl...
> Hi folks
> Trying to copy a single table from database (contains many tables) to
> another database (new DB) with just that one table. Trying to do this with
> sql statements if possible?
> Any help would be appreciated.
> Dan
>
No comments:
Post a Comment