Parameters

It's all about being flexible ..

Dynamic queries are essential for flexible data retrieval. By using variable criteria instead of hard-coded values, you can filter records differently each time without modifying the query structure. This makes your queries more reusable and adaptable to changing requirements.

SQL

select {[Measures].[Sales], [Measures].[Quantity]} ON COLUMNS,
NON EMPTY [Time].Children ON ROWS
from [SteelWheelsSales]
where ([Order Status].[${status}])

MDX

SELECT
NON EMPTY {[Measures].[Quantity]} ON COLUMNS,
NON EMPTY {[Markets].[${markets}].Members} ON ROWS
FROM [SteelWheelsSales]

A parameter acts as a dynamic variable in your queries, allowing you to change values without modifying the query structure itself. Let's explore how they work and how to use them effectively.

Instead of using fixed values in your queries, you can use parameters that change based on user input or other conditions.

Parameters use the syntax: ${parameterName}.

-- Static query
select * from customers where country in ('USA');

-- Parameterized query
select * from customers where country in (${country});

In CDA, parameters are defined using XML syntax under a Parameters element:

<Parameters>
    <Parameter default="USA" name="country" type="String"/>
</Parameters>

So now when defined in the CDA, the Query attribute needs to match the name in the parameter:

<?xml version="1.0" encoding="UTF-8"?>
<CDADescriptor>
   <DataSources>
      <Connection id="sqlSample" type="sql.jndi">
         <Jndi>SampleData</Jndi>
      </Connection>
   </DataSources>
   <DataAccess access="public" connection="sqlSample" id="sqlSample"
type="sql">
      <Cache duration="3600" enabled="true"/>
      <Columns/>
      <Parameters>
         <Parameter default="USA" name="country" type="StringArray"/>
      </Parameters>
      <Query>select * from customers where country in (${country})</
Query>
   </DataAccess>
</CDADescriptor>

Prepared statements in SQL are a security and performance feature where query parameters are replaced by placeholders. When the JDBC driver supports it, these queries are precompiled in the database during preparation; otherwise, precompilation happens at execution time.

The key benefits are twofold:

First, the database can reuse the access plan for similar queries, making execution significantly faster than regular statement objects.

Second, prepared statements automatically protect against SQL injection attacks by properly escaping and handling input parameters.


Defining Multiple Parameters in a Query

You can define several parameters within a single query using the Parameters tag. Each parameter needs its own Parameter element with three key attributes:

• Name - Must match the parameter reference in your query

• Default Value - Used when no specific value is provided

• Type - Determines what kind of data the parameter accepts

Available Parameter Types:

Single Value Types:

String: Text values

Integer: Whole numbers only (e.g., 10, 365)

Numeric: Numbers with decimals (e.g., 15.39)

Date: Date values

Array Types (for IN conditions):

StringArray: Multiple text values

IntegerArray: Multiple whole numbers

NumericArray: Multiple decimal numbers

DateArray: Multiple date values

Last updated

Was this helpful?