Anda di halaman 1dari 19

SQL> DESC EMP

Name
Null?
----------------------------------------------------- --------------------------EMPNO
NOT NULL
ENAME
JOB
MGR
HIREDATE
SAL
COMM
DEPTNO
SQL>
2
3
4
5
6
7
8
KING

DECLARE
V_ENAME VARCHAR2(10);
BEGIN
SELECT ENAME INTO V_ENAME FROM EMP
WHERE EMPNO=7839;
DBMS_OUTPUT.PUT_LINE(V_ENAME);
END;
/

PL/SQL procedure successfully completed.


SQL> UPDATE EMP
2 SET ENAME='KING KHANS'
3 WHERE EMPNO=7839;
1 row updated.
SQL> COMMIT;
Commit complete.
SQL>
2
3
4
5
6
7
8
KING

DECLARE
V_ENAME VARCHAR2(10);
BEGIN
SELECT ENAME INTO V_ENAME FROM EMP
WHERE EMPNO=7839;
DBMS_OUTPUT.PUT_LINE(V_ENAME);
END;
/
KHANS

PL/SQL procedure successfully completed.


SQL> SAVE D:\EE
Created file D:\EE.sql
SQL> ALTER TABLE EMP
2 MODIFY ENAME VARCHAR2(15);
Table altered.
SQL> ALTER TABLE EMP
2 MODIFY ENAME VARCHAR2(20);
Table altered.
SQL> DESC EMP

Type
---------------NUMBER(4)
VARCHAR2(10)
VARCHAR2(9)
NUMBER(4)
DATE
NUMBER(7,2)
NUMBER(7,2)
NUMBER(2)

Name
Null?
----------------------------------------------------- --------------------------EMPNO
NOT NULL
ENAME
JOB
MGR
HIREDATE
SAL
COMM
DEPTNO
SQL>
1
2
3
4
5
6
7*
SQL>
KING

Type
---------------NUMBER(4)
VARCHAR2(20)
VARCHAR2(9)
NUMBER(4)
DATE
NUMBER(7,2)
NUMBER(7,2)
NUMBER(2)

GET D:\EE
DECLARE
V_ENAME VARCHAR2(10);
BEGIN
SELECT ENAME INTO V_ENAME FROM EMP
WHERE EMPNO=7839;
DBMS_OUTPUT.PUT_LINE(V_ENAME);
END;
/
KHANS

PL/SQL procedure successfully completed.


SQL> UPDATE EMP
2 SET ENAME='KASHIF MASOOD HASHMI'
3 WHERE EMPNO=7839;
1 row updated.
SQL> COMMIT;
Commit complete.
SQL> GET D:\EE
1 DECLARE
2
V_ENAME VARCHAR2(10);
3 BEGIN
4
SELECT ENAME INTO V_ENAME FROM EMP
5
WHERE EMPNO=7839;
6
DBMS_OUTPUT.PUT_LINE(V_ENAME);
7* END;
SQL> /
DECLARE
*
ERROR at line 1:
ORA-06502: PL/SQL: numeric or value error: character string buffer too small
ORA-06512: at line 4
SQL>
2
3
4
5
6
7
8
SQL>

DECLARE
V_ENAME SCOTT.EMP.ENAME%TYPE;
BEGIN
SELECT ENAME INTO V_ENAME FROM EMP
WHERE EMPNO=7839;
DBMS_OUTPUT.PUT_LINE(V_ENAME);
END;
.
SAVE D:\EE1

Created file D:\EE1.sql


SQL> /
KASHIF MASOOD HASHMI
PL/SQL procedure successfully completed.
SQL> UPDATE EMP
2 SET ENAME='KING'
3 WHERE EMPNO=7839;
1 row updated.
SQL> ALTER TABLE EMP
2 MODIFY ENAME VARCHAR2(10);
Table altered.
SQL> @D:\EE1
KING
PL/SQL procedure successfully completed.
SQL>
1
2
3
4
5
6
7*
SQL>

L
DECLARE
V_ENAME SCOTT.EMP.ENAME%TYPE;
BEGIN
SELECT ENAME INTO V_ENAME FROM EMP
WHERE EMPNO=7839;
DBMS_OUTPUT.PUT_LINE(V_ENAME);
END;
CL SCR

SQL> CONN HR/HR


