ASSOCIATE LOCATORS

The ASSOCIATE LOCATORS statement gets the result set locator value for each result set returned by a stored procedure.

Invocation

This statement can be embedded in an application program. It is an executable statement that can be dynamically prepared. It cannot be issued interactively.

Authorization

None required.

Syntax

Read syntax diagram
              .-RESULT SET-.                 
>>-ASSOCIATE--+------------+--+-LOCATOR--+---------------------->
                              '-LOCATORS-'   

      .-,-------------------.      
      V                     |      
>--(----rs-locator-variable-+--)-------------------------------->

>--WITH PROCEDURE--+-procedure-name-+--------------------------><
                   '-host-variable--'   

Description

rs-locator-variable
Identifies a result set locator variable that has been declared according to the rules for declaring result set locator variables.
WITH PROCEDURE procedure-name or host-variable
Identifies the stored procedure that returned one or more result sets. When the ASSOCIATE LOCATORS statement is executed, the procedure name must identify a stored procedure that the requester has already invoked using the SQL CALL statement. The procedure name can be specified as a one-part, two-part, or three-part name. The procedure name in the ASSOCIATE LOCATORS statement must be specified the same way that it was specified on the CALL statement. For example, if a two-part procedure name was specified on the CALL statement, you must specify a two-part procedure name in the ASSOCIATE LOCATORS statement.

If a host variable is used to specify the name:

  • It must be a character string variable with a length attribute that is not greater than 255.
  • It must not be followed by an indicator variable.
  • The value of the host variable is a specification that depends on the server. Regardless of the server, the specification must:
    • Be left justified within the host variable
    • Not contain embedded blanks
    • Be padded on the right with blanks if its length is less than that of the host variable

Notes

Assignment of locator values: If the ASSOCIATE LOCATORS statement specifies multiple locator variables, locator values are assigned to the locator variables in the order that the associated cursors are opened regardless of whether they are still open or not at run time. Locator values are assigned to the locator variables in the same order that they would be placed in the SQLVAR entries in the SQLDA as a result of a DESCRIBE PROCEDURE statement.

Locator values are not provided for cursors that are closed when control is returned to the invoking application. If a cursor was closed and later opened again before returning to the invoking application, the most recently executed OPEN CURSOR statement for the cursor is used to determine the order in which the locator values are returned for the procedure result sets. For example, assume procedure P1 opens three cursors A, B, C, closes cursor B and then issues another OPEN CURSOR statement for cursor B before returning to the invoking application. The locator values assigned for the following ASSOCIATE LOCATORS statement will be in the order A, C, B:
ASSOCIATE RESULT SET LOCATORS (:loc1, :loc2, :loc3) WITH PROCEDURE P1;
   -- assigns locators for result set cursors A, C, and B

More than one locator can be associated with a result set. You can issue multiple ASSOCIATE LOCATORS statements for the same stored procedure with different result set locator variables to associate multiple locators with each result set.

  • If the number of result set locator variables specified in the ASSOCIATE LOCATORS statement is less than the number of result sets returned by the stored procedure, all locator variables specified in the statement are assigned a value, and a warning is issued. For example, assume procedure P1 exists and returns four result sets. Each of the following ASSOCIATE LOCATORS statement returns information on the first result set along with a warning that not enough locators were provided to obtain information about all the result sets.
    CALL P1;									
    ASSOCIATE RESULT SET LOCATORS (:loc1) WITH PROCEDURE P1;
    		-- :loc1 is assigned a value for first result set, and a warning is returned
    ASSOCIATE RESULT SET LOCATORS (:loc2) WITH PROCEDURE P1;
         -- :loc2 is assigned a value for first result set, and a warning is returned
    ASSOCIATE RESULT SET LOCATORS (:loc3) WITH PROCEDURE P1;
         -- :loc3 is assigned a value for first result set, and a warning is returned
    ASSOCIATE RESULT SET LOCATORS (:loc4) WITH PROCEDURE P1;
         -- :loc4 is assigned a value for first result set, and a warning is returned
  • If the number of result set locator variables that are listed in the ASSOCIATE LOCATORS statement is greater than the number of locators returned by the stored procedure, the extra locator variables are assigned a value of 0.

