Beruflich Dokumente
Kultur Dokumente
1.
3.
4.
Must be specified,
a)Shall take values from caller / returns a value to caller.
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.
(ordno varchar2(12),
itemcode varchar2(12),
qtyord number,
qtydeld number);
Table created.
ORDER_DETAILS
VALUES('1','ITEM4',300,300);
ORDER_DETAILS
VALUES('5','ITEM1',200,200);
ORDER_DETAILS
VALUES('7','ITEM1',300,200);
1 row created.
1 row created.
1 row created.
ORDER_DETAILS
ORDER_DETAILS
VALUES('31','ITEM2',300,300);
1 row created.
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
is
qtyor number;
qtydel number;
code varchar2(5);
begin
b:= code;
10
b);
11
12
b:= code;
13
b);
14
else
15
b:=code;
16
17
end if;
18
exception
19
20
21
22
23
24
Procedure created.
'||b);
'||
'||
After successful compilation of the procedure we can execute the same by passing the
parameters
Executing the Procedure with intialized variable:
DECLARE
var varchar2(10):='123';
begin
ORDERDETAILS('23',var);
end;
no data returned
SQL>
DECLARE
var varchar2(10):='232';
begin
ORDERDETAILS('7',var);
end;
ITEM1
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
ITEM_CODE VARCHAR2(12),
MINLEVL NUMBER(5),
QTYNOW NUMBER(5),
MAXLEVL NUMBER(5),
RATE NUMBER(9,2)
);
TABLE CREATED.
1 ROW CREATED.
1 ROW CREATED.
1 ROW CREATED.
1 ROW CREATED.
ITEM_CODE
MINLEVL
QTYNOW
MAXLEVL
RATE
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>
IS
QTY_NOW NUMBER;
MIN_LEVEL NUMBER;
MAX_LEVEL NUMBER;
BEGIN
SELECT
QTYNOW,
MINLEVL,
10
11
12
13
14
15
ELSE
16
DBMS_OUTPUT.PUT_LINE('ITEM PRESENT');
17
END IF;
18
EXCEPTION
19
20
21
22
23
24
25
END;
26
PROCEDURE CREATED.
ITEM_CODE
MINLEVL
QTYNOW
MAXLEVL
RATE
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
ITEM_CODE
MINLEVL
QTYNOW
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
Examples of Rollup:
DEPTNO
SUM(SAL)
---------- ---------10
8750
20
10875
19625
DEPTNO JOB
SUM(SAL)
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)
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)
6000
20 MANAGER
2975
13 rows selected.
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.
The single row sub query operators are =, >, <, <=, >=, <>, ^=, !=
Note: 2
Note: 3
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 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])
ENAME
EMPNO
SAL JOB
NEW_SAL
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.
EMPNO
SAL
N_S JOB
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.
SAL
rev_sal
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.
SAL
DEPTNO
new_sal
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.
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));