Connected.
USER is "HR"
trimspool ON
linesize 100
pagesize 100
long 4000
verify OFF
SQL> DECLARE
2
employee_id
NUMBER(6);
3 BEGIN
4
SELECT employee_id
5
INTO
employee_id
6
FROM
employees
7
WHERE
last_name = 'Kochhar';
8 END;
9 /
PL/SQL procedure successfully completed.
SQL> DECLARE
2
employee_id
NUMBER(6) :=100;
3
last_name
varchar2(30);
4 BEGIN
5
SELECT last_name
6
INTO
last_name
7
FROM
employees
8
WHERE
employee_id = employee_id;

9
dbms_output.put_line(last_name);
10 END;
11 /
DECLARE
*
ERROR at line 1:
ORA-01422: exact fetch returns more than requested number of rows
ORA-06512: at line 5
SQL> conn scott/tiger
Connected.
USER is "SCOTT"
trimspool ON
linesize 100
pagesize 100
long 4000
verify OFF
SQL> SELECT * FROM DEPT , EMP
2 WHERE DEPTNO=DEPTNO;
WHERE DEPTNO=DEPTNO
*
ERROR at line 2:
ORA-00918: column ambiguously defined
SQL> SELECT * FROM DEPT , EMP
2 --WHERE DEPTNO=DEPTNO;
DEPTNO DNAME
LOC
EMPNO ENAME
JOB
M
GR HIREDATE
---------- -------------- ------------- ---------- ---------- --------- --------- --------SAL
COMM
DEPTNO
---------- ---------- ---------50 FINANCE
AL AIN
7369 SMITH
CLERK
79
02 17-DEC-80
800
20
50 FINANCE
98 20-FEB-81
1600
300

AL AIN

50 FINANCE
98 22-FEB-81
1250
500

AL AIN

50 FINANCE
39 02-APR-81
2975

AL AIN

50 FINANCE
98 28-SEP-81
1250
1400

AL AIN

50 FINANCE
39 01-MAY-81
2850

AL AIN

50 FINANCE

AL AIN

7499 ALLEN

SALESMAN

76

7521 WARD

SALESMAN

76

7566 JONES

MANAGER

78

7654 MARTIN

SALESMAN

76

7698 BLAKE

MANAGER

78

7782 CLARK

MANAGER

78

30

30

20

30

30

39 09-JUN-81
2450

10

50 FINANCE
66 19-APR-87
3000

AL AIN

50 FINANCE
17-NOV-81
5000

AL AIN

50 FINANCE
98 08-SEP-81
1500
0

AL AIN

50 FINANCE
88 23-MAY-87
1100

AL AIN

50 FINANCE
98 03-DEC-81
950

AL AIN

50 FINANCE
66 03-DEC-81
3000

AL AIN

50 FINANCE
82 23-JAN-82
1300

AL AIN

10 ACCOUNTING
02 17-DEC-80
800

NEW YORK

10 ACCOUNTING
98 20-FEB-81
1600
300

NEW YORK

10 ACCOUNTING
98 22-FEB-81
1250
500

NEW YORK

10 ACCOUNTING
39 02-APR-81
2975

NEW YORK

10 ACCOUNTING
98 28-SEP-81
1250
1400

NEW YORK

10 ACCOUNTING
39 01-MAY-81
2850

NEW YORK

10 ACCOUNTING
39 09-JUN-81
2450

NEW YORK

10 ACCOUNTING

NEW YORK

7788 SCOTT

ANALYST

75

7839 KING

PRESIDENT

7844 TURNER

SALESMAN

76

7876 ADAMS

CLERK

77

7900 JAMES

CLERK

76

7902 FORD

ANALYST

75

7934 MILLER

CLERK

77

7369 SMITH

CLERK

79

7499 ALLEN

SALESMAN

76

7521 WARD

SALESMAN

76

7566 JONES

MANAGER

78

7654 MARTIN

SALESMAN

76

7698 BLAKE

MANAGER

78

7782 CLARK

MANAGER

78

7788 SCOTT

ANALYST

75

20

10

30

20

30

20

10

20

30

30

20

30

30

10

66 19-APR-87
3000

20

10 ACCOUNTING
17-NOV-81
5000

NEW YORK

10 ACCOUNTING
98 08-SEP-81
1500
0

