How Do I Use a Query Tag?
Use the Query Tag to retrieve a data set from your data source, then store that result in a newly created variable. The name of this variable can subsequently be used in other Tags using the "${VariableName}" syntax.
The Query Tag holds a single row or node. If the result of the Query Tag's select statement returns multiple rows or nodes, only the first one is accessible.
In this article we'll go through a step-by-step example of using a Query Tag.
For more details about Query Tags, see the Query Tag Reference.
Open a new Word document, then connect to our public mssql.windward.net data source. If you're unfamiliar with connecting to a SQL Server data source, see How Do I Connect to a Microsoft SQL Server Data Source?
Click on the Tags button in the AutoTag ribbon, then select the Query Tag:
Double-click on the Query Tag to bring up the Tag Editor. Expand the Tables node in the Data Pane, then drag and drop the Employees table from the Data Pane onto the Query Pane:
Then click on the Properties tab, give the Tag a nickname, and save the Tag:
To refine the Query Tag select statement to return a single row, click on the Wizard button in the AutoTag ribbon:
For this example we'll narrow the select statement to return the employee with employee ID 5.
For more details about using the SQL Wizard, see the SQL Wizard Reference article.
Select the Query Tag and click on the Preview button to see the row returned by the Query Tag's new select statement:
Now we'll create some Out Tags to display the data held in the Query Tag variable. Let's display the first name, last name and birth date of the employee with ID 5.
Enter some titles into your Word template like this:
Insert an Out Tag after each title like this:
We need to create a select statement for each Out Tag to return the data we wish to display.
- Select the first Out Tag
- Click on the [Data Tree] button in the AutoTag ribbon. At the top of the Data Tree window is a graphical view of the table columns returned by the Query Tag (variable ${varName1}).
- Select the FirstName column
The Out Tag will now output the first name of the employee whose ID is 5.
Repeat this process for the other Tags. In addition, set the type of the [BirthDate] Out Tag to DATE by selecting that Tag and using the Type select list on the AutoTag ribbon:
Now save the template, generate output, and your output will look like: