Create an object component template
Object component templates extract schema objects (tables, views, stored procedures, functions, triggers) from the live database and version them in your workspace. This guide shows the minimum template, then the options you'll reach for.
Before you start
- You've created a workspace and connected it to a database.
- You know which kind of object you're generating for: table, view, procedure, function, trigger, or DDL trigger.
- You've decided whether you want one component per object (simple, explicit) or one template that discovers objects (see Generate components in bulk).
The minimum template
Create an XML file under your workspace's templates/ folder:
<?xml version="1.0" encoding="utf-8"?>
<ObjectComponent Category="functions" Name="GetAccountBalance" Schema="dbo">
<SchemaObject Name="GetAccountBalance" Type="Function"/>
</ObjectComponent>
Three things going on:
<ObjectComponent>. The root.Categorygroups this in the UI;Nameis the component filename.<SchemaObject>. The object being extracted.Nameis the object's name in the database;Typetells DataStar how to render the CREATE script.Schemaon the root. The database schema/owner to extract from.
Save the file, refresh the workspace, and DataStar will offer to extract dbo.GetAccountBalance as a component.
The Type attribute
<SchemaObject> supports these types:
Type | What gets extracted |
|---|---|
Table | CREATE TABLE with columns, constraints, indexes (optionally), triggers |
View | CREATE VIEW with the full definition |
Procedure | CREATE PROCEDURE with body |
Function | CREATE FUNCTION (scalar or table-valued) |
Trigger | CREATE TRIGGER (DML trigger) |
DdlTrigger | CREATE TRIGGER ... ON DATABASE ... (DDL trigger) |
Pick one per <SchemaObject>. A single template can have several <SchemaObject> elements if you want to group related objects, but most templates have just one.
Common options
Options tune what's included in the generated script. Add them as <Option> elements before <SchemaObject>:
<ObjectComponent Category="functions" Name="GetAccountBalance" Schema="dbo">
<Option Name="Permissions" Value="True"/>
<Option Name="CreateOrAlter" Value="True"/>
<SchemaObject Name="GetAccountBalance" Type="Function"/>
</ObjectComponent>
| Option | Effect |
|---|---|
Permissions | Include GRANT / DENY statements for the object |
Indexes | Include index definitions (tables and indexed views) |
CreateOrAlter | Generate CREATE OR ALTER instead of CREATE (SQL Server 2016+) |
LineFormatting | Control blank-line handling in the extracted body |
ServerVersion | Pin generated syntax to a specific target (e.g. 11.0 for SQL Server 2012) |
Deployment ordering
If your object depends on other objects (a view on a table, a procedure on a function), control deployment order with <Deployment Weight>:
<ObjectComponent Category="views" Name="AccountSummary" Schema="dbo">
<SchemaObject Name="AccountSummary" Type="View"/>
<Deployment Weight="300"/>
</ObjectComponent>
Lower weights deploy first. A conventional scheme:
| Weight | Object kind |
|---|---|
| 100 | Tables |
| 200 | Functions |
| 280 | Stored procedures |
| 300 | Views |
| 400 | Triggers |
| 500 | Data components |
Pick once, apply consistently across templates.
One template, many components
For anything where you'd otherwise end up with 100 near-identical templates (every view, every stored procedure), use a <DynamicQuery> to discover targets from the database itself:
<ObjectComponent Category="views" Name="${filename}" Schema="${schema}">
<SchemaObject Name="${name}" Type="View"/>
<DynamicQuery><![CDATA[
SELECT TABLE_SCHEMA AS '${schema}',
'[' + LOWER(TABLE_SCHEMA) + '].[' + LOWER(TABLE_NAME) + ']' AS '${filename}',
TABLE_NAME AS '${name}'
FROM INFORMATION_SCHEMA.VIEWS
]]></DynamicQuery>
<Deployment Weight="300"/>
</ObjectComponent>
One template generates a component per view in the database. See Generate components in bulk for the full pattern.
Verifying the output
Once saved, extract a component and inspect the generated script:
- It should be a complete
CREATE …(orCREATE OR ALTER) statement, nothing missing. - Permissions, if enabled, appear at the end.
- Index definitions (for tables) appear after the column list.
If the script looks truncated or out of order, try the options above; most gaps are a missing Permissions="True" or a ServerVersion that's too old for the feature being emitted.
See also
- Data vs object components: pick the right template type.
- Generate components in bulk: one template, many components.
- Include static scripts: for pre/post DDL that doesn't fit the object model.