![]() | Programming Guide | ![]() |
Edits the HAVING clause in a SELECT statement for automatic SQL generation
#include <tmusubs.h>int dm_gen_change_select_having(char *arg, char *search_cond, int flag);
arg- Reserved for future use.
search_cond- The search condition to include in the
HAVINGclause.flag- Specifies the type of change to make with one of the following constants:
DM_GEN_APPEND- Adds
search_condto the end of theHAVINGclause. This produces the following statement:DBMS DECLAREcursorFOR SELECTselect_listFROMtablesHAVINGexisting_having_clauseANDsearch_condDM_GEN_PREPEND- Adds
search_condto the beginning of theHAVINGclause. This produces the following statement:DBMS DECLAREcursorFOR SELECTselect_listFROM
tablesHAVINGsearch_condANDexisting_having_clauseDM_GEN_REPLACE_ALLsearch_condreplaces the existingHAVINGclause. This produces the following statement:DBMS DECLAREcursorFOR SELECTselect_listFROMtablesHAVINGsearch_condIf
flagis set to this value andsearch_condis set to an empty string, theHAVINGclause is removed. For example:x = dm_gen_change_select_having
("", "", DM_GEN_REPLACE_ALL)
dm_gen_change_select_havinglets you edit theHAVINGclause built with the SQL generator. The data structure for theSELECTstatement, which is built by a call to dm_gen_sql_info (generally in theTM_SEL_GENevent), must already exist before this function is called.Generally, a
HAVINGclause sets search conditions for the precedingGROUP BYclause. The SQL generator createsGROUP BYclauses automatically for aggregate functions.GROUP BYclauses can also be generated using the function dm_gen_change_select_group_by.HAVINGclauses can be generated with the Having property or by using this function. For more information on automatic SQL generation, refer to Chapter 33, "Using Automated SQL Generation," in Application Development Guide.This function can be implemented as part of a transaction manager event function which processes the
TM_SEL_BUILD_PERFORMevent. If you are modifying the select processing for a server view, calldm_gen_change_select_havingfrom an event function attached to the first parent table view in the server view.To view a sample event function written in JPL, refer to the example in this section. For more information on writing transaction event functions, refer to Chapter 32, "Writing Transaction Event Functions," in Application Development Guide.
# JPL Procedure:
# Generate a having clause.
# Function property is set to titles_having.
proc titles_having (event)
vars retval(5)
if (event == TM_SEL_BUILD_PERFORM)
{
retval = dm_gen_change_select_having\
("", "count(*) > 2", DM_GEN_APPEND)
retval = dm_gen_change_select_having\
("", "dir_last_name like 'W%'", DM_GEN_APPEND)
if (retval != 0)
return TM_FAILURE
}
return TM_PROCEED