Sie sind auf Seite 1von 22

AUTOCOMMIT in SQL- Simple and Useful

DML Statements or Data Manipulation Language statements will not be


committed unless they are committed explicitly, But if you use any data
definition language (DDL) statement before or after , then Oracle Database
issues an implicit COMMIT.
There is a provision in Oracle to commit every DML Transaction automatically
once its run. This is called AUTOCOMMIT. We can also turn AUTOCOMMIT on
and off based on our requirement.
Below are commands to check for AUTOCOMMIT.

When AUTOCOMMIT IMMEDIATE is activated all the DML statements


executed afterward will be treated as a transaction and will be committed to
the Database.
We can also limit the number of AUTOCOMMIT to number that we want
using the below command.

PROCEDURAL PARAMETER MODES IN PLSQL


There are three modes, which tells the compiler, how to treat the
actual parameters, used during call.

1.

They are IN, OUT AND IN OUT.

2.The default is IN PARAMETER (calls are by reference)

Take values from calling environment.

Behaves like constant [during exec, sub-prog cant change value]

Actual parameter can be literals, expressions, or constants.

Can be assigned default values.

3.

OUT PARAMETER (calls are by value)

Must be specified, returns a value to calling environment.

Behaves like uninitialized variable.

a)Uninitialized variables hold nulls.


b)During exec, sub-program shall change value.
c)During exec, if not changed a null is returned to caller.

Actual parameter must be a variable. [Cannot be constant]

Cannot be assigned default values.

4.

IN OUT PARAMETER (calls are by value.)

Must be specified,
a)Shall take values from caller / returns a value to caller.

Behaves like initialized variable.


a)Uninitialized variables hold nulls.
b)During exec, sub-program shall change value.
c)During exec, if not changed a old is returned to caller.
d)Actual parameter must be a variable. [cannot be constant]

Actual parameter must be a variable. [cannot be constant]

Cannot be assigned default values.

Note:To improve performance out and in out can be given a compiler


hint no copy, requesting the compiler to pass by reference.
How to Create a Procedure with both IN and OUT Parameter

In this post I will try to explain how to create a procedure with both Input and output
Parameter

Out parameter: The out parameter mode is used to return values to the caller of
the subprogram.
In parameter: The in parameter mode is used to pass values to the subprogram.

First Lets Create a table order_details


SQL> create table ORDER_DETAILS
2

(ordno varchar2(12),

itemcode varchar2(12),

qtyord number,

qtydeld number);

Table created.

SQL> INSERT INTO

ORDER_DETAILS

VALUES('1','ITEM4',300,300);

ORDER_DETAILS

VALUES('5','ITEM1',200,200);

ORDER_DETAILS

VALUES('7','ITEM1',300,200);

1 row created.

SQL> INSERT INTO

1 row created.

SQL> INSERT INTO

1 row created.

SQL> INSERT INTO

ORDER_DETAILS

VALUES('13','ITEM4', 20, 20);

ORDER_DETAILS

VALUES('31','ITEM2',300,300);

1 row created.

SQL> INSERT INTO

1 row created.

Below is the procedure which will pass the input parameter into the procedure as well
as output the result into an initialized variable

SQL> ed
Wrote file afiedt.buf

1 create or replace procedure ORDERDETAILS(orno in varchar2,b in


out varchar2
)
2

is

qtyor number;

qtydel number;

code varchar2(5);

begin

7 select QTYORD,QTYDELD,ITEMCODE into qtyor, qtydel, code from


ORDER_DETAILS
where ORDNO=orno;
8

if qtydel < qtyor then

b:= code;

10
b);

dbms_output.put_line('You supply less than what is ordered

11

elsif qtydel > qtyor then

12

b:= code;

13
b);