NEW YORK

10 ACCOUNTING
88 23-MAY-87
1100

NEW YORK

10 ACCOUNTING
98 03-DEC-81
950

NEW YORK

10 ACCOUNTING
66 03-DEC-81
3000

NEW YORK

10 ACCOUNTING
82 23-JAN-82
1300

NEW YORK

20 RESEARCH
02 17-DEC-80
800

DALLAS

20 RESEARCH
98 20-FEB-81
1600
300

DALLAS

20 RESEARCH
98 22-FEB-81
1250
500

DALLAS

20 RESEARCH
39 02-APR-81
2975

DALLAS

7839 KING

PRESIDENT

7844 TURNER

SALESMAN

76

7876 ADAMS

CLERK

77

7900 JAMES

CLERK

76

7902 FORD

ANALYST

75

7934 MILLER

CLERK

77

7369 SMITH

CLERK

79

7499 ALLEN

SALESMAN

76

7521 WARD

SALESMAN

76

7566 JONES

MANAGER

78

10

30

20

30

20

10

20

30

30

20

DEPTNO DNAME
LOC
EMPNO ENAME
JOB
M
GR HIREDATE
---------- -------------- ------------- ---------- ---------- --------- --------- --------SAL
COMM
DEPTNO
---------- ---------- ---------20 RESEARCH
98 28-SEP-81
1250
1400

DALLAS

20 RESEARCH
39 01-MAY-81
2850

DALLAS

20 RESEARCH
39 09-JUN-81

DALLAS

7654 MARTIN

SALESMAN

76

7698 BLAKE

MANAGER

78

7782 CLARK

MANAGER

78

30

30

2450

10

20 RESEARCH
66 19-APR-87
3000

DALLAS

20 RESEARCH
17-NOV-81
5000

DALLAS

20 RESEARCH
98 08-SEP-81
1500
0

DALLAS

20 RESEARCH
88 23-MAY-87
1100

DALLAS

20 RESEARCH
98 03-DEC-81
950

DALLAS

20 RESEARCH
66 03-DEC-81
3000

DALLAS

20 RESEARCH
82 23-JAN-82
1300

DALLAS

30 SALES
02 17-DEC-80
800

CHICAGO

30 SALES
98 20-FEB-81
1600
300

CHICAGO

30 SALES
98 22-FEB-81
1250
500

CHICAGO

30 SALES
39 02-APR-81
2975

CHICAGO

30 SALES
98 28-SEP-81
1250
1400

CHICAGO

30 SALES
39 01-MAY-81
2850

CHICAGO

30 SALES
39 09-JUN-81
2450

CHICAGO

30 SALES
66 19-APR-87

CHICAGO

7788 SCOTT

ANALYST

75

7839 KING

PRESIDENT

7844 TURNER

SALESMAN

76

7876 ADAMS

CLERK

77

7900 JAMES

CLERK

76

7902 FORD

ANALYST

75

7934 MILLER

CLERK

77

7369 SMITH

CLERK

79

7499 ALLEN

SALESMAN

76

7521 WARD

SALESMAN

76

7566 JONES

MANAGER

78

7654 MARTIN

SALESMAN

76

7698 BLAKE

MANAGER

78

7782 CLARK

MANAGER

78

7788 SCOTT

ANALYST

75

20

10

30

20

30

20

10

20

30

30

20

30

30

10

3000

20

30 SALES
17-NOV-81
5000

CHICAGO

30 SALES
98 08-SEP-81
1500

CHICAGO

7839 KING

PRESIDENT

7844 TURNER

SALESMAN

76

7876 ADAMS

CLERK

77

7900 JAMES

CLERK

76

7902 FORD

ANALYST

75

7934 MILLER

CLERK

77

7369 SMITH

CLERK

79

7499 ALLEN

SALESMAN

76

7521 WARD

SALESMAN

76

7566 JONES

MANAGER

78

7654 MARTIN

SALESMAN

76

7698 BLAKE

MANAGER

78

7782 CLARK

MANAGER

78

7788 SCOTT

ANALYST

75

10

30

30 SALES
88 23-MAY-87
1100

CHICAGO

30 SALES
98 03-DEC-81
950

CHICAGO

30 SALES
66 03-DEC-81
3000

CHICAGO

30 SALES
82 23-JAN-82
1300

