Monday, March 19, 2012

Move table

How can i move tables from one database to another without losing any of the PK, FK or the identity column constraints?

I currently use wizard and thought maybe you can do this with some Tsql code...

Is it possible?

Thanks for any help

You can do the following in TSQL:

1. Use SELECT INTO to create the tables with data (identity property of column will be copied over including the NULLability constraints)

2. Add the constraints using metadata from INFORMATION_SCHEMA.TABLE_CONSTRAINTS, CHECK_CONSTRAINTS etc.

3. Create defaults, computed columns using syscomments information

In SQL Server 2005, you can use different set of catalog views to get the metadata. Since this is lot of work, you are probably better off using SSIS/DTS Transfer task.

No comments:

Post a Comment