Controlling Variable Substitution

As you use SQL*Plus, two problems may arise concerning the use of substitution variables. The first problem you are likely to encounter is that you will need to use an ampersand somewhere in your script, and you won't mean for it to be part of a substitution variable name. This common problem happens most often when you're using an ampersand in a quoted string or as part of a comment.

The second problem, which you may never encounter, is that you may want to place a substitution variable smack in the middle of a word. This is a less common problem.

SQL*Plus provides several ways to deal with these problems. A special escape character can be used whenever you need to place an ampersand in your script and have it stay there. A concatenation character is provided for those unusual cases where you want to place a substitution variable at the beginning or middle of a word. You can change the substitution character entirely if you don't like using the ampersand and want to use some other character instead. Finally, if you aren't really into writing scripts, you can turn the substitution feature completely off. Then you won't have to worry about it at all.

The Escape Character

The escape character preceding an ampersand tells SQL*Plus to leave it alone and that it is not part of a substitution variable. Consider the following DEFINE command:

DEFINE friends = "Joe & Matt"

If you executed that command, SQL*Plus would interpret "& Matt" as a substitution variable ...

Get Oracle SQL*Plus: The Definitive Guide, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.