CHICAGO

40 OPERATIONS
02 17-DEC-80
800

BOSTON

40 OPERATIONS
98 20-FEB-81
1600
300

BOSTON

40 OPERATIONS
98 22-FEB-81
1250
500

BOSTON

40 OPERATIONS
39 02-APR-81
2975

BOSTON

40 OPERATIONS
98 28-SEP-81
1250
1400

BOSTON

40 OPERATIONS
39 01-MAY-81
2850

BOSTON

40 OPERATIONS
39 09-JUN-81
2450

BOSTON

40 OPERATIONS
66 19-APR-87

BOSTON

20

30

20

10

20

30

30

20

30

30

10

DEPTNO DNAME
LOC
EMPNO ENAME
JOB
M
GR HIREDATE
---------- -------------- ------------- ---------- ---------- --------- --------

-- --------SAL
COMM
DEPTNO
---------- ---------- ---------3000
20
40 OPERATIONS
17-NOV-81
5000

BOSTON

40 OPERATIONS
98 08-SEP-81
1500
0

BOSTON

40 OPERATIONS
88 23-MAY-87
1100

BOSTON

40 OPERATIONS
98 03-DEC-81
950

BOSTON

40 OPERATIONS
66 03-DEC-81
3000

BOSTON

40 OPERATIONS
82 23-JAN-82
1300

BOSTON

7839 KING

PRESIDENT

7844 TURNER

SALESMAN

76

7876 ADAMS

CLERK

77

7900 JAMES

CLERK

76

7902 FORD

ANALYST

75

7934 MILLER

CLERK

77

10

30

20

30

20

10

70 rows selected.
SQL> SELECT * FROM DEPT , EMP
2 WHERE DEPT.DEPTNO=10;
DEPTNO DNAME
LOC
EMPNO ENAME
JOB
M
GR HIREDATE
---------- -------------- ------------- ---------- ---------- --------- --------- --------SAL
COMM
DEPTNO
---------- ---------- ---------10 ACCOUNTING
NEW YORK
7369 SMITH
CLERK
79
02 17-DEC-80
800
20
10 ACCOUNTING
98 20-FEB-81
1600
300

NEW YORK

10 ACCOUNTING
98 22-FEB-81
1250
500

NEW YORK

10 ACCOUNTING
39 02-APR-81
2975

NEW YORK

10 ACCOUNTING
98 28-SEP-81
1250
1400

NEW YORK

7499 ALLEN

SALESMAN

76

7521 WARD

SALESMAN

76

7566 JONES

MANAGER

78

7654 MARTIN

SALESMAN

76

30

30

20

30

10 ACCOUNTING
39 01-MAY-81
2850

NEW YORK

10 ACCOUNTING
39 09-JUN-81
2450

NEW YORK

10 ACCOUNTING
66 19-APR-87
3000

NEW YORK

10 ACCOUNTING
17-NOV-81
5000

NEW YORK

10 ACCOUNTING
98 08-SEP-81
1500
0

NEW YORK

10 ACCOUNTING
88 23-MAY-87
1100

NEW YORK

10 ACCOUNTING
98 03-DEC-81
950

NEW YORK

10 ACCOUNTING
66 03-DEC-81
3000

NEW YORK

10 ACCOUNTING
82 23-JAN-82
1300

NEW YORK

7698 BLAKE

MANAGER

78

7782 CLARK

MANAGER

78

7788 SCOTT

ANALYST

75

7839 KING

PRESIDENT

7844 TURNER

SALESMAN

76

7876 ADAMS

CLERK

77

7900 JAMES

CLERK

76

7902 FORD

ANALYST

75

7934 MILLER

CLERK

77

30

10

20

10

30

20

30

20

10

14 rows selected.
SQL> CL SCR
SQL> DECLARE
2
employee_id
NUMBER(6) :=100;
3
last_name
varchar2(30);
4 BEGIN
5
SELECT last_name
6
INTO
last_name
7
FROM
employees
8
WHERE
employee_id = employee_id;
9
dbms_output.put_line(last_name);
10 END;
11 .
SQL> CONN HR/HR
Connected.
USER is "HR"
trimspool ON
linesize 100
pagesize 100
long 4000

