What is the difference between these two variable declarations?
1: num number:='&&num';
2: variable num1 number;
Since in both cases I can reference num by using &num or &&num in other files also,
and in the case of bind variables :num1.
Moreover I have one more confusion: whether any of the below statements differ somehow, are they both valid and do they mean the same thing?
1: variable num1 number;
2: var num1 number;
You appear to have some confusion about the differences between bind variables in Oracle and substitution variables in SQL*Plus.
Let’s start with substitution variables. Substitution variables are unique to SQL*Plus and are not part of the database. They won’t work if you try to use them with JDBC, for example.
Substitution variables can only hold a piece of text. If SQL*Plus encounters a substitution variable in a line of input, it will replace the variable with its text contents:
SQL> define subvar=X SQL> select * from dual where dummy = &subvar; old 1: select * from dual where dummy = &subvar new 1: select * from dual where dummy = X select * from dual where dummy = X * ERROR at line 1: ORA-00904: "X": invalid identifierNote that SQL*Plus replaced our substitution variable with its text value with no regard for whether it gave us valid SQL. In the example above, we omitted the single quotes around
&subvarand it gave us invalid SQL, so we got an error.The lines beginning
oldandnewshow us the line we entered before and after SQL*Plus applied the substitution variables. Thenewline is the line the database tried to run.You can enable or disable the display of the
oldandnewlines usingSET VERIFY ONandSET VERIFY OFF. You can also turn the replacement of substitution variables on or off by usingSET DEFINE ONandSET DEFINE OFF.If we want to run the above query using the substitution variable, we must put quotes around it:
If
&subvarhappens to contain a string that was a valid number (e.g.5), then we can get away without using the quotes, but that’s only because taking out the text&subvarand replacing it with the text5happens to give us valid SQL.For example, suppose we have a table called
testwith the following data in it:A ---------- 1 2 3 4 5Then we can do
SQL> define subvar=5 SQL> select * from test where a = &subvar; old 1: select * from test where a = &subvar new 1: select * from test where a = 5 A ---------- 5Bind variables, on the other hand, have types. They are not simple text values. Their values are sent to the database, and the database can also set their values.
You don’t put quotes around a bind variable when you want to use it:
In the second example above, we got no rows returned because the
DUALtable has no rows with theDUMMYcolumn containing the text:bindvar.You’ll get an error if you attempt to assign a value of the wrong type to a bind variable:
Bind variables are a standard part of the database, and you can use them with JDBC or whichever method of connecting to the database you choose.
Finally,
variable num1 numberandvar num1 numberboth mean the same thing. They both define a bind variablenum1of typenumber.varis just an abbreviation forvariable.