dbms_output.put_line('You supply more than what is ordered

14

else

15

b:=code;

16

dbms_output.put_line('NOTHING WRONG WITH THE SUPPLY

17

end if;

18

exception

19

when no_data_found then

20

dbms_output.put_line('no data returned');

21

when TOO_MANY_ROWS then

22

dbms_output.put_line('MANY ITEMS WITH SAME ITEMCODE');

23

when OTHERS then

24

dbms_output.put_line('SOME OTHER ERROR');

25* end ORDERDETAILS;


26

Procedure created.

'||b);

'||

'||

After successful compilation of the procedure we can execute the same by passing the
parameters
Executing the Procedure with intialized variable:

SQL> set serveroutput on;


SQL>

DECLARE

var varchar2(10):='123';

begin

ORDERDETAILS('23',var);

end;

no data returned

PL/SQL procedure successfully completed.

SQL>

DECLARE

var varchar2(10):='232';

begin

ORDERDETAILS('7',var);

end;

You supply less than what is ordered

ITEM1

PL/SQL procedure successfully completed.

Please let me know if you want to know how to execute the same
procedure using initialized variables
A NOCOPY hints can be added to improve the performance
(create or replace procedure ORDERDETAILS(orno in varchar2,b in out NOCOPY
varchar2)
Using the NOCOPY hint tells the compiler to use pass by reference,
so no temporary buffer is needed and no copy forward and copy back operations
happen.
Instead, any modification to the parameter values are written directly to the
parameter variable .
Procedure To Check FOR ITEM LEVEL FROM THE ITEMS TABLE

Below I have tried to explain Creation of Items_table and Procedure to


Find Items less in Quantity from the Items Table;

SQL> CREATE TABLE ITEMS_TABLE(


2

ITEM_CODE VARCHAR2(12),

MINLEVL NUMBER(5),

QTYNOW NUMBER(5),

MAXLEVL NUMBER(5),

RATE NUMBER(9,2)

);

TABLE CREATED.

SQL> INSERT INTO ITEMS_TABLE VALUES ('ITEM_01',100,80,175,120.99);

1 ROW CREATED.

SQL> INSERT INTO ITEMS_TABLE VALUES('ITEM_02',125,100,250,200.99);

1 ROW CREATED.

SQL> INSERT INTO ITEMS_TABLE VALUES('ITEM_03',100,200,325,200.99);

1 ROW CREATED.

SQL> INSERT INTO ITEMS_TABLE VALUES('ITEM_05',100,200,325,200.00);

1 ROW CREATED.

SQL> SELECT * FROM ITEMS_TABLE;

ITEM_CODE

MINLEVL

QTYNOW

MAXLEVL

RATE

------------ ---------- ---------- ---------- ---------ITEM_01

100

80

175

120.99

ITEM_02

125

100

250

200.99

ITEM_03

100

200

325

200.99

ITEM_05

100

200

325

200

SQL>

CREATE OR REPLACE PROCEDURE ITEMS_PROC(ITEM VARCHAR2)

IS

QTY_NOW NUMBER;

MIN_LEVEL NUMBER;

MAX_LEVEL NUMBER;

BEGIN

SELECT

QTYNOW,

MINLEVL,

10

MAXLEVL INTO QTY_NOW,MIN_LEVEL,MAX_LEVEL FROM

11

ITEMS_TABLE WHERE ITEM_CODE=ITEM;

12

IF QTY_NOW < MIN_LEVEL THEN

13
14

UPDATE ITEMS_TABLE SET QTYNOW=MINLEVL+QTYNOW WHERE ITEM_CODE=ITEM;


DBMS_OUTPUT.PUT_LINE('QTY UPDATED');

15

ELSE

16

DBMS_OUTPUT.PUT_LINE('ITEM PRESENT');

17

END IF;

18

EXCEPTION

19

WHEN NO_DATA_FOUND THEN

20

DBMS_OUTPUT.PUT_LINE('NO DATA RETURNED');

21

WHEN TOO_MANY_ROWS THEN

22

DBMS_OUTPUT.PUT_LINE('MANY ITEMS WITH SAME ITEM CODE');

23

WHEN OTHERS THEN

24

DBMS_OUTPUT.PUT_LINE('SOME OTHER ERROR');

25

END;

26

PROCEDURE CREATED.

SQL> EXEC ITEMS_PROC('ITEM_01');

PL/SQL PROCEDURE SUCCESSFULLY COMPLETED.

SQL> SELECT * FROM ITEMS_TABLE;

ITEM_CODE

MINLEVL

QTYNOW

MAXLEVL

RATE

------------ ---------- ---------- ---------- ---------ITEM_01

100

180

175

120.99

ITEM_02

125

100

250

200.99

ITEM_03

100

200

325

200.99

ITEM_05

100

200

325

200

MAXLEVL

RATE

SQL> EXEC ITEMS_PROC('ITEM_03');

PL/SQL PROCEDURE SUCCESSFULLY COMPLETED.

SQL> SELECT * FROM ITEMS_TABLE;

ITEM_CODE

MINLEVL

QTYNOW

------------ ---------- ---------- ---------- ---------ITEM_01

100

180

175

120.99

ITEM_02

125

100

250

200.99

ITEM_03

100

200

325

200.99

ITEM_05

100

200

325

200

Using Group by clause along with ROLLUP or CUBE operators

Use the ROLLUP OPERATOR to produce subtotal values, ROLLUP IS AN EXTENSION


OF GROUP BY CLAUSE, Use the CUBE OPERATOR to produce cross-tabulation values.
Use the GROUPING function to identify the row values created by ROLLUP or CUBE.
These operators and GROUPING function can best be used ALONG WITH GROUP
FUNCTIONS, as group functions operate on a set of rows to give one result per
group.

Examples of Rollup:

SQL> select deptno,sum(sal) from emp where deptno <30 group by


rollup(deptno);

DEPTNO

SUM(SAL)

---------- ---------10

8750

20

10875
19625

SQL> select deptno,job,sum(sal) from emp where deptno <30 group by


rollup(deptno,job);

DEPTNO JOB

SUM(SAL)

---------- --------- ---------10 CLERK

1300

10 MANAGER

2450

10 PRESIDENT

5000

10

8750

20 CLERK

1900

20 ANALYST

6000

20 MANAGER

2975

20

10875
19625

Example of CUBE:

The cube operator is used to produce results sets that are typically used for crosstabular reports. This means Rollup produces only one possible subtotaling where as
Cube produces subtotal for all possible conditions of grouping specified in the group
by clause and a grand total
SQL> select deptno,sum(sal) from emp where deptno <30 group by
cube(deptno);

DEPTNO

SUM(SAL)

---------- ---------19625
10

8750

20

10875

The following query produces subtotaling results based on job,based on deptno and
based on the individual jobs(clerk or analyst or manager etc in dept 10 and 20)
SQL> select deptno,job,sum(sal) from emp where deptno <30
group by cube(deptno,job);

DEPTNO JOB

SUM(SAL)

---------- --------- ---------19625


CLERK

3200

ANALYST

6000

MANAGER

5425

PRESIDENT

5000

10

8750

10 CLERK

1300

10 MANAGER

2450

10 PRESIDENT

5000

20
20 CLERK

DEPTNO JOB

10875
1900

SUM(SAL)

---------- --------- ---------20 ANALYST

6000

20 MANAGER

2975

13 rows selected.

NVL2 Function In Oracle

The nvl2 (expr1, expr2, expr3) works as follows:

if expr1 is not null it returns expr2,

If expr1 is null it returns expr3.

SQL> select ename,nvl2(comm,comm,0),comm from emp order by comm;


ENAME
NVL2(COMM,COMM,0)
COMM
---------- ----------------- --------TURNER
0
0
ALLEN
300
300
WARD
500
500
MARTIN
1400
1400
SMITH
0
JONES
0
JAMES
0
MILLER
0
FORD
0
ADAMS
0
BLAKE
0
CLARK
0
SCOTT
0
KING
0
14 rows selected.
SQL> select ename, vsize (ename) from emp;
ENAME
VSIZE(ENAME)
---------- -----------SMITH
5
ALLEN
5
WARD
4
JONES
5
MARTIN
6
BLAKE
5
CLARK
5
SCOTT
5
KING
4
TURNER
6
ADAMS
5
JAMES
5
FORD
4
MILLER
6
14 rows selected.
SQL> select ename,nvl2(comm,comm,null),comm from emp order by
comm;
ENAME
NVL2(COMM,COMM,NULL)
COMM
---------- -------------------- ---------TURNER
0
0
ALLEN
300
300

WARD
MARTIN
SMITH
JONES
JAMES
MILLER
FORD
ADAMS
BLAKE

500
1400

500
1400

ENAME
NVL2(COMM,COMM,NULL)
COMM
---------- -------------------- ---------CLARK
SCOTT
KING
14 rows selected.

SQL> select ename,nvl2(comm,null,0),comm from emp order by comm;


ENAME
NVL2(COMM,NULL,0)
COMM
---------- ----------------- ---------TURNER
0
ALLEN
300
WARD
500
MARTIN
1400
SMITH
0
JONES
0
JAMES
0
MILLER
0
FORD
0
ADAMS
0
BLAKE
0
ENAME
NVL2(COMM,NULL,0)
COMM
---------- ----------------- ---------CLARK
0
SCOTT
0
KING
0
14 rows selected.

What are Sub Queries in ORACLE


Nesting of queries, i.e., a query which is inside another query. Here the outer query
is called the parent query, which gets a result from the inner query. So the inner
query gets executed first and based on its result the outer query gets executed.
Note1:

Do no add an order by clause to a sub query.

Use single row operator with single row sub query.

Use multiple row operators with multiple-row sub query.

The single row sub query operators are =, >, <, <=, >=, <>, ^=, !=

The multiple row operators are IN, ANY, ALL

Note: 2

=any means IN and

!=all means NOT IN

Note: 3

<any means less than maximum

>any means greater than minimum

<all means less than minimum

>all means greater than maximum

SUB-QUERIES:SINGLE ROW SUB QUERY


SQL> select * from emp where sal>(select sal from emp where ename='SCOTT');
EMPNO ENAME
JOB
--------- ---------- --------7839 KING
PRESIDENT

HIREDATE
SAL
DEPTNO
--------- --------- --- --------17-NOV-81
5000
10

SQL >select * from emp where sal>=(select sal from emp where ename='SCOTT');
EMPNO ENAME
--------- ---------7788 SCOTT
7839 KING
7902 FORD

JOB
--------ANALYST
PRESIDENT
ANALYST

HIREDATE
SAL
--------- --------09-DEC-82
3000
17-NOV-81
5000
03-DEC-81
3000

DEPTNO
--------20
10
20

SQL >select ename,job from emp where job=(select job from emp where
empno=7369);
ENAME
---------SMITH
ADAMS
JAMES
MILLER

JOB
--------CLERK
CLERK
CLERK
CLERK

SQL > select ename,job from emp where job=(select job from emp where
empno=7369)and sal>(select sal from emp where empno=7876);

ENAME
JOB
---------- --------MILLER
CLERK

SQL >select * from emp where job='CLERK';


EMPNO ENAME
JOB
HIREDATE
SAL
DEPTNO
--------- - --------- --------- --------- ---------------------------7369 SMITH
CLERK
17-DEC-80
800
20
7876 ADAMS
CLERK
12-JAN-83
1100
20
7900 JAMES
CLERK
03-DEC-81
950
30
7934 MILLER
CLERK
23-JAN-82
1300
10

SUB-QUERIES: MULTIPLE ROW SUB QUERY


SQL >select empno,ename,sal from emp where sal=any(select sal from
emp,salgrade
where emp.sal between salgrade.losal and salgrade.hisal);
EMPNO ENAME
SAL
--------- ---------- --------7369 SMITH
800
7900 JAMES
950
7876 ADAMS
1100
7521 WARD
1250
7654 MARTIN
1250
7934 MILLER
1300
7844 TURNER
1500
7499 ALLEN
1600
7782 CLARK
2450
7698 BLAKE
2850
7566 JONES
2975
7788 SCOTT
3000
7902 FORD
3000
7839 KING
5000
14 rows selected.

SQL >select empno,ename,sal from emp where sal !=all (select sal from
emp,salgrade where emp.sal between salgrade.losal and salgrade.hisal);
no rows selected
Decode Function in Oracle
Decode helps us to use conditional inquires by doing the work of a case or if-thenelse statement (switch or if-then-else). The syntax is:

Decode(col/expression,search1,result1
[,search2,result2]
[,search3,result3] [,search4,result4] [..] [, default])

SQL> select ename, empno, sal, job,


decode (job,'MANAGER', sal+5000) new_sal from emp;

ENAME

EMPNO

SAL JOB

NEW_SAL

---------- --------- --------- --------- --------SMITH

7369

800 CLERK

ALLEN

7499

1600 SALESMAN

WARD

7521

1250 SALESMAN

JONES

7566

2975 MANAGER

MARTIN

7654

1250 SALESMAN

BLAKE

7698

2850 MANAGER

7850

CLARK

7782

2450 MANAGER

7450

SCOTT

7788

3000 ANALYST

KING

7839

5000 PRESIDENT

TURNER

7844

1500 SALESMAN

ADAMS

7876

1100 CLERK

JAMES

7900

950 CLERK

FORD

7902

3000 ANALYST

MILLER

7934

1300 CLERK

7975

14 rows selected.

SQL> select ename, empno, sal,


decode (job,'MANAGER',SAL+5000) "N_S", job from emp;
ENAME

EMPNO

SAL

N_S JOB

---------- --------- --------- --------- --------SMITH

7369

800

CLERK

ALLEN

7499

1600

SALESMAN

WARD

7521

1250

SALESMAN

JONES

7566

2975

7975 MANAGER

MARTIN

7654

1250

SALESMAN

BLAKE

7698

2850

7850 MANAGER

CLARK

7782

2450

7450 MANAGER

SCOTT

7788

3000

ANALYST

KING

7839

5000

PRESIDENT

TURNER

7844

1500

SALESMAN

ADAMS

7876

1100

CLERK

JAMES

7900

950

CLERK

FORD

7902

3000

ANALYST

MILLER

7934

1300

CLERK

14 rows selected.

SQL> select job, sal, decode (job,'ANALYST',sal*1.10, 'CLERK',SAL*1.15,


'MANAGER',sal*1.20,sal) "rev_sal" from emp ORDER BY JOB;
JOB

SAL

rev_sal

--------- --------- --------ANALYST

3000

3300

ANALYST

3000

3300

CLERK

800

920

CLERK

1100

1265

CLERK

1300

1495

CLERK

950

1092.5

MANAGER

2975

3570

MANAGER

2450

2940

MANAGER

2850

3420

PRESIDENT

5000

5000

SALESMAN

1600

1600

SALESMAN

1250

1250

SALESMAN

1500

1500

SALESMAN

1250

1250

14 rows selected.

SQL> select ename,sal,deptno,decode(deptno,10,


decode(job,'MANAGER',sal+4000,sal),sal+1000) "new_sal" from emp;
ENAME

SAL

DEPTNO

new_sal

---------- --------- --------- --------SMITH

800

20

1800

ALLEN

1600

30

2600

WARD

1250

30

2250

JONES

2975

20

3975

MARTIN

1250

30

2250

BLAKE

2850

30

3850

CLARK

2450

10

6450

SCOTT

3000

20

4000

KING

5000

10

5000

TURNER

1500

30

2500

ADAMS

1100

20

2100

JAMES

950

30

1950

FORD

3000

20

4000

MILLER

1300

10

1300

14 rows selected.
What are EXTERNAL TABLES in Oracle?

DEFINITION: You can user external table feature to access external files as if they
are tables inside the database.
When you create an external table, you define its structure and location with in
oracle.
When you query the table, oracle reads the external table and returns the results
just as if the data had been stored with in the database.

ACCESSING EXTERNAL TABLE DATA


To access external files from within oracle, you must first use the create directory
command to define a directory object pointing to the external file location
Users who will access the external files must have the read and write privilege on
the directory.
Ex:
CREATING DIRECTORY AND OS LEVEL FILE

SQL> Sqlplus system/manager


SQL> Create directory saketh_dir as /Visdb/visdb/9.2.0/external;
SQL> Grant all on directory saketh_dir to saketh;
SQL> Conn saketh/saketh
SQL> Spool dept.lst
SQL> Select deptno || , || dname || , || loc from dept;
SQL> Spool off

CREATING EXTERNAL TABLE

SQL> Create table dept_ext


(deptno number(2),
Dname varchar(14),

Loc varchar(13))
Organization external ( type oracle_loader
Default directory saketh_dir
Access parameters
( records delimited by newline
Fields terminated by ,
( deptno number(2),
Dname varchar(14),
Loc varchar(13)))
Location (/Visdb/visdb/9.2.0/dept.lst));

SELECTING DATA FROM EXTERNAL TABLE

SQL> select * from dept_ext;


This will read from dept.lst which is a operating system level file.

LIMITATIONS ON EXTERNAL TABLES


a) You can not perform insert, update, and delete operations
b) Indexing not possible
c) Constraints not possible

BENEFITS OF EXTERNAL TABLES


a) Queries of external tables complete very quickly even though a full table scan id
required with each access
b) You can join external tables to each other or to standard tables

Das könnte Ihnen auch gefallen