verify OFF
SQL> DECLARE
2
V_employee_id
NUMBER(6) :=100;
3
last_name
varchar2(30);
4 BEGIN
5
SELECT last_name
6
INTO
last_name
7
FROM
employees
8
WHERE
employee_id = V_employee_id;
9
dbms_output.put_line(last_name);
10 END;
11 /
King
PL/SQL procedure successfully completed.
SQL> CONN SCOTT/TIGER
Connected.
USER is "SCOTT"
trimspool ON
linesize 100
pagesize 100
long 4000
verify OFF
SQL> CL SCR
SQL> -- BIND VARIABLE
SQL> VAR B_DEPTNO NUMBER;
SQL> VAR
variable b_deptno
datatype NUMBER
SQL> HELP VAR
VARIABLE
-------Declares a bind variable that can be referenced in PL/SQL, or
lists the current display characteristics for a single variable
or all variables.
VAR[IABLE] [variable [type]]
where type represents one of the following:
NUMBER
CHAR
NCHAR
NCHAR (n)
NVARCHAR2 (n) CLOB
REFCURSOR
BINARY_FLOAT
SQL>
2
3
4
5
6
7
20

CHAR (n [CHAR|BYTE])
VARCHAR2 (n [CHAR|BYTE])
NCLOB
BINARY_DOUBLE

BEGIN
SELECT DEPTNO INTO :B_DEPTNO
FROM EMP
WHERE EMPNO=7788;
DBMS_OUTPUT.PUT_LINE(:B_DEPTNO);
END;
/

PL/SQL procedure successfully completed.

SQL> PRINT B_DEPTNO


B_DEPTNO
---------20
SQL> SELECT * FROM EMP NATURAL JOIN DEPT
2 WHERE DEPTNO=:B_DEPTNO;
DEPTNO
EMPNO ENAME
JOB
MGR HIREDATE
SAL
COMM DNAME
---------- ---------- ---------- --------- ---------- --------- ---------- --------- -------------LOC
------------20
7369 SMITH
CLERK
7902 17-DEC-80
800
RESEARCH
DALLAS
20
RESEARCH

7566 JONES

MANAGER

7839 02-APR-81

2975

7788 SCOTT

ANALYST

7566 19-APR-87

3000

7876 ADAMS

CLERK

7788 23-MAY-87

1100

7902 FORD

ANALYST

7566 03-DEC-81

3000

DALLAS
20
RESEARCH
DALLAS
20
RESEARCH
DALLAS
20
RESEARCH
DALLAS
SQL> CL SCR
SQL>
2
3
4
5
6

BEGIN
SELECT DEPTNO INTO :B_DEPTNO
FROM EMP
WHERE EMPNO=7839;
END;
/

PL/SQL procedure successfully completed.


SQL> PRINT B_DEPTNO
B_DEPTNO
---------10
SQL> SHOW AUTOPRINT
autoprint OFF
SQL> SET AUTOPRINT ON
SQL> L
1 BEGIN
2 SELECT DEPTNO INTO :B_DEPTNO

3
4
5*
SQL>

FROM EMP
WHERE EMPNO=7839;
END;
/

PL/SQL procedure successfully completed.


B_DEPTNO
---------10
SQL> SET AUTOPRINT OFF
SQL> DECLARE
2 V_ENAME EMP.ENAME%TYPE:=INITCAP('SCOTT');
3 -- V_CNTR NUMBER:=COUNT(EMPNO);
4 BEGIN
5 DBMS_OUTPUT.PUT_LINE(V_ENAME);
6 END;
7 /
Scott
PL/SQL procedure successfully completed.
SQL> DECLARE
2 --V_ENAME EMP.ENAME%TYPE:=INITCAP('SCOTT');
3 V_CNTR NUMBER:=COUNT(EMPNO);
4 BEGIN
5 DBMS_OUTPUT.PUT_LINE(V_CNTR);
6 END;
7 /
V_CNTR NUMBER:=COUNT(EMPNO);
*
ERROR at line 3:
ORA-06550: line 3, column 17:
PLS-00204: function or pseudo-column 'COUNT' may be used inside a SQL statement
only
ORA-06550: line 3, column 9:
PL/SQL: Item ignored
ORA-06550: line 5, column 23:
PLS-00320: the declaration of the type of this expression is incomplete or malfo
rmed
ORA-06550: line 5, column 2:
PL/SQL: Statement ignored
SQL>
SQL>
2
3
4
5
6
7
8
14

