Home Docs Changelog Download Pricing Customer Portal Contact

Using Variables in Tasks

SQLMessenger User Manual - Using Variables in Tasks

 

1 Overview

To enhance the flexibility of tasks, SQLMessenger supports the use of variables within tasks. For example, you can utilize variables to change email subjects, email body content, recipient email addresses, and query conditions in SQL statements during task execution.

There are two types of variables in SQLMessenger:

User Variables

Variables customized by users. The variable name must only contain underscores, letters, and numbers. The variable value can be a fixed value or dynamically determined using SQL statements during task execution.

System Variables

System-predefined variables. The variable name starts with the "@@" characters.

2 Create User Variables

Variables customized by users. The variable name must only contain underscores, letters, and numbers. The variable value can be a fixed value or dynamically determined using SQL statements during task execution.

You can create task variables within a task, which are available throughout the scope of the task. You can also create template variables in an attachment template, which are available within the scope of the corresponding template.

Create task variable

Create template variable

Variable Attributes dialog box

In this dialog box:

Variable Name

Enter the name of the variable here.

The variable name must only contain underscores, letters, and numbers.

In the same context, variable names cannot be duplicated. For example, within the same task, the name of a task variable must be unique, and within the same template, the variable names must also be unique.

Variable Value Source

Select the source for the variable value here.

The variable value can be a fixed value set during configuration, or it can be dynamically queried using an SQL statement during task execution.

Value

If the "Variable Value Source" is "Fixed Value", you can enter a value for the variable here.

SQL

If the "Variable Value Source" is "Generated by SQL Query", then you need to enter the SQL query here to retrieve the value of the variable.

Variable Options:

Use the value of the variable as HTML code if it is used in an HTML template

If this option is selected, when using this variable in an HTML document (such as the body of an email), the variable value will be inserted directly into the HTML document as HTML code.

For example, the value of a variable is "<a href='https://www.sqlmessenger.com'>link to web<a/>":

This option is not selected, the variable value will be displayed as plain text

This option is selected, the variable value will be displayed as a hyper link

Allow passing variable value from external interface table

If this option is selected, the system will allow the value for this variable to be passed in from the TASK_PARAMS field in an external interface table. For example, the following SQL statement will initiate a task with ID 123 and pass a value for the task's variable "UserName":

INSERT INTO sqlmessenger_intf INSERT INTO sqlmessenger_intf
            (oper_type,
             task_id,
             task_params)
VALUES      ( 'T',--Operation type. "T" indicates to start a task.
              123,--Task ID to start
              'UserName=Andy'
--Pass "Andy" as the variable value to the task's variable "UserName".
); 

Note: When passing variable values from the external interface table, the task will use the passed values as the variable values for this run, and the configured values for these variables will be ignored.

Allow empty value for the variable

Select whether to allow this variable to have an empty value. If this option is not selected, the task will be aborted when the variable's value is an empty string.

If the SQL field is a datetime type, convert it according to the local date format

Available since v2.4.

If a variable value comes from a database field of datetime type, SQLMessenger converts the datetime to a string using the database’s default format. For example, SQL Server may convert a datetime to "2026-03-22 16:04:15.810".

When this option is selected, SQLMessenger converts the variable value to a string using the local date format. For instance, if the local date format is "DD.MM.YYYY", the variable value will be converted to "22.03.2026 16:04:15".

Note: This option is enabled by default for newly created task variables. For task variables created in older versions, this option is disabled by default.

SQL Security Validation: If the variable is used in SQL statements, conduct a security validation on the variable value

When this option is selected, the system will perform a security check on the variable's value when it is used in an SQL statement. For example, if the value of variable "Month" is "202401;", the following statement will not be executed:

SELECT *
FROM   sales_stat
WHERE  sales_month = #month#; 

The value of the variable did not pass the SQL statement's security check

To avoid SQL statement execution failures caused by variable values containing special characters, you can modify the SQL query's parameter format to JDBC parameters, for example:

SELECT *
FROM   sales_stat
WHERE  sales_month = #[month]#; 

Using Variables in SQL Statements

3 System Variables

For ease of task configuration, the system has predefined some commonly used system variables. The variable names of system variables start with "@@".

Different system variables can be used in different locations within a task. You can view the list of available system variables in the Select Variable dialog box.

View the list of available system variables in the Select Variable dialog box

Tips: The date format returned by date-related variables depends on the date settings of the operating system when SQLMessenger is installed.

4 Encrypted Variables

