Dynamic Query (SQL)

Dynamic Query is a query that contains one or more values of certain issue fields in the WHERE clause.

Building a query using custom fields

  1. Create the necessary custom fields in Jira and remember their IDs. They should have a customfield_yyyyy format, where yyyyy is a number. 
    To find out a custom field ID:

    1. Go to the Custom Fields section in Jira administration.

    2. Locate your custom field and click Configure from the actions menu on the right.
      You can see your custom field ID as a parameter in the url:
      /secure/admin/ConfigureCustomField!default.jspa?customFieldId=yyyyy.

  2. After you go through the steps from the Configuration page and the data source configuration page, go to Administration > POWER PLUGINS CONFIGURATION > Database Custom Field.

  3. Fill in the Sql Query field for the main database custom field (customfield_xxxxx) with a dynamic clause of the following format:
    "WHERE col1={customfield_yyyyy} AND col2={customfield_zzzzz} OR col2={customfield_ttttt}").

  4. Save the configuration.

Example

SELECT col1, col2, col3, col4 FROM testTable WHERE col1 = {customfield_10300}

 

The dynamic query works on an issue when creating, editing the issue or for transition screens. If for the customfield_xxxxx database custom field the dynamic clause has the "WHERE col1={customfield_yyyyy} AND col2={customfield_zzzzz}" format for instance, then when you change the value for customfield_yyyyy or customfield_zzzzz, the value for customField_xxxxx is cleared, and after that you can select a value from a new list of values determined by re-executing the query and taking into account the new values for customfield_yyyyy and customfield_zzzzz.

Building a Query Using System Fields

Project field works on any Jira screen, but note that Jira creates the key in uppercase.

System fields can also be used in the dynamic query by using one of the following parameters in the WHERE clause:

  • project – key of the project

  • assignee

  • key – key of the current issue

  • reporter

  • priority

  • versions

  • fixVersions

  • components

  • labels

  • created

  • updated

  • resolved

  • duedate

  • issuetype (the name of the issue type)

  • status

  • resolution 

  • currentuser 

  • currentuserkey 

  • issueid – available starting with versions 3.0.8/3.1.1. The cast to numeric type is not ensured, so you need to cast it.

     

For the versions, fixVersions, and components fields, if there are more than one value, the string in the database should be like the one saved on the issue in Jira.
For example: "v1, v2"  with comma and a space between values.

Example

 SELECT col1, col2, col3, col4 FROM testTable WHERE col1 = {assignee} AND col2 = {reporter}

Dynamic Query Using Dates Values

Normally we can only have columns of text type in combination with dynamic custom fields in the WHERE clause. If a date column or some other non text type is used in the WHERE clause, the query does not work.

For instance if the testdate column is numeric in the testTable table, the following query does not work, because the value of the {created} is a string.

select testdate, col1, col2 from worklog where testdate = {created};

The solution is to use functions available for the database chosen, to transform from string to the type of the column in the SQL query.

For instance, for PostgreSQL: SELECT testdate, col1, col2 from worklog WHERE testdate = cast({created} as timestamp).

For the dates(created, updated), the date from the database should be of the following format: 2012-03-12 17:40:34.973.

Types of Supported Custom Fields

Jira custom fields

  • Text Field (< 255 characters)

  • User Picker

  • URL Field

  • Free Text Field (unlimited text)

  • Number

  • Select List

  • Radio Buttons (since v. 2.1.13)

Additional custom field

Dynamic Query Using Numeric Values for Dependents

Normally we can only have columns of text type in combination with dynamic custom fields in the WHERE clause. If a numeric column or some other non text is used in the WHERE clause, the query does not work.

For instance if the column id is numeric in the testTable table, the following query does not work, because the value of the customfield_10300 is a string.

The solution is to use functions available for the database chosen, to transform from string to the type of the column in the SQL query.

For instance, for PostgreSQL: SELECT id, col2, col3, col4 FROM testTable WHERE id = cast({customfield_10300} as numeric).

Dynamic fields cannot be used between single or double quotes. For instance, a query with a dynamic field in the LIKE clause does not work.

The solution is to use string concatenation provided by the database, so that {key} is outside of ' or ".

For instance, for PostgreSQL, Oracle, HSQLDB: SELECT col1 FROM table WHERE col1 LIKE '%' || {key} || '%'

For MySQL: SELECT col1 FROM table WHERE col1 LIKE concat('%', {key}, '%')

For MS Sql Server: SELECT col1 FROM table WHERE col1 LIKE '%' + {key} + '%'

For other databases, please see what operator should be used for string concatenation.