-- USING GROUP FUNCTIONS


DECLARE
--V_ENAME EMP.ENAME%TYPE:=INITCAP('SCOTT');
V_CNTR NUMBER;
BEGIN
SELECT COUNT(*) INTO V_CNTR FROM EMP;
DBMS_OUTPUT.PUT_LINE(V_CNTR);
END;
/

PL/SQL procedure successfully completed.


SQL> CL SCR

SQL>
2
3
for
4
5
6
7
8
9
62

DECLARE
v_desc_size INTEGER(5);
v_prod_description VARCHAR2(70):='You can use this product with your radios
higher frequency';
BEGIN
-- get the length of the string in prod_description
v_desc_size:= LENGTH(v_prod_description);
DBMS_OUTPUT.PUT_LINE(V_DESC_SIZE);
END;
/

PL/SQL procedure successfully completed.


SQL> DECLARE
2 --v_desc_size INTEGER(5);
3 --v_prod_description VARCHAR2(70):='You can use this product with your radi
os for higher frequency';
4 V_TENURE NUMBER;
5 BEGIN
6 -- get the length of the string in prod_description
7 --v_desc_size:= LENGTH(v_prod_description);
8 --DBMS_OUTPUT.PUT_LINE(V_DESC_SIZE);
9 v_tenure:= MONTHS_BETWEEN (SYSDATE, ADD_MONTHS(SYSDATE,-2));
10 &D(V_TENURE);
11 END;
12 .
SQL> DECLARE
2 --v_desc_size INTEGER(5);
3 --v_prod_description VARCHAR2(70):='You can use this product with your radi
os for higher frequency';
4 V_TENURE NUMBER;
5 BEGIN
6 -- get the length of the string in prod_description
7 --v_desc_size:= LENGTH(v_prod_description);
8 --DBMS_OUTPUT.PUT_LINE(V_DESC_SIZE);
9 v_tenure:= TRUNC(MONTHS_BETWEEN (SYSDATE, ADD_MONTHS(SYSDATE,-2)),0);
10 &D(V_TENURE);
11 END;
12 /
2
PL/SQL procedure successfully completed.
SQL> CLS CR
SP2-0042: unknown command "CLS CR" - rest of line ignored.
SQL> CL SCR
SQL> -- USING OF SEQUENCE BEFORE 11G
SQL> SELECT MAX(EMPNO) FROM EMP;
MAX(EMPNO)
---------7934
SQL>
2
3
4

CREATE SEQUENCE EMP_SEQ


START WITH 7934
INCREMENT BY 1
MAXVALUE 9999

5 CACHE 100
6 NOCYCLE;
Sequence created.
SQL> SELECT EMP_SEQ.NEXTVAL FROM DUAL;
NEXTVAL
---------7934
SQL>
2
3
4
5
6
7
7935

DECLARE
V_EMPNO NUMBER;
BEGIN
SELECT EMP_SEQ.NEXTVAL INTO V_EMPNO FROM DUAL;
&D(V_EMPNO);
END;
/

PL/SQL procedure successfully completed.


SQL>
SQL>
2
3
4
5
6
7936

-- IN 11G
DECLARE
V_EMPNO NUMBER:=EMP_SEQ.NEXTVAL;
BEGIN
&D(V_EMPNO);
END;
/

PL/SQL procedure successfully completed.


SQL> CL SCR
SQL> -- IMPLICIT AND EXPLICIT CONVERSATION
SQL> DECLARE
2 V_DATE_1 DATE:='14-DEC-13';
3 V_DATE_2 DATE:='FEB,2013-29';
4 BEGIN
5 &D(V_DATE_1||CHR(10)||V_DATE_2);
6 END;
7 /
DECLARE
*
ERROR at line 1:
ORA-01858: a non-numeric character was found where a numeric was expected
ORA-06512: at line 3
SQL> DECLARE
2 V_DATE_1 DATE:='14-DEC-13';
3 V_DATE_2 DATE:=TO_DATE('FEB,2013-29','MON,RRRR-DD');
4 BEGIN
5 &D(V_DATE_1||CHR(10)||V_DATE_2);
6 END;
7 /
DECLARE
*
ERROR at line 1:

ORA-01839: date not valid for month specified


