Before I give the answers, I’ll explain the problem fully.Bydefault, SQL*Plus will scan each line ofinputand look for an & character. Upon finding it, SQL*Plus will scan the characters after the ampersand and use those as a variable name (the variable name in the above example is Vivek). SQL*Plus will thenprompt the userfor a valuefor Vivek—like this:
SQL>insertinto test (name)
values('&Vivek');
Enter valuefor vivek: Hello World
old 1:insertinto test (name)
values('&Vivek')
new 1:insertinto test (name)
values('Hello World')
1 row created.
Here you can see how SQL*Plus turned &Vivek into Hello World. Now the question is how to stop it from doing that. The easiest method is simply to issue the SQL*Plus setdefineoff command:
SQL>setdefineoff
SQL>insertinto test (name)
values('&Vivek');
1 row created.
SQL>select*from test;
NAME
———————————————————————
Hello World
&Vivek
That prevents SQL*Plus from scanning the inputto try to find the substitution character. Another approach isto use a different substitution character:
SQL>setdefine@
SQL>insertinto test (name)
values('&Vivek @X');
Enter valuefor x: this was x
old 1:insertinto test (name)
values('&Vivek @X')
new 1:insertinto test (name)
values('&Vivek this was x')
1 row created.
In this case, the @ character is doing what the & used to do.
There are other approaches, such as avoiding the & character in your SQL:
SQL>insertinto test
values(chr(38)||'Vivek xxx');
1 row created.
SQL>select*from test
where name like'% xxx';
NAME
—————————————————————————
&Vivek xxx
Although that approach works, I’m not a fan of it, because you have tochange your SQL statement.
Yet another approach isto use a zero-length substitution variable name, which will make SQL*Plus just leave that & character alone: