Automation variables are a powerful tool that can greatly extend your scripts. Variables can add flexibility and customization. Variables can be used in almost every Automation activity.
Watch Using Variables in Automation for a video demonstration of this feature.
User-Defined Variables
You can use the Set Variable activity to create user-defined variables. See Use System Automation Activities for more information.
Built-In Variables
Several Automation activities create variables by default. The following built-in variables are created by Automation activities.
RowCount variable— This integer type variable is created by several activities. The value assigned to this variable is the number of rows returned by the query in the activity.
The Automation script creates the following built-in variables.
Where to Use Variables
The following are some examples of where you can use a variable (built-in or user-defined) in an Automation script.
How to Use a Variable
To express the value of a variable, surround the variable name with hash symbols (#), for example #myvar#. The following are some examples of how variables might be used.
#myvar#_report.xlsx
Report_#Dept_Name#.xlsx
#File_1_RCOUNT# > 0
It is important to remember that when a variable is expressed, the variable value is substituted as text in place of the variable name (#myvar#). The only exception is when you use a variable as a bind variable in a SQL statement.
To use a variable as a bind variable in a SQL statement, use the following syntax format (regardless of what the address_id column type is):
SELECT * FROM contact WHERE address_id = :myvar
You can also use the Automation variable here as text substitution, but in this case single quotes might be required (because it is a text substitution) depending on the column type in the WHERE clause.
For example, if last_name is a character column, use the following syntax (single quotes are required):
SELECT * FROM contact WHERE last_name = ‘#myvar#’
If address_id is a non-character column, use the following syntax:
SELECT * FROM contact WHERE address_id = #myvar#
How to Use a String Variable in a Comparison Expression
When comparing a string variable to a string value in an expression, always surround the variable name and the string value with single quotes, for example:
'#myvar#' = 'Dog'
About SQL Type Variables
SQL type variables are created in two ways:
If you ask just for the variable value of a SQL type variable, Toad returns the value from the first row and column of the result set. However, you can filter by column using this syntax:
<variable name>.<column name>.
For example, Region.Region_Name returns the first value (first row) in the column Region_Name.
Dataset/SQL Variable - Special Format for Filtering
You can use the following syntax format to filter data from the dataset variable (or any SQL type variable):
<dataset variable name>.<filter parameter>
Example: Loop_data_1_SQL.region_id, where region_id is one column in the dataset variable
For example, use this format in a SQL statement (in a Select to File activity) to select and export data based on each region_id value in the dataset variable. The statement is executed with each loop of the Dataset activity.
SELECT * FROM address WHERE address.region_id = :Loop_data_1_SQL.region_id
SELECT * FROM address WHERE address.region_id = #Loop_data_1_SQL.region_id#
Bind Variables
You can store global bind variable/value combinations in Toad
In Automation scripts, a common use case is to create a variable to be used later in the script in a SQL statement (such as in the Select to File activity). When creating a variable for a date value, there are two variable types to choose from in the Set Variable activity: String or DateTime. The type you choose depends on how the value is to be used in the SQL.
How to Define a Date Variable
In the Set Variable Activity | ||
---|---|---|
Example SQL |
Variable Type |
Valid Values |
SELECT * FROM contact |
String | '2017-7-7' |
SELECT * FROM contact |
DateTime |
To_date('2017-07-07', 'YYYY-MM-DD') '2017-7-7' |
Using a String. For a String variable type, surround the date value in single quotes. This ensures that date delimiters, such as the dash (-) or forward slash (/), do not cause Toad to interpret the value as an expression.
‘2017-7-7’ is treated as a string
2017-7-7 is evaluated to 2003
When you are building an Automation script that uses a variable, it is important to understand the variable's scope of visibility. This section describes the concept of variable scope in Automation scripts.
Term | Definition |
---|---|
Container |
Let's define a container as any one of the following:
|
Levels |
Suppose that a script is composed of nested containers. Each container is a different level with respect to variable scope. Level 1—The outermost container is the script itself. The script is comprised of activities. The script and its activities in the main workflow comprise the first level. Level 2—Some activities can contain other activities. "Container activities" (such as the Loop Dataset) are at the second level. Level 2 is nested below level 1. Level 3—Container activities can contain branches. A branch within a container activity is the third level. Level 3 is nested below level 2. And so on, until we get to the innermost level. |
Container Activity | A container activity is an activity that contains other activities. The Loop Dataset, Group, If Condition, While, and Parallel activities are container activities. |
Set Variable Activity | Use this activity to define a variable. |
Set Variable Value Activity | Use this activity to assign a new value to a previously-defined variable. |
Basically, the scope of a variable includes all activities subsequent to the variable-defining activity AND either at or nested below the level at which the variable is defined.
The following are the rules for variable scope.
Reassigning a variable value—You can reassign the value of a variable (except a SQL type variable) at any point within the variable's scope. This task is accomplished using the Set Variable Value activity. See Use System Automation Activities for more information.
Once a variable value is reassigned, the new value applies to activities subsequent to the original Set Variable activity AND either at the same level or nested below the level of the original Set Variable activity.
Example: You can use the Set Variable Value to turn a previously-defined variable into a counter (var=var+1) in the Loop Dataset activity.
Define a new variable with the same name—You can redefine a new variable with the same name, but only within an activity that is nested one or more levels below the variable-defining activity. For example, if you defined a variable in the main workflow of your script (first level), you can define a new variable with the same name within a container activity (second level). The scope of this variable value is restricted to that container activity.
You cannot define a new variable with the same name at the same level as the original.
Additional Examples to Illustrate Scope
You can execute automation scripts on demand from the Automation window or the Project Manager. You can also schedule scripts to run on a recurring basis at a specific time. For example, schedule a script that performs long-running tasks to run when database activity is light.
When you schedule a script in Toad, the Job Manager creates a Windows scheduling task. Scheduled scripts run in batch mode. If using another scheduling application, specify "-batch=true" in the command. This executes the script in non-visual (batch) mode. See the procedure below.
To execute a script from the Automation window
Click Run in the Wizard bar. The script is compiled and executes immediately.
To view the script's execution log, select the Log tab.
If you want to schedule the script, first resolve any problems that caused activities to fail or disable failed activities. An activity can be disabled from its Activity Info window.
Tip: To execute a script from the Project Manager, right-click the script and select Execute.
To schedule an automation script
Note: To run a scheduled script against a password-protected database, the password must be saved in the Connection Properties dialog.
To execute a script in batch mode
To start Toad and execute a script in batch mode (non-visual mode), enter the following command and arguments:
<path to the toad.exe application> -batch=true “<path to Automation script>”
For example:
C:\Program Files\Quest Software\Toad Data Point 6.2.3\toad.exe -batch=true "<path\scriptfilename.tas>"
Notes:
The Automation module is disabled if Toad was installed with the Prohibit saving passwords option selected.
© ALL RIGHTS RESERVED. Termini di utilizzo Privacy Cookie Preference Center