Sunday, February 19, 2012

Copy table structure to a new table

Hello everyone,

I have a local MSSQL server (I guess it's called MSDE), with some tables that I would like to use as a template for a set of new tables. I would simply like a Stored Procedure that takes these 3 tables, makes a copy of their structure (not data, since they will be empty), and name them by using a parameter given to the SP. I have made something that I thought would work, but after testing it a bit more, it seems to forget default values for the fields, which is of course not good enough :). I hope that someone can tell me how to duplicate these 3 tables, including every detail for the fields!

This is not really a trivial task in T-SQL. You can probablycobble something together using the INFORMATION_SCHEMA views and someheavy dynamic SQL, but it may be easier to simply hardcode the tables'DDL into your stored procedure.
Or, you could use DMO to help you. See if this helps:
http://www.nigelrivett.net/DMO/DMOScripting.html

No comments:

Post a Comment