Oracle® Rules Palette Release 9.1.0.0.0 E15811-01

# SQL (Structured Query Language) Best Practices

## SQL Format

1. Capitalize all letters in SQL keywords such as SELECT,JOIN or WHERE.

2. String (text) variables in SQL statements should have brackets around them and then single quotes around them.

3. Integer variables in SQL statements should have brackets around them without quotes.

4. Avoid using SQL inside Math Loops.

- SQLs needed in MathLoops should be handled outside the loop.

- SQLs may fill arrays and then the arrays should be indexed inside the loop for manipulation.

5. Consider whether the query could return one or more than one result and configure it appropriately.

- Use functions to ensure one value is returned when appropriate. Functions such as SUM, MIN, MAX, etc., ensure the return of one value.

- SQL that is not used to fill a combo box field should return one value for a MathVariable.

- An exception to returning one value can be made when performing a COLLECTION.

6. COLLECTION SQL should return two elements: Name and Value with multiple rows. When SQL is used to fill a field’s combo box, the SQL should return two columns and multiple rows.

7. In a COLLECTION if the KEY is the same name as a field name, then concatenate a string to the first column returned in the COLLECTION SQL.

- If possible, avoid calling the FIELD and COLLECTION KEY the same name.

8. SQL that is used to fill a field combo box should return two values.

- One value for the Option Value and the other value for the Text Value.

9. When possible, subqueries should be avoided and JOINs should be used for better performance.

10. Organize table names used in SQLs with the selection criteria of that table. The FROM table will be last using WHERE, all other tables (through JOINs) should use AND.

11. Only one table name should be after FROM. The remaining tables will be part of the SQL through JOIN.

12. When using ThisPendingActivityXML, use the following configuration to help performance:

- <MathVariable VARIABLENAME="ThisPendingActivityXML" TYPE="SQL">SELECT XMLData FROM AsActivity WHERE ActivityGUID = '[Activity:ActivityGUID]'</MathVariable>.

13. Use ThisPendingActivityXML as needed then do:

- <MathVariable VARIABLENAME="ThisPendingActivityXML" TYPE="VALUE"></MathVariable>. This configuration will stop writing too much data to the database. In the extreme circumstances that you do need to load up a large amount of XML into a variable, this will clear out the variable so it does not write as much data to AsActivity XMLData.

## Retrieving Data

1. Use SQL to retrieve data from the database.

- Do not use SQL to manipulate dates.

- Do not use the SQL CASE statement.

2. Don’t JOIN tables unnecessarily.

- For example, AsActivity contains PolicyGUID, therefore there is no need to JOIN AsPolicy to retrieve PolicyGUID.

3. When performing SQL to select one or more AsActivity records, be sure to consider AsActivity.StatusCode and AsActivity.TypeCode values to include/exclude records as desired.

- Avoid returning pending, shadow, undo, redo, etc., records that are not needed.

- For AsActivity.StatusCode,

- For active use 01 and 14 together.

- For pending use 02, 09 and 13 together.

4. When querying for the most recent transaction of a certain type, query by EffectiveDate DESC, ProcessingOrder DESC, ActivityGMT DESC and then fetch first row only. (Also see #1 above).

5. If a piece of information is already defined as a field, don’t use a SQL to retrieve this information again from the database. Since this value is already a <Field>, change to TYPE=”FIELD” and reference the field name.

6. Use PLANFIELD, POLICYFIELD, or SEGMENTFIELD when retrieving information from AsPlanField, policy information, or a segment.

- For SEGMENTFIELD retrieval, if more than one field is to be returned, and not in a segment loop, then query the SegmentGUID and insert into an array.

- Initialize a MathVariable to hold the array's index value.

- With these two variables, you can now use SEGMENTFIELD.

Example XML

<MathVariable VARIABLENAME="SegmentGUIDArray" TYPE="NUMERICARRAY" OPERATION="FILLBY-SQL">Write SQL to retrieve the SegmentGUID that you would like to retrieve a Segment Field</MathVariable>

<MathVariable VARIABLENAME="SegmentGUIDArray_index" TYPE="VALUE">0</MathVariable>

<MathVariable VARIABLENAME="SegmentFieldRetrieved" TYPE="SEGMENTFIELD" SOURCEARRAY="SegmentGUIDArray">Name of the Field you would like to retrieve</MathVariable>

7. In fields, when defining a combo box, use SQL queries calling ASCODE rather than redefining as fixed queries in business rules when possible. This will help with standardization and maintenance.

- Information may need to be added to AsCode and must be cleared with a Configuration Lead.

8. Use SQL as little as possible. Use COLLECTION to bring back as much usable data as possible in one round trip to the database. Use CollectionValue to parse the individual pieces of data from the COLLECTION.