Was this helpful?
String Literals
Use single quotes to delimit embedded SQL string literals. To embed a single quote in a string literal, you must double it.
Example: Quote usage in string literals
exec sql insert
into comments (anecdotes)
 values ('single'' quote followed by double " quote');
This insert example writes the following string into the anecdotes column of the comments table:
single' quote followed by double " quote
In embedded SQL statements, the double quote and backslash need not be escaped because they have no special meaning.
To continue a string literal to additional lines, use the backslash (\) character. Any leading spaces on the next line are considered part of the string. This follows the C convention. The following message statement example shows valid use of the backslash character.
Example: Backslash usage in string literals
exec frs message 'Please correct errors found in\
      updating the database tables.'
Use C conventions in the declaration section. You must use double quotes to delimit most C strings. For example:
char *dbname = "personnel";
String Literals and Statement Strings
The Dynamic SQL statements prepare and execute immediate, both use statement strings that specify an SQL statement. To specify the statement string, use a string literal or character string variable.
Example: Statement string usage
exec sql execute immediate 'drop employee';
 str = "drop employee";
exec sql execute immediate :str;
As with regular embedded SQL string literals, the statement string delimiter is the single quote. However, quotes embedded in statement strings must conform to SQL runtime rules when the statement executes. For example, the following dynamic insert statement:
exec sql prepare s1 from
  'insert into t1 values (''single''''double"slash\ '')';
is equivalent to the statement:
str = "insert into t1 values
    ('single''double\"slash\\ ')";
 exec sql prepare s1 from :str;
In fact, the string literal that the embedded SQL/C preprocessor generates for the first example matches the string literal assigned to the variable str in the second example. The runtime evaluation of the above statement string is:
insert into t1 values ('single''double"slash\ ');
Avoid using a string literal for a statement string whenever it contains quotes or the backslash character. Instead, build the statement string using the C language's rules for string literals together with the SQL rules for the runtime evaluation of the string.
Last modified date: 06/10/2024