You are on page 1of 3

Bind Parameters: Bind references (or bind variables) are used to replace

a single value.

Specifically, bind references may be used to replace expressions in SELECT, WHERE,


GROUP BY, ORDER BY, HAVING, CONNECT BY, and START WITH clauses of queries.
Bind references may not be referenced in FROM clauses.

While creating the bind parameter we should use the : before the name of the
parameter.

Example :p_deptno

Example 1: SELECT clause In the following example, the value of DFLTCOMM


replaces null values of
COMM in the rows selected.

SELECT empno, NVL(COMM, :DFLTCOMM) COMM FROM EMP;

Example 2: WHERE clause The value of CUST is used to select a single customer.

SELECT empno,ename,sal,comm FROM EMP WHERE


deptno=:p_deptno;

Example 3: GROUP BY clause All non-aggregate expressions such as


NVL(COMM, :DFLTCOMM) in
the SELECT clause must be replicated in the GROUP BY clause.
SELECT NVL(COMM, :DFLTCOMM) COMM, SUM(SAL) TOTAL_SAL
FROM EMP
GROUP BY NVL(COMM, :DFLTCOMM);

Example 4: HAVING clause The value of MIN SAL is used to select with a minimum
total salary of
dept.

SELECT DEPTNO, SUM(SAL) TOTAL_SAL


FROM EMP
GROUP BY DEPTNO
HAVING SUM(SAL) > :MINTOTAL;

Example 5: ORDER BY clause The value of SORT is used to select either SAL or
HIREDATE as the sort
criterion. Note that this is not the same as ORDER BY 1 because
:SORT is used as a value
rather than to identify the position of an expression in the SELECT
list. Note that DECODE is
required in this example. You cannot use a bind variable in an ORDER
BY clause unless it is
with DECODE.

SELECT EMPNO, SAL, HIREDATE FROM EMP


ORDER BY DECODE(:SORT, 1, SAL, 2, HIREDATE);

Lexical Parameter: Lexical parameters will be used to replace some part


of SELECT statement.

Lexical references are placeholders for columns or parameters that you embed in a
SELECT statement. You can use lexical references to replace the clauses appearing
after SELECT, FROM, WHERE, GROUP BY, ORDER BY, HAVING, CONNECT BY, and
START WITH. Use a lexical reference when you want to substitute multiple values at
runtime.

While defining the lexical parameter we need to use &.

Ex: SELECT EMPNO,ENAME,SAL


FROM EMP
&P_WHERE;

Step 1) Go to the Object Navigator and go to the Data Model and write the sql
query.
Define the Lexical parameter P_WHERE as below.

SELECT EMPNO,ENAME,SAL
FROM EMP
&P_WHERE

Difference b/w Bind and Lexical parameters.


By using bind parameter we can replace single value
By using Lexical parameter we can replace some part of select statement.

You might also like