Database - SQL Query
Declaration
<AMDATABASE ACTIVITY="sql_query" CONNECTION="text (encrypted)" LOGIN="YES/NO" RESULTDATASET="text" SQLQUERY="text" CACHEDAFTER="DateSerial%" MAXROWS="text" TIMEOUT="10" />
Description: Passes a SQL (Structured Query Language) statement including queries to the data source specified via OLEDB. If a query is specified, a Dataset is created and populated with the query results.
Because the results retrieved from a query may contain multiple records (rows) and multiple fields (columns), to access the data, you must use the Loop Dataset activity to iterate the records and embedded expressions to extract the data from the individual fields. See Datasets below for more information.
Practical Usage
Useful for automated retrieval, update and management of data. Could also be used to test database response times and size quotas. Given that this action provides the full power of SQL, which is a powerful database language itself - the possibilities are limitless. A working knowledge of SQL will help get the most out of this action.
Connection Parameters
Property |
Type |
Required |
Default |
Markup |
Description |
---|---|---|---|---|---|
Connection |
|
|
|
|
Indicates where the connection string should originate from. This is a design time parameter used only during task construction and configuration, thus, comprises no markup. The available options are:
|
Type |
Text (options) |
Yes if Connection set to Host |
Database |
|
The type of connection string to use. This parameter is active only of the Connection parameter is set to Host. The available options are:
|
Database connection |
Text |
Yes if Type set to Database |
(Empty) |
CONNECTIONSTRING= "AM1P2qCkbYiFHdqWN7qDphOEw==aME" |
Specifies a custom OLEDB connection string. The connection string provides driver, server, username, password and other important information to the OLEDB subsystem so that the connection can be made. To generate a connection string, press the Build connection button. This opens the Data Link Properties Wizard dialog which will help you with construct of your connection string. To clear an existing connection string, press the Clear button. This parameter is available only if the Type parameter is set to Database. |
Connection string |
Text |
Yes if Type set to Connection string |
(Empty) |
PREDEFINEDCONNECTION= "%MyPreDefinedConnection%" |
Specifies the OLEDB connection string. Use this option to copy an existing connection string and paste it in the provided text box. This parameter is available only if the Type parameter is set to Connection string. |
Data link file |
Text |
Yes if Type set to Data link file |
(Empty) |
CONNECTION="c:\temp\fileName.udl" |
The path and filename of the data link (.udl,.dsn) file to connect with. Click the folder icon to open a standard Explorer dialog allowing for easy navigation to the desired data link file. This parameter is active only if the Type parameter is set to Data link file. |
Prompt user for a name and password |
Yes/No |
No |
No |
LOGIN="YES" |
If set to YES, specifies that the user should be prompted to enter a username and password for the database server each time the task is run.Set to NO by default. |
Protect connection string |
|
|
|
|
If enabled, the value entered in the Connection string parameter will be encrypted in both visual and AML mode as a means of protecting data embedded in the connection string. If disabled (default), the connection string will appear as normal readable text. Enable this option if your connection string contains sensitive data that you may not want others to view. This is a visual mode parameter used only during design time, therefore, contains no properties or markup. It is available only if the Type parameter is set to Connection string. |
Session name |
Text |
Yes if Connection set to Session |
SQLSession1 |
SESSION="Session10" |
Indicates the name of an existing session to attach this activity to. The connection string will derive from that session. This parameter is active only if the Connection parameter is set to Session. |
Pre-defined connection string |
Text |
Yes if Connection set to System |
(Empty) |
|
Indicates the pre-defined connection string that should be used to connect to the database. This parameter is active only if the Connection parameter is set to System. |
SQL statement |
Text |
Yes |
(Empty) |
SQLSTATEMENT="SELECT * FROM CUSTOMER WHERE CITY='Los Angeles'" |
Specifies the SQL statement that should be executed. |
Create and populate dataset |
Text |
Yes |
(Empty) |
RESULTDATASET="theDataset" |
The name of the dataset that should be created and populated with the results (if any) of the SQL Statement upon execution. To access the data in subsequent steps simply specify %datasetname.fieldname% (where datasetname specifies the name of the dataset and fieldname specifies the name of the dataset field) inside a Loop Dataset action (to recurse the rows). More details regarding datasets can be found below under Datasets. |
Cache Parameters
Property |
Type |
Required |
Default |
Markup |
Description |
---|---|---|---|---|---|
Never use cached data |
|
|
|
|
If enabled, specifies that cached data will not be used for this connection. This is a visual parameter used only during design time, therefore, contains no markup. When this parameter is enabled, the Use cached query data if execution date is after parameter is ignored. |
Use cached query data if execution date is after |
Date |
No |
(Empty) |
CACHEDAFTER="%DateSerial(2010,06,14) + TimeSerial(11,41,44)%" |
If enabled, specifies a date/time after which the query should use cached data instead of contacting the database. Usually this would be a calculated value using an expression containing DateAdd. When this parameter is enabled, the Never use cached data parameter is ignored. |
Advanced Parameters
Property |
Type |
Required |
Default |
Markup |
Description |
---|---|---|---|---|---|
Property |
Type |
Required |
Default |
Markup |
Description |
Maximum number of rows returned (optional) |
Number |
No |
(Empty) |
MAXROWS="500" |
The maximum number of rows that should be returned by the database server. |
The query times out after (optional) |
Number |
No |
(Empty) |
TIMEOUT="2" |
The amount of time that the query should be allowed before timing out at the server level. |
Measure |
Text (options) |
No |
Seconds |
MEASURE="minutes" |
The time measurement that should correspond to the query timeout value entered. The available options are:
|
Description tab - A custom description can be provided on the Description tab to convey additional information or share special notes about a task step.
Error Causes tab - Specify how this step should behave upon the occurrence of an error. (Refer to Task Builder > Error Causes Tab for details.)
On Error tab - Specify what AWE should do if this step encounters an error as defined on the Error Causes tab. (Refer to Task Builder > On Error Tab for details.)
Datasets
A dataset is a multiple column, multiple row container object. This action creates and populates a dataset. The fields contained within the dataset are determined by the query that was executed. For example if the following query is executed:
Then the following dataset would be generated (where 'datasetname' specifies the name of the dataset):
datasetname.firstname
datasetname.lastname
datasetname.company
A record (row) is created for each record (row) that is retrieved from the server. To access this data use the Loop Dataset Action to loop through the records, inside the loop you can extract the data from the field of your choice (from the current record) by using an embedded expression such as the one that follows:
%datasetname.firstname%
or you could combine two fields together like this:
%datasetname.firstname + " " + datasetname.lastname%
Embedded Expressions such as these can be used in any parameter in any action. So, to display the data in a message box the AML code would look like this:
<AMMESSAGEBOX MESSAGETEXT="%datasetname.firstname%" WINDOWTITLE="The firstname of the current record is">
At runtime the text %datasetname.firstname% is replaced by the contents of the subject of the current record.
The amount of datasets as well as the name assigned for each dataset fundamentally depends on the activity that initially created them. In addition, there are standard fields included in every dataset. You can easily view the list The table below describes these fields (assuming the name of the dataset is theDataset):
Name |
Data Type |
Return Value |
---|---|---|
theDataset.CurrentRow |
Number |
The current row that will be accessed in the dataset by an expression that does not contain a specific row index. |
theDataset.TotalRows |
Number |
The total number of rows in the dataset |
theDataset.TotalColumns |
Number |
The total number of columns (not including the static columns) in the dataset. |
theDataset.ExecutionDate |
Date |
The date and time the dataset was created and populated |
theDataset.RowsAffected |
Number |
The number of rows affected by an update. |
theDataset.SQLQuery |
Text |
The SQL Query that was used to generate this dataset (If a SQL Query was not used, this value is empty). |
theDataset.Datasource |
Text |
The datasource used for the SQL Query, if applicable. |
theDataset.ColumnNames |
Text |
A comma-delimited list of the column names in the dataset |
Example
The sample AML code below can be copied and pasted directly into the Steps panel of the Task Builder.
Description: This sample task demonstrates using SQL query with a Loop Dataset activity. It retrieves all customers where the 'City' field is 'San Diego' and populates a dataset with the results. A Loop Dataset activity is used to loop through the rows of the dataset. During each iteration, a message box appears showing the current result. To make this task work, change the SQL Query connection string and query to match your database.
<AMSQLQUERY CONNECTIONSTRING="AM2y6SWh0mxeUsrZJdHT9FyayHUkPdLQWKrLISRd0X xeqsoFJ7XTZF+2yImnfdJ4X17LWSfx08jfpsltISHRFF3yyREndda10/BfctA==aME" RESULTDATASET="theDataset" TIMEOUT="2" MEASURE="minutes">SELECT firstname, lastname from customer where city='San Diego'</AMSQLQUERY> <AMLOOP TYPE="DATASET" DATASET="theDataset"> <AMSHOWDIALOG WINDOWTITLE="CURRENT NAME">First Name - %theDataset.firstname% Last Name - %theDataset.lastname%</AMSHOWDIALOG> </AMLOOP>