ORA-06512: at line 3
SQL> DECLARE
2 V_DATE_1 DATE:='14-DEC-13';
3 V_DATE_2 DATE:=TO_DATE('MAR,2013-29','MON,RRRR-DD');
4 BEGIN
5 &D(V_DATE_1||CHR(10)||V_DATE_2);
6 END;
7 /
14-DEC-13
29-MAR-13
PL/SQL procedure successfully completed.
SQL> DECLARE
2 V_DATE_1 DATE:='14-DEC-13';
3 V_DATE_2 DATE:=TO_DATE('MAR,2013-29','MON,RRRR-DD');
4 BEGIN
5 &D(V_DATE_1||CHR(10)||V_DATE_2||CHR(10)||TO_CHAR(V_DATE_1,'MONTH,RRRR DD')
);
6 END;
7 /
14-DEC-13
29-MAR-13
DECEMBER ,2013 14
PL/SQL procedure successfully completed.
SQL> CL SCR
SQL> -- NESTED BLOCK
SQL> DECLARE
2 v_outer_variable VARCHAR2(20):='GLOBAL VARIABLE';
3 BEGIN
4
DECLARE
5
v_inner_variable VARCHAR2(20):='LOCAL VARIABLE';
6
BEGIN
7
DBMS_OUTPUT.PUT_LINE(v_inner_variable);
8
DBMS_OUTPUT.PUT_LINE(v_outer_variable);
9
END;
10 DBMS_OUTPUT.PUT_LINE(v_outer_variable);
11 END;
12 /
LOCAL VARIABLE
GLOBAL VARIABLE
GLOBAL VARIABLE
PL/SQL procedure successfully completed.
SQL>
SQL>
2
3
4
5
6
7
8

-- WHAT HAPPEN IF WE USED INNER VARIABLE AT OUTSIDE


DECLARE
v_outer_variable VARCHAR2(20):='GLOBAL VARIABLE';
BEGIN
DECLARE
v_inner_variable VARCHAR2(20):='LOCAL VARIABLE';
BEGIN
DBMS_OUTPUT.PUT_LINE(v_inner_variable);
DBMS_OUTPUT.PUT_LINE(v_outer_variable);

9
END;
10 DBMS_OUTPUT.PUT_LINE(v_inner_variable);
11 END;
12 /
DBMS_OUTPUT.PUT_LINE(v_inner_variable);
*
ERROR at line 10:
ORA-06550: line 10, column 23:
PLS-00201: identifier 'V_INNER_VARIABLE' must be declared
ORA-06550: line 10, column 2:
PL/SQL: Statement ignored
SQL> CL SCR
SQL> -- VARIABLE STATES
SQL> DECLARE
2 v_father_name VARCHAR2(20):='Patrick';
3 v_date_of_birth DATE:='20-Apr-1972';
4 BEGIN
5
DECLARE
6
v_child_name VARCHAR2(20):='Mike';
7
v_date_of_birth DATE:='12-Dec-2002';
8
BEGIN
9
DBMS_OUTPUT.PUT_LINE('Father''s Name: '||v_father_name);
10
DBMS_OUTPUT.PUT_LINE('Date of Birth: '||v_date_of_birth);
11
DBMS_OUTPUT.PUT_LINE('Child''s Name: '||v_child_name);
12
END;
13 DBMS_OUTPUT.PUT_LINE('Date of Birth: '||v_date_of_birth);
14 END;
15 /
Father's Name: Patrick
Date of Birth: 12-DEC-02
Child's Name: Mike
Date of Birth: 20-APR-72
PL/SQL procedure successfully completed.
SQL> DECLARE
2 v_father_name VARCHAR2(20):='Patrick';
3 v_date_of_birth DATE:='20-Apr-1972';
4 BEGIN
5
DECLARE
6
v_child_name VARCHAR2(20):='Mike';
7
v_date_of_birth DATE:='12-Dec-2002';
8
BEGIN
9
DBMS_OUTPUT.PUT_LINE('Father''s Name: '||v_father_name);
10
DBMS_OUTPUT.PUT_LINE('Father''s Date of Birth: '||v_date_of_birth);
11
DBMS_OUTPUT.PUT_LINE('Child''s Name: '||v_child_name);
12
DBMS_OUTPUT.PUT_LINE('Child''s Date of Birth: '||v_date_of_birth);
13
END;
14 END;
15 /
Father's Name: Patrick
Father's Date of Birth: 12-DEC-02
Child's Name: Mike
Child's Date of Birth: 12-DEC-02
PL/SQL procedure successfully completed.

