0

What i m looking for is to duplicate a table.

The complicate part is to get the table script (which includes also the relationships) with a query and not using the Script Table as - CREATE To.

Any ideas?

Error 1004
  • 7,877
  • 3
  • 23
  • 46

1 Answers1

0

This will suffice for columns, datatypes and nulability:

select top 0 *
into destination_table
from source_table

For keys, constraints, indexes, triggers and other stuff you'll still have to use information_schema/sys tables

George Menoutis
  • 6,894
  • 3
  • 19
  • 43
  • There are many use cases where this will fail – Hogan Mar 26 '21 at 13:55
  • Interesting. The only case I have in my mind to avoid this is creating identical `rowversion`s in case the `top` number is positive. Could you elaborate? – George Menoutis Mar 26 '21 at 14:02
  • it was a while ago for a specific examples --- as I remember it you could have the default varchar sizes set to one thing and a select would always use the default size in creating the table unless there was data that was larger in the select – Hogan Mar 26 '21 at 14:34