Create the template in the Template Editor
This time you use the Template Editor in Visual Studio to create the template, which also shows you how much it speeds up template development.
-
In Visual Studio, select File > New > File.
Tip
You can also open a new or existing Visual Studio project and add a new item from Solution Explorer.
-
Select the CodeSmith Generator category. The dialog shows only the CodeSmith Generator item templates.
-
Select Generator Template (Visual Basic), then click Open.
Generator creates a template with some boilerplate code that shows how the parts of a template fit together.
-
Change the CodeTemplate directive to:
<%@ CodeTemplate Language="VB" TargetLanguage="T-SQL" Description="Creates a stored procedure that returns a table's rows as JSON." %>TargetLanguagesets how the template's static content is syntax highlighted.Descriptionprovides a tooltip for the template. -
Replace the rest of the boilerplate code with the T-SQL you want to generate.
You now have a starting point: a template that produces completely static SQL.
<%@ CodeTemplate Language="VB" TargetLanguage="T-SQL" Description="Creates a stored procedure that returns a table's rows as JSON." %>
CREATE OR ALTER PROCEDURE Person.GetAddressTypeJson
WITH EXECUTE AS CALLER
AS
BEGIN
SET NOCOUNT ON;
SET TRANSACTION ISOLATION LEVEL READ COMMITTED;
SELECT
[AddressTypeID],
[Name],
[rowguid],
[ModifiedDate]
FROM
[Person].[AddressType]
FOR JSON PATH, ROOT('AddressType');
END
GO
Of course, you don't want a static template. Next, you start making the content dynamic.