Accessing result sets from multiple CALL statements: An application can access to result sets created by multiple CALL statements. The result sets can be created by different procedure or by the same procedure invoked multiple times.

  • Invoking different procedures: Invoking different procedures with the same name can be done either explicitly by specifying the different collections or implicitly with the use of the PACKAGE PATH. For example, to identify the different collections explicitly, specify qualified names on the CALL statement. Although both procedures are named P2, they are different procedures. After the second CALL statement, result sets from both procedures are accessible to the application.
    CALL X.P2;
    CALL Y.P2;
    The collections for the two different procedures can also be determined implicitly from the PACKAGE PATH when unqualified procedure names are specified as part of the CALL statement. For example, assume that procedure P4 exists in collections X and Z. An application contains two CALL statements to invoke procedure P4. The references to procedure P4 in the CALL statements are unqualified. So, the PACKAGE PATH special register is used to resolve the procedure name. Procedure X.P4 is invoked for the first CALL statement and procedure Z.P4 is invoked by the second CALL statement. Following the second CALL statement, result sets from both procedures are accessible to the application.
    SET CURRENT PACKAGE PATH = X, Y, Z;
    CALL P4;
    SET CURRENT PACKAGE PATH = PATH Z, Y, X;
    CALL P4;
  • Invoking the same procedure multiple times: If the server and requester are both the same version of DB2®, you can call a stored procedure multiple times within an application and at the same nesting level. Each call to the same stored procedure causes a unique instance of the stored procedure to run. If the stored procedure returns result sets, each instance of the stored procedure opens its own set of result set cursors. For more information on this situation, see Multiple calls to the same stored procedure.
    When a procedure is invoked multiple times in an application and there is a need to process the result sets from the different instances at the same time, be sure to use the ASSOCIATE LOCATORS statement after each CALL statement to capture the locator values returned from each invocation of the procedure. For example, assume that procedure P exists in collection Z and that an application contains two CALL statements to invoke procedure P. The PACKAGE PATH is used to determine the collection for the procedure in the first CALL statement, and the collection is explicitly specified in the second CALL statement. Result sets from both procedures can be accessible to the application following both CALL statements if the locators for the result sets produced by the first CALL statement are captured with an ASSOCIATE LOCATOR statement before invoking the procedure the second time.
    SET CURRENT PACKAGE PATH = X, Y, Z;
    CALL P3;
    ASSOCIATE LOCATORS ...
    CALL Z.P3;
    ASSOCIATE LOCATORS ...
    -- process the result sets using the locators

Using host variables: If the ASSOCIATE LOCATORS statement contains host variables, the following conditions apply:

  • If the statement is executed statically, the contents of the host variables are assumed to be in the encoding scheme that was specified in the ENCODING parameter when the package or plan that contains the statement was bound.
  • If the statement is executed dynamically, the contents of the host variables are assumed to be in the encoding scheme that is specified in the APPLICATION ENCODING bind option.

Examples

The statements in the following examples are assumed to be in PL/I programs.

Example 1: Use result set locator variables LOC1 and LOC2 to get the result set locator values for the two result sets returned by stored procedure P1. Assume that the stored procedure is called with a one-part name from current server SITE2.
   EXEC SQL CONNECT TO SITE2;
   EXEC SQL CALL P1;
   EXEC SQL ASSOCIATE RESULT SET LOCATORS (:LOC1, :LOC2)
            WITH PROCEDURE P1;
Example 2: Repeat the scenario in Example 1, but use a two-part name to specify an explicit schema name for the stored procedure to ensure that stored procedure P1 in schema MYSCHEMA is used.
   EXEC SQL CONNECT TO SITE2;
   EXEC SQL CALL MYSCHEMA.P1;
   EXEC SQL ASSOCIATE RESULT SET LOCATORS (:LOC1, :LOC2)
            WITH PROCEDURE MYSCHEMA.P1;
Example 3: Use result set locator variables LOC1 and LOC2 to get the result set locator values for the two result sets that are returned by the stored procedure named by host variable HV1. Assume that host variable HV1 contains the value SITE2.MYSCHEMA.P1 and the stored procedure is called with a three-part name.
   EXEC SQL CALL SITE2.MYSCHEMA.P1;
   EXEC SQL ASSOCIATE LOCATORS (:LOC1, :LOC2)
            WITH PROCEDURE :HV1;
The preceding example would be invalid if host variable HV1 had contained the value MYSCHEMA.P1, a two-part name. For the example to be valid with that two-part name in host variable HV1, the current server must be the same as the location name that is specified on the CALL statement as the following statements demonstrate. This is the only condition under which the names do not have to be specified the same way and a three-part name on the CALL statement can be used with a two-part name on the ASSOCIATE LOCATORS statement.
   EXEC SQL CONNECT TO SITE2;
   EXEC SQL CALL SITE2.MYSCHEMA.P1;
   EXEC SQL ASSOCIATE LOCATORS (:LOC1, :LOC2)
            WITH PROCEDURE :HV1;