Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I ignore ampersands in a SQL script running from SQL Plus?

I have a SQL script that creates a package with a comment containing an ampersand (&). When I run the script from SQL Plus, I am prompted to enter a substitute value for the string starting with &. How do I disable this feature so that SQL Plus ignores the ampersand?

like image 814
JoshL Avatar asked Sep 22 '08 23:09

JoshL


People also ask

How do you escape ampersand in SQL query?

If you set escape on, it uses an esape using the backslash. So, backslash ampersand will show you an ampersand. Hope this helps.

What does double ampersand mean in SQL?

Using a double ampersand in front of a substitution variable tells SQL*Plus to define that variable for the duration of the session. This is useful when you need to reference a variable several times in one script, because you don't usually want to prompt the user separately for each occurrence.

What is the use of double ampersand && in SQL queries?

Both single ampersand (&) and double ampersand (&&) can prefix a substitution variable name in a statement. SQL*Plus pre-processes the statement and substitutes the variable's value. The statement is then executed.

Which is an iSQL * Plus command in SQL?

SQL*Plus is a command-line tool that provides access to the Oracle RDBMS. SQL*Plus enables you to: Enter SQL*Plus commands to configure the SQL*Plus environment. Startup and shutdown an Oracle database.


4 Answers

This may work for you:

set define off

Otherwise the ampersand needs to be at the end of a string,

'StackOverflow &' || ' you'

EDIT: I was click-happy when saving... This was referenced from a blog.

like image 175
Austin Salonen Avatar answered Oct 14 '22 07:10

Austin Salonen


If you sometimes use substitution variables you might not want to turn define off. In these cases you could convert the ampersand from its numeric equivalent as in || Chr(38) || or append it as a single character as in || '&' ||.

like image 30
Leigh Riffel Avatar answered Oct 14 '22 06:10

Leigh Riffel


I resolved with the code below:

set escape on

and put a \ beside & in the left 'value_\&_intert'

Att

like image 14
Cauca Avatar answered Oct 14 '22 06:10

Cauca


You can set the special character, which is looked for upon execution of a script, to another value by means of using the SET DEFINE <1_CHARACTER>

By default, the DEFINE function itself is on, and it is set to &

It can be turned off - as mentioned already - but it can be avoided as well by means of setting it to a different value. Be very aware of what sign you set it to. In the below example, I've chose the # character, but that choice is just an example.

SQL> select '&var_ampersand #var_hash' from dual;
Enter value for var_ampersand: a value

'AVALUE#VAR_HASH'
-----------------
a value #var_hash

SQL> set define #
SQL> r
  1* select '&var_ampersand #var_hash' from dual
Enter value for var_hash: another value

'&VAR_AMPERSANDANOTHERVALUE'
----------------------------
&var_ampersand another value

SQL>
like image 9
tvCa Avatar answered Oct 14 '22 06:10

tvCa