SQL> cl scr
SQL> -- with qualifier
SQL> <<a1>>
2 DECLARE
3 v_father_name VARCHAR2(20):='Patrick';
4 v_date_of_birth DATE:='20-Apr-1972';
5 BEGIN
6
DECLARE
7
v_child_name VARCHAR2(20):='Mike';
8
v_date_of_birth DATE:='12-Dec-2002';
9
BEGIN
10
DBMS_OUTPUT.PUT_LINE('Father''s Name: '||v_father_name);
11
DBMS_OUTPUT.PUT_LINE('Father''s Date of Birth: '||a1.v_date_of_birth);
12
DBMS_OUTPUT.PUT_LINE('Child''s Name: '||v_child_name);
13
DBMS_OUTPUT.PUT_LINE('Child''s Date of Birth: '||v_date_of_birth);
14
END;
15 END;
16 /
Father's Name: Patrick
Father's Date of Birth: 20-APR-72
Child's Name: Mike
Child's Date of Birth: 12-DEC-02
PL/SQL procedure successfully completed.
SQL> <<a1>>
2 DECLARE
3 v_father_name VARCHAR2(20):='Patrick';
4 v_date_of_birth DATE:='20-Apr-1972';
5 BEGIN
6
<<b1>>
7
DECLARE
8
v_child_name VARCHAR2(20):='Mike';
9
v_date_of_birth DATE:='12-Dec-2002';
10
BEGIN
11
DBMS_OUTPUT.PUT_LINE('Father''s Name: '||v_father_name);
12
DBMS_OUTPUT.PUT_LINE('Father''s Date of Birth: '||a1.v_date_of_birth);
13
DBMS_OUTPUT.PUT_LINE('Child''s Name: '||v_child_name);
14
DBMS_OUTPUT.PUT_LINE('Child''s Date of Birth: '||v_date_of_birth);
15
END;
16
DBMS_OUTPUT.PUT_LINE('Child''s Date of Birth: '||b1.v_date_of_birth);
17 END;
18 /
DBMS_OUTPUT.PUT_LINE('Child''s Date of Birth: '||b1.v_date_of_birth);
*
ERROR at line 16:
ORA-06550: line 16, column 56:
PLS-00219: label 'B1' reference is out of scope
ORA-06550: line 16, column 4:
PL/SQL: Statement ignored
SQL> cl scr
SQL> BEGIN <<outer>>
2 DECLARE
3
v_sal
NUMBER(7,2) := 60000;
4
v_comm
NUMBER(7,2) := v_sal * 0.20;
5
v_message VARCHAR2(255) := ' eligible for commission';

6
7
8
9
10
11
12
13
14
15
16
17
18

BEGIN
DECLARE
v_sal
NUMBER(7,2) := 50000;
v_comm
NUMBER(7,2) := 0;
v_total_comp NUMBER(7,2) := v_sal + v_comm;
BEGIN
v_message := 'CLERK not'||v_message;
outer.v_comm := v_sal * 0.30;
END;
v_message := 'SALESMAN'||v_message;
END;
END outer;
/

PL/SQL procedure successfully completed.


SQL> BEGIN <<outer>>
2 DECLARE
3
v_sal
NUMBER(7,2) := 60000;
4
v_comm
NUMBER(7,2) := v_sal * 0.20;
5
v_message VARCHAR2(255) := ' eligible for commission';
6 BEGIN
7
DECLARE
8
v_sal
NUMBER(7,2) := 50000;
9
v_comm
NUMBER(7,2) := 0;
10
v_total_comp NUMBER(7,2) := v_sal + v_comm;
11
BEGIN
12
v_message := 'CLERK not'||v_message;
13
outer.v_comm := v_sal * 0.30;
14
END;
15 v_message := 'SALESMAN'||v_message;
16 dbms_output.put_line(v_message);
17 END;
18 END outer;
19 /
SALESMANCLERK not eligible for commission
PL/SQL procedure successfully completed.
SQL> cl scr
SQL> SPOOL OFF