Writing a template with database metadata
One of the most common uses for code generation is building code from a database schema. CodeSmith Generator supports this with SchemaExplorer, a library of types for working with database metadata, plus designers that let you pick database objects from the property sheet.
In this tutorial, you combine SchemaExplorer with template code to generate a stored procedure that returns any table you choose as JSON. You write the template in Visual Basic in the Template Editor in Visual Studio.
Steps
- The scenario: the T-SQL you'll generate and why it suits a template.
- The desired SQL statements: start from an example of the output.
- Create the template in the Template Editor.
- Set up enumerated properties for values with a fixed list of choices.
- Set up a SQL property so the user can pick a table.
- Write the database code that lists the table's columns.
- Test the final result.
To learn more about SchemaExplorer, see Using SchemaExplorer.