LITERALS Vs BIND VARIABLES WITH CURSOR - SHARING
LITERALS Vs BIND VARIABLES WITH CURSOR - SHARING
LITERALS Vs BIND VARIABLES WITH CURSOR - SHARING
VARIBLE
VARIABLE_NAME DATATYPE;
val1 number(6);
VARIABLE_NAME := value;
val1 := 40; or
val1 number := 40
SIMPLE PL/SQL
SQL>DECLARE
v_cnt NUMBER; -- Define a number variable
BEGIN
v_cnt := 0;
WHILE v_cnt < 10 LOOP
v_cnt := v_cnt + 1;
END LOOP;
END ;
LITERALS
SQL> DECLARE
BEGIN
DBMS_OUTPUT.PUT_LINE('v_var1: '||v_var1);
DBMS_OUTPUT.PUT_LINE('v_var2: '||v_var2);
DBMS_OUTPUT.PUT_LINE('v_var3: '||v_var3);
END;
SQL> DECLARE
v_var1 VARCHAR2(20);
v_var2 VARCHAR2(6);
v_var3 NUMBER(5,3);
***********************************************
BEGIN
v_var2 := '12.345';
v_var3 := 12.345;
DBMS_OUTPUT.PUT_LINE('v_var1: '||v_var1);
DBMS_OUTPUT.PUT_LINE('v_var2: '||v_var2);
DBMS_OUTPUT.PUT_LINE('v_var3: '||v_var3);
END;
v_var1: 12.345
v_var1: 12.345
'string literal' and '12.345', are string literals because they are enclosed in
single quotes. The third value, 12.345, is a numeric literal.
It prompts the user to enter a value for that variable then it tries
to replace it with the value. Substitution variable may declared with
"define". they have no data type.
**********************
Simple Example
Declare
SQL>define var1 = sam
SQL>Select '&var1' from dual;
RESU
var1
*************************************************
The "&&" will actually define the variable similarly to what the DEFINE
command or OLD_VALUE/NEW_VALUE clauses of a COLUMN
statement would have done.
POINTS TO NOTE :
The SET DEFINE statement is used to specify the value of the non –
alphanumeric character used to prefix substitution variables. The
default value is the apersand (&).
RESULT
THIS IS ORACLE
RESULT
&HELLO
If SET DEFINE OFF , then it will not ask the variable value.
POINTS TO NOTE
BIND VARIABLES
:ret_val := 4;
END;
/
PL/SQL procedure successfully completed.
HR>PRINT ret_val;
RET_VAL
*******************************************
POINTS TO NOTE
Bind variables are used in SQL and PL/SQL statements for holding
data. They are commonly used in SQL statements to optimize
statement performance. A statement with a BIND VARIABLE may
be re-executed multiple times without needing to be re-parsed.
When using bind variables database server binds values with the
variable place holder but submitted SQL query text is being same.
Hard Parses are really bad. Avoiding HARD PARSES can reduce
overall run time of given SQL statement more than once. Oracle
considers identical SQL statements to be the same, hard parses will
be performed for both of these following statements.
Above two SQL statements above are not identical This will result in two
different cursors being stored in the Library Cache.
************************************
We can avoid this problem through the use of bind variables used
within your SQL and PL/SQL code.
dbms_output.put_line(v_display);
END;
/
sam
PL/SQL can be executed many times with different values for :v_value, and
hard parsed once. This approach not only performs better but also is a much
more scalable way of writing SQL and PL/SQL. Multiple concurrent statements
will be able to be executed.
Parsing steps and the use of bind variables are the most important
things to understand for developers variable .
HR>BEGIN
2 select name into :b2 from tab1 where no=:b1;
3 END;
4 /
PL/SQL procedure successfully completed.
HR>print :b2;
B2
ford
POINTS TO NOTE :
Variable and exec are SQL Plus commands and are not part of the pl/sql
language. We can't use them as part of PL/SQL.
HR>var n number;
HR>print n;
N
1
SET VERIFY
*******************************************
CURSOR_SHARING
Oracle offers the cursor_sharing parameter starting with oracle 8i. When
CURSOR SHARING is enabled setting of parameter CURSOR_SHARING to
FORCE/ EXACT/SIMILAR. SIMILAR is only available above oracle 8i. Oracle
Oracle database can take a query of the form SELECT * FROM TABLE
WHERE COL = 'literal' and replace the 'literal' with a bind value - so the
predicate will become WHERE COL = :"SYS_B_0" . This permits the reuse of
the generated query plan , to better utilization of the shared pool and a
reduction of hard parses performed by the system.
CURSOR_SHARING = FORCE
This setting rewrites the query. Share the plan (forcibly) of a SQL.
If the text of SQL matches (except the literal values of the SQL) in
shared pool. ‘CURSOR_SHARING to allows similar statements to share
SQL. Two (2) SQL statements which differ only by a literal value).
CURSOR_SHARING = SIMILAR
This is very tricky one ! This setting also rewrites the query,
replacing the literals with bind variables, but can setup different
plans for different bind variable combinations. SIMILAR might reduce
the number of plans generated because multiple plans may be
generated, the setting of SIMILAR may or may not reduce the number
of actual plans to observe in the shared pool.
EXACT.txt
that implies that the database is forced to hard-parse They not only
consumes a lot of CPU cycles but also leads to decreased scalability.
FORCE.txt
***************************************************
SIMILAR causes statements that may differ in some literals, but are
SIMILAR.txt
*****************************
DIFFERENCE BETWEEN SIMILAR AND FORCE
know the values of the bind variables. This may positively or negatively
impact the database performance.