Database - SQL query

Declaration

<AMDATABASE ACTIVITY="sql_query" SESSION="text" CONNECTIONTYPE="text (options)" CONNECTIONSTRING="text (encrypted)" PREDEFINEDCONNECTION="text" CONNECTION="text (encrypted)" LOGIN="YES/NO" RESULTDATASET="text" INCLUDEMILLSECONDS="YES/NO" SQLSTATEMENT="text" SQLQUERY="text" CACHEDAFTER="DateSerial%" MAXROWS="text" TIMEOUT="10" MEASURE="text (options)" /> />

Related Topics    

Description

Passes SQL (Structured Query Language) statement(s) (including queries) to the data source by way of OLEDB. Specifying a query creates and populates a Dataset with the query results.

IMPORTANT: Multiple statements are sent individually, not all at once.
NOTE: 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 in order 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.

Parameters

Connection

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:
  • Host (default) - A new or existing connection string will be set individually for this activity. Select this option if performing a single database operation.
  • Session - The connection string should derive from a session created by a previous Database - Open SQL connection step. This allows several queries to be linked to a specific session.
  • System - The connection string will derive from one configured and saved for the system.
Type Text (options) Yes if Connection set to Host Database
  • CONNECTIONTYPE="database"
  • CONNECTIONTYPE="connection_string"
  • CONNECTIONTYPE="file"
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 (default) - Database connection will derive from a new OLEDB connection string created by way of the Data Link Properties Wizard. Press the Build Connection button to open the Data Link Properties Wizard. Press the Clear button to clear an existing connection string.
  • Connection string - Database connection will be established using a specific connection string. Enables you to copy an existing string onto the properties of this activity.
  • Data link file - Connection will be established by way of the data link file specified.
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 file name 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 selected, specifies that the user should be prompted to enter a username and password for the database server each time the task is run. This parameter is disabled 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)
  • PREDEFINEDCONNECTION="%varName%"
  • PREDEFINEDCONNECTION="AM1NGBAj8FWs8A0A"
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 to execute.
IMPORTANT: Multiple statements are sent individually, not all at once.
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 recurs the rows). More details regarding datasets can be found below under Datasets.
Include milliseconds for dataset DateTime type Yes/No No No INCLUDEMILLISECONDS=YES If selected, milliseconds associated with DateTime dataset values are included in the query results. This parameter is disabled by default.

Cache

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

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.
NOTE: The minimum timeout is 15 seconds. Any timeout set to less than that will be converted to 15 seconds.
Measure Text (options) No Seconds  MEASURE="minutes"  The time measurement that should correspond to the query timeout value entered. The available options are:
  • Milliseconds - The timeout value is measured in milliseconds.
  • Seconds (default) - The timeout value is measured in seconds.
  • Minutes - The timeout value is measured in minutes.
  • Hours - The timeout value is measured in hours.

Description

Error Causes

On Error

Additional notes

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:

SELECT firstname, lastname, company from customer where city='Los Angeles';

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):

Example

NOTE:
  • Copy and paste the sample AML code below directly into the Task Builder Steps Panel.
  • To successfully run the sample code, update parameters containing user credentials, files, file paths, or other information specific to the task to match your environment.

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.

Copy
<AMDATABASE ACTIVITY="sql_query" CONNECTIONTYPE="connection_string" CONNECTION="AM2y6SWh0mxeUsrZJdHT9FyayHUkPdLQWKrLISRd0XxeqsoFJ7XTZF+2yImnfdJ4X17LWSfx08jfpsltISHRFF3yyREndda10/BfctA==aME" RESULTDATASET="theDataset" SQLQUERY="SELECT firstname, lastname from customer where city='San Diego" TIMEOUT="2" MEASURE="minutes" />
<AMLOOP ACTIVITY="dataset" DATASET="theDataset" /><AMSHOWDIALOG WINDOWTITLE="CURRENT NAME">First Name - %theDataset.firstname%Last Name - %theDataset.lastname%</AMSHOWDIALOG><AMLOOP ACTIVITY="end" />