Skip to content
Advertisement

SSIS: generate create table DDL programmatically

Is there source or library somewhere that would help me generate DDL on the fly?

I have a few hundred remote databases that I need to copy to the local server. Upgrade procedure in the field is to create a new database. Locally, I do the same.

So, instead of generating the DDL for all the different DB versions in the field, I’d like to read the DDL from the source tables and create an identical table locally.

Is there such a lib or source?

Advertisement

Answer

Actually, you will discover that your can do this yourself and you will learn something in the process. I use this on several databases I maintain. I create a view that makes it easy to look use DDL style info.

And I use the following to “show the table structure”

You can use other info in InformationSchemas if you also need to generate Foreign keys, etc. It is a bit complex and I never bothered to flesh out everything necessary to generate the DDL, but you should get the right idea. Of course, I would not bother with rolling your own if you can use what has already been suggested.

Added comment — I did not give you an exact answer, but glad to help. You will need to generate lots of dynamic string manipulation to make this work — varchar(max) helps. I will point out the TSQL is not the language of choice for this kind of project. Personally, if I had to generate full table DDL’s I might be tempted to write this as a CLR proc and do the heavy string manipulation in C#. If this makes sense to you, I would still debug the process outside of SQL server (e.g. a form project for testing and dinking around). Just remember that CLR procs are Net 2.0 framework.

You can absolutely make a stored proc that returns a set of results, i.e., 1 for the table columns, 1 for the foreign keys, etc. then consume that set of results in C# and built the DDL statements. in C# code.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement