Example of a delimited identifier

The following figure shows a delimited identifier that specifies characters that are not alphabetic in both a cursor name and a statement ID.
Figure 1: Using delimited identifiers for a cursor name
EXEC SQL BEGIN DECLARE SECTION;
   char curname1[10];
   char stmtname[10];
EXEC SQL END DECLARE SECTION;

stcopy("%#!", curname1);
stcopy("(_=", stmtname);
EXEC SQL prepare :stmtname from 
   'select customer_num from customer';
EXEC SQL declare :curname1 cursor for $stmtname;
EXEC SQL open :curname;
In the previous figure, you can also list the cursor name or statement ID directly in the SQL statement. For example, the following PREPARE statement is also valid (with DELIMIDENT set):
EXEC SQL prepare "%#!" from 
   'select customer_num from customer';

If you set DELIMIDENT, the SELECT string in the preceding PREPARE statement must be enclosed in quotation marks for the preprocessor to treat it as a string. If you enclose the statement in double quotation marks, the preprocessor treats it as an identifier.

To declare a cursor name that contains a double quotation mark, you must use escape characters in the delimited identifier string. For example, to use the string "abc" as a cursor name, you must escape the initial quotation mark in the cursor name:
EXEC SQL BEGIN DECLARE SECTION;
   char curname2[10];
   char stmtname[10];
EXEC SQL END DECLARE SECTION;

stcopy("\"abc\"", curname2);
EXEC SQL declare :curname2 cursor for :stmtname;
In the preceding example, the cursor name requires several escape characters:
  • The backslash (\) is the C escape character. You need it to escape the double quotation mark.

    Without the escape character, the C compiler would interpret the double quotation mark as the end of the string.

  • The cursor name must contain two double quotation marks.

    The first double quotation mark escapes the double quotation mark and the second double quotation mark is the literal double quotation mark. The ANSI standard states that you cannot use a backslash to escape quotation marks. Instead, you must escape the quotation mark in the cursor name with another quotation mark.

The following table shows the string that contains the cursor name as it is processed.
Table 1. Escaped cursor name string as it is processed
Processor After processing
ESQL/C preprocessor \"\"abc
C Compiler ""abc
ANSI-compliant database server "abc