SQL Query

Declaration

<AMSQLQUERY CONNECTIONSTRING="text" RESULTDATASET="text" TIMEOUT="number" MEASURE="text(options)">SELECT firstname, lastname from customer where city='San Diego'</AMSQLQUERY>

See Also

Description

Passes an SQL (Structured Query Language) statement including queries to the data source specified via OLEDB. If a query is specified, an AutoMate Dataset with the name you indicate is created and populated with the query results.

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.

NOTE: Because the results retrieved may contain multiple records (rows) and multiple fields (columns) - to access data retrieved from SQL Query you must use the Loop Dataset action to iterate the records and embedded expressions in order to extract the data from the individual fields (i.e. %datasetname.fieldname%). See notes below for more information.

Parameters

General Properties

Property

Type

Required

Default

Markup

Description

Connection String Type

 

 

 

 

Indicates where this action's connection string should originate from. The connection string includes the source database name, driver, username, password and other properties needed to establish the initial connection. Different parameters are available depending on the option selected. The available options are:

  • Use custom connection string (Default) - Specifies that a custom OLEDB connection string will be set for this action. Select this option if performing a single query.

  • Use existing SQL connection from a session - Specifies that a connection string should derive from a session created by a previous Open SQL Connection step. This allows several queries to be linked to a specific session.

  • Use pre-defined connection string - Specifies that the connection should derive from a variable or constant.

NOTE: This is a visual mode parameter only used during design time. It contains no markup.

Connection String

Text

Yes if connection type is custom

(Empty)

a)CONNECTIONSTRING=

"AM1NGBAj8FWs8hn9B2/DA0A"

b)CONNECTIONSTRING="%varName&"

Specifies an OLEDB connection string. Click the New button in the visual step editor to open the Data Link Properties Wizard dialog allowing you to setup a custom connection string. Valid only if Use custom connection string is selected in the Connection String Type parameter. For more details, see Data_Link_Properties_Wizard.

Session Name

Text

Yes if connection is from a session

SQLSession1

SESSION="Session10"

Indicates the session name that this connection should derive from. Valid only if Use existing SQL connection from a session is selected in the Connection String Type parameter.

Pre-defined Connection String

Text

Yes if connection is pre-defined

(Empty)

a)PREDEFINEDCONNECTION=

"%varName%"

b)PREDEFINEDCONNECTION=

"AM1NGBAj8FWs8hn9B2/DA0A"

Indicates the pre-defined connection string that should be used. Valid only if Use pre-defined connection string is selected in the Connection String Type parameter.

Prompt user for a name and password

Yes/No

No

No

LOGIN="YES"

Specifies that the user should be prompted to enter a username and password for the database server each time the task is run. Valid only if Use custom connection string or Use pre-defined connection string is selected in the Connection String Type parameter.

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 Notes.

SQL Statement

Text

Yes

(Empty)

SQLSTATEMENT="SELECT * FROM CUSTOMER WHERE CITY='Los Angeles'"

Specifies the SQL statement that should be executed.

 

Cache Properties

Property

Type

Required

Default

Markup

Description

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.

The default option is Never use cached data which specifies that cached data will not be used for this connection.

 

Advanced Properties

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"

 Enter a value and select from the following time intervals:

  • Milliseconds

  • Seconds (default)

  • Minutes

  • Hours

 

Description Properties

The Description tab allows you to customize the text description of any step as it appears in the Task Builder's Steps Pane.

More on setting custom step description

Error Causes Properties

The Error Causes tab allows you to select/omit specific errors that should cause a particular step to fail.

More on Error Causes properties

On Error Properties

The On Error tab allows you to determine what the task should do if a particular step encounters an error.

More about On Error properties

Notes

Datasets

A dataset is a multiple column, multiple row container object. This action creates and populates a dataset, the fields contained within that 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.

More on datasets

Along with the above fields, there are standard fields included in every dataset. 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

 

Variables and Expressions

All text fields allow the use of expressions, which can be entered by surrounding the expression in percentage signs (Example: %myVariable% or %Left('Text',2)%). To help construct these expressions, you can open Expression Builder from these fields by clicking the Insert expression/variable button or pressing F2.

More on variables
More on expressions

More on the Expression Builder

Example

NOTE: The code below can be copied and pasted directly into the Steps pane of the Task Builder.

 

Description: This sample task demonstrates using SQL query with a Loop Dataset action. It retrieves all customers where the 'City' field is 'San Diego' and populates a dataset with the results. A Loop Dataset action 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="AM2y0lldgEXA7IsSVZ0/xcPsmhIHnT2FxWyg0mgVNsXerJASbF0xRf8khhJXHTUF9Oy3klqdP837rLDSZV0axcvsopJEHQdF/yyy0kddEo3YLI6SWh0mxeUsrZJdHT9FyayHUkPdLQWKrLISRd0XxeqsoFJ7XTZF+2yImnfdJ4X17LWSfx08jfpsltISHRFF3yyREnddMQ3bLIHSUx0phcEsvZJ2XSEF2my70mXdCAVpbJUSVl07xersrRJsXS0Fn2yyEnNdEcXGrAySWB0phclsp1Jm3TYFzyyZ0mHdPgXJLLHSXB0Nhc0siNJBHTKF7GyZ0kEdFcXt7J5Sa10/BfctA==aME" RESULTDATASET="theDataset" TIMEOUT="2" MEASURE="minutes">SELECT firstname, lastname from customer where city='San Diego'</AMSQLQUERY>

<AMLOOP TYPE="DATASET" DATASET="theDataset">

     <AMSHOWDIALOG>First Name - %theDataset.firstname%

Last Name - %theDataset.lastname%

</AMSHOWDIALOG>

</AMLOOP>

 

↑ Top of Page