Starting from version 2.2.0004, SQLMessenger introduces the "Encrypted Task Variables" feature. You can use an encrypted variable table in tasks to store confidential variable values, such as the Telegram BOT Access Token.

Encrypted variables have the following differences with regular variables:

When the system stores the value of an encrypted variable, it will encrypt the variable value.

The value of an encrypted variable can only be retrieved in HTTP command using "@@GetEncryptedTaskVarForHTTPCmd" method.

The value of the encrypted variable is displayed as "*" in task logs.

When exporting task configurations, the value of the encrypted variable will not be exported.

You can open the Encrypted Variable Manager by clicking the "Encrypted Vars Config" button on the Task Variables tab in the task configuration dialog.

Opening the Encrypted Variable Manager

In this dialog, you can add, modify, or delete encrypted variables.

Encrypted Variable Configuration Dialog

In this dialog box:

Var Name

Enter the name of the variable here.

The variable name must only contain underscores, letters, and numbers.

Variable names are case-insensitive and must be unique in the encrypted variable table.

Var Value

Enter the value of the variable here.

Note: The variable value is only displayed when it is entered. After saving the task draft or publishing the task, the system will no longer display the value of the variable.

Apply To Domain

Specify domains (e.g., "microsoft.com" or "google.com") where the variable can be used.

If the variable is allowed to be used on multiple domains, please separate the domains with a semicolon ";".

When the variable is used, the system will compare the trailing characters of the host part in the URL called by the HTTP command with the domains configured here. If an attempt is made to use this variable on a domain other than those specified in "Apply To Domain", SQLMessenger will report an error and abort the HTTP command.

For example:

microsoft.com means the variable can be used on all URLs ending with "microsoft.com".

login.microsoft.com means the variable can be used on all URLs ending with "login.microsoft.com".

microsoft.com;azure.com means the variable can be used on all URLs ending with "microsoft.com" or "azure.com".

Using @@GetEncryptedTaskVarForHTTPCmd method in HTTP Commands

The value of the encrypted variable can only be retrieved in HTTP commands using the @@GetEncryptedTaskVarForHTTPCmd method. The format is:

@@GetEncryptedTaskVarForHTTPCmd(Var Name)

For example:

select

'

url:https://api.telegram.org/bot#@@GetEncryptedTaskVarForHTTPCmd(TELEGRAM_BOT_TOKEN)#/sendDocument

method:post

field:chat_id=#ChatID#

file:document=#@@GetAttachFile($body$)#

' as _http_request_

Using @@GetEncryptedTaskVarForHTTPCmd Method in HTTP Commands

When SQLMessenger executes the task, it will place the value of the variable "TELEGRAM_BOT_TOKEN" in the corresponding position in the URL.

5 The Scope of Variables

System variables and task variables are available within the scope of the entire task, while template variables are only available within the scope of the corresponding attachment template or body template.

6 Use Cases of Variables

6.1 Using Variables in SQL Statements

Using variables in SQL statements

6.2 Using Variables in Email Body Template

Using variables in the email body template

6.3 Using Variables in Email Subject

Using Variables in the email subject

6.4 Using Variables to Dynamically Determine Email Recipients

Using variables to dynamically determine email recipients

Dynamically Set Recipients for the Task

6.5 Using Variables in Attachment File Names

Using variables in attachment file names

6.6 Using Variables in Sheet Names

Using variables in sheet names

6.7 Using Variables as Watermarks in PDF Files

Using variables in sheet names

Convert Document To PDF File

Last updated on June 05, 2024


Cookie Settings

We use cookies to enhance your browsing experience, serve personalized content, and analyze our traffic. By clicking "Accept All", you consent to our use of cookies in accordance with our Cookie Policy.

You can change your preferences at any time by clicking on "Cookie Settings" in the website footer.

Cookie Preferences

Manage your cookie preferences. You can enable or disable different types of cookies below. Necessary cookies cannot be disabled as they are required for the website to function properly.

Necessary Cookies

Always active

These cookies are essential for the website to function and cannot be switched off. They are usually only set in response to actions made by you such as setting your privacy preferences, logging in or filling in forms.

Preferences Cookies

Makes the site easier to use

These cookies allow the website to remember choices you make and provide enhanced, more personal features.

Analytics Cookies

Helps us improve our website

These cookies help us understand how visitors interact with our website, which helps us improve our products and services.

Marketing Cookies

Used to track visitors across websites

These cookies are used to track visitors across websites. The intention is to display ads that are relevant and engaging.

Leave us a message.