Sie sind auf Seite 1von 13

declare

a number:=104;
b varchar2(30);
begin
select last_name into b from employees where employee_id=a;
dbms_output.put_line('Employee Name Is:' || b);
end;

declare
a number:=104;
b employees.last_name%type;
c b%type;
begin
select last_name,job_id into b,c from employees
where employee_id=a;
dbms_output.put_line('Employee Name is:' ||chr(9)|| b ||chr(10) || 'His Desg is:'
||chr(9)||c);
end;

declare
a number:=105;
b employees%rowtype;
begin
select * into b from employees where employee_id=a;
dbms_output.put_line(b.first_name||chr(9)||b.last_name||chr(9)||b.job_id||chr(9)||
b.salary);
end;

var name varchar2(30)


var job varchar2(10)

declare
a number:=104;
begin
select last_name,job_id into :name,:job from employees
where employee_id=a;
end;

print or
select employee_id,last_name,salary,job_id from employees
where job_id=:job;

declare
a number:=105;
b employees%rowtype;
begin
select * into b from employees where employee_id=a;
dbms_output.put_line(b.first_name||chr(9)||b.last_name||chr(9)||b.job_id||chr(9)||
b.salary);
end;

declare
a number:=345;
b number:=2000;
begin
update employees set salary=salary+b where employee_id=a;
dbms_output.put_line(sql%rowcount|| ' rows updated');
end;
declare
a number;
b number;
begin
a:=10;
b:=20;
if a>b then
dbms_output.put_line(b || 'is greater than a');
else
dbms_output.put_line(a || 'is greater than b');
end if;
end;

SELECT last_name, job_id, salary,


CASE job_id WHEN 'IT_PROG' THEN 1.10*salary
WHEN 'ST_CLERK' THEN 1.15*salary
WHEN 'SA_REP' THEN 1.20*salary
ELSE salary END "REVISED_SALARY"
FROM employees;

DECLARE
grade CHAR(1) := UPPER('&grade');
appraisal VARCHAR2(20);
BEGIN
appraisal :=
CASE
WHEN grade = 'A' THEN 'Excellent'
WHEN grade IN ('B','C') THEN 'Good'
ELSE 'No such grade'
END;
DBMS_OUTPUT.PUT_LINE ('Grade: '|| grade || ' Appraisal ' || appraisal);
END;

SET SERVEROUTPUT ON

DECLARE
deptid NUMBER;
deptname VARCHAR2(20);
emps NUMBER;
mngid NUMBER:= 108;
BEGIN
CASE mngid
WHEN 108 THEN
SELECT department_id, department_name INTO deptid, deptname
FROM departments WHERE manager_id=108;
SELECT count(*) INTO emps FROM employees WHERE
department_id=deptid;
WHEN 200 THEN
SELECT department_id, department_name INTO deptid, deptname
FROM departments WHERE manager_id=200;
SELECT count(*) INTO emps FROM employees WHERE
department_id=deptid;
WHEN 121 THEN
SELECT department_id, department_name INTO deptid, deptname
FROM departments WHERE manager_id=121;
SELECT count(*) INTO emps FROM employees WHERE
department_id=deptid;
END CASE;
DBMS_OUTPUT.PUT_LINE ('You are working in the '|| deptname||' department. There are
'||emps ||' employees in this department');
END;

declare
a number:=0;
begin
loop
a:=a+1;
exit when a>10;
dbms_output.put_line(a);
end loop;
end;

declare
a number:=0;
begin
while a<10 loop
a:=a+1;
dbms_output.put_line(a);
end loop;
end;

begin
for a in 1..10
loop
dbms_output.put_line(a);
end loop;
end;

select employee_id,last_name,salary,job_id from employees


where rownum<=5
minus
select employee_id,last_name,salary,job_id from employees
where rownum<=4

select employee_id,last_name,salary,job_id from (select e.*,rownum x from employees


e) where x=5

declare
a number;
b varchar2(200);
begin
select count(*) into a from employees;
for i in 1..a loop
select employee_id||' '||last_name||' '||salary||' '||job_id into b from(select
e.*,rownum x from employees e) where x=i;
dbms_output.put_line(b);
end loop;
end;

DECLARE
type t_rec is record
(v_sal number(8),
v_minsal number(8) default 1000,
v_hire_date employees.hire_date%type,
v_rec1 employees%rowtype);
v_myrec t_rec;
BEGIN
v_myrec.v_sal := v_myrec.v_minsal + 500;
v_myrec.v_hire_date := sysdate;
SELECT * INTO v_myrec.v_rec1
FROM employees WHERE employee_id = 100;
DBMS_OUTPUT.PUT_LINE(v_myrec.v_rec1.last_name ||' '||
to_char(v_myrec.v_hire_date) ||' '|| to_char(v_myrec.v_sal));
END;

DECLARE
TYPE emp_table_type is table of
employees%ROWTYPE INDEX BY PLS_INTEGER;
my_emp_table emp_table_type;
max_count NUMBER(3):= 206;
BEGIN
FOR i IN 100..max_count
LOOP
SELECT * INTO my_emp_table(i) FROM employees
WHERE employee_id = i;
END LOOP;
FOR i IN my_emp_table.FIRST..my_emp_table.LAST
LOOP
DBMS_OUTPUT.PUT_LINE(my_emp_table(i).last_name||chr(9)||
my_emp_table(i).job_id);
END LOOP;
END;

DECLARE
TYPE emp_table_type is table of
employees%ROWTYPE INDEX BY PLS_INTEGER;
my_emp_table emp_table_type;
BEGIN
SELECT * bulk collect INTO my_emp_table FROM employees;
FOR i IN my_emp_table.FIRST..my_emp_table.LAST
LOOP
DBMS_OUTPUT.PUT_LINE(my_emp_table(i).last_name||chr(9)||
my_emp_table(i).job_id);
END LOOP;
END;

DECLARE
TYPE emp_table_type is table of
employees%ROWTYPE INDEX BY PLS_INTEGER;
my_emp_table emp_table_type;
BEGIN
SELECT * bulk collect INTO my_emp_table FROM employees;

FOR i IN my_emp_table.FIRST..my_emp_table.LAST
LOOP
DBMS_OUTPUT.PUT_LINE(my_emp_table(i).last_name||chr(9)||
my_emp_table(i).job_id);
END LOOP;
END;

DECLARE
TYPE emp_table_type is table of
employees%ROWTYPE INDEX BY PLS_INTEGER;
my_emp_table emp_table_type;
BEGIN
SELECT * bulk collect INTO my_emp_table FROM employees;
DBMS_OUTPUT.PUT_LINE(my_emp_table(25).employee_id||chr(9)||
my_emp_table(25).last_name||chr(9)||my_emp_table(25).job_id);
END;

declare
CURSOR C1 is select employee_id,job_id,salary from emp_cur;
e c1%rowtype;
begin
open c1;
loop
fetch c1 into e;
exit when c1%notfound;
if e.job_id='IT_PROG' then
update emp_cur set salary=salary+1000 where employee_id=e.employee_id;
elsif e.job_id='SA_MAN' then
update emp_cur set salary=salary+1500 where employee_id=e.employee_id;
elsif e.job_id='ST_MAN' then
update emp_cur set salary=salary+1800 where employee_id=e.employee_id;
else
update emp_cur set salary=salary+2000 where employee_id=e.employee_id;
end if;
end loop;
close c1;
end;

declare
cursor c1 is select employee_id,last_name,salary from emp_cur;
e c1%rowtype;
begin
open c1;
fetch c1 into e;
while c1%found loop
dbms_output.put_line(e.employee_id||chr(9)||e.salary||chr(9)||e.last_name);
fetch c1 into e;
end loop;
close c1;
end;

declare
cursor c1 is select employee_id,last_name,salary from emp_cur;
begin
for e in c1 loop
dbms_output.put_line(e.employee_id||chr(9)||e.salary||chr(9)||e.last_name);
end loop;
end;
declare
cursor c1 is select * from departments;
cursor c2(dno number) is select * from employees where department_id=dno;
begin
for i in c1 loop
dbms_output.put_line(i.department_id||chr(9)||i.department_name||chr(9)||
i.location_id);
for j in c2(i.department_id) loop
dbms_output.put_line(j.employee_id||chr(9)||j.last_name||chr(9)||j.job_id);
end loop;
end loop;
end;

declare
cursor c1 is select employee_id,job_id,salary from emp_cur for update ;
e c1%rowtype;
begin
open c1;
loop
fetch c1 into e;
exit when c1%rowcount>108;
if e.job_id='IT_PROG' then
update emp_cur set salary=salary+925 where current of c1;
elsif e.job_id='SA_MAN' then
update emp_cur set salary=salary+1025 where current of c1;
elsif e.job_id='ST_MAN' then
update emp_cur set salary=salary+1525 where current of c1;
else
update emp_cur set salary=salary+2025 where current of c1;
end if;
end loop;
commit;
end;

DECLARE
v_lname VARCHAR2(15);
BEGIN
SELECT last_name INTO v_lname FROM employees WHERE
first_name='John';
DBMS_OUTPUT.PUT_LINE ('John''s last name is : ' ||v_lname);
EXCEPTION
WHEN TOO_MANY_ROWS THEN
DBMS_OUTPUT.PUT_LINE (' Your select statement retrieved multiple
rows. Consider using a cursor.');
END;
/

declare
a number:=50;
del_child exception;
pragma exception_init(del_child,-02292);
begin
delete from departments where department_id=a;
dbms_output.put_line(sql%rowcount||' '||' rows deleted ');
exception
when del_child then
dbms_output.put_line('Before deleting this record remove dependent records from
employees table');
end;

declare
a number:=567;
No_val exception;
begin
update employees set salary=salary+1000 where employee_id=a;
if sql%notfound then
raise no_val;
else
dbms_output.put_line(sql%rowcount|| ' rows updated ');
end if;
exception
when no_val then
dbms_output.put_line('Enter Valid Employee Number');
end;

declare
a number:=567;
begin
update employees set salary=salary+1000 where employee_id=a;
if sql%notfound then
raise_application_error(-20123,'Invalid Employee Number');
else
dbms_output.put_line(sql%rowcount|| ' rows updated ');
end if;
end;

select object_name,object_type,status,created from user_objects


where object_type='PROCEDURE';

CREATE OR REPLACE PROCEDURE AR_PRO1 IS


CURSOR C1 IS SELECT * FROM EMPLOYEES;
BEGIN
FOR I IN C1 LOOP
DBMS_OUTPUT.PUT_LINE(I.EMPLOYEE_ID||CHR(9)||I.LAST_NAME||CHR(9)||I.JOB_ID);
END LOOP;
END;

SELECT TEXT FROM USER_SOURCE WHERE NAME='AR_PRO1'

IN APEX :
BEGIN
AR_PRO1;
END;

IN SQL*PLUS

EXEC AR_PRO1

CREATE OR REPLACE PROCEDURE AR_PRO2(ENO NUMBER) IS


NAME VARCHAR2(50);
HDATE VARCHAR2(100);
SAL NUMBER(9,2);
BEGIN
SELECT FIRST_NAME||' '||LAST_NAME,TO_CHAR(HIRE_DATE,'DDTH MONTH YYYY'),SALARY*12
INTO NAME,HDATE,SAL FROM EMPLOYEES WHERE EMPLOYEE_ID=ENO;
DBMS_OUTPUT.PUT_LINE('Employee Details:'|| NAME ||' '||HDATE||' '||SAL);
END;

CREATE OR REPLACE PROCEDURE AR_PRO3(ENO NUMBER,NAME OUT VARCHAR2,


HDATE OUT VARCHAR2,SAL OUT NUMBER) IS
BEGIN
SELECT FIRST_NAME||' '||LAST_NAME,TO_CHAR(HIRE_DATE,'DDTH MONTH YYYY'),SALARY*12
INTO NAME,HDATE,SAL FROM EMPLOYEES WHERE EMPLOYEE_ID=ENO;
DBMS_OUTPUT.PUT_LINE('Employee Details:'|| NAME ||' '||HDATE||' '||SAL);
END;
DECLARE
A NUMBER:=123;
B VARCHAR2(50);
C VARCHAR2(100);
D NUMBER;
BEGIN
AR_PRO3(A,B,C,D);
END;

In SQL * PLUS

VAR A VARCHAR2(50)
VAR B VARCHAR2(100)
VAR C NUMBER
EXEC AR_PRO3(123,:A,:B,:C)

CREATE OR REPLACE PROCEDURE AR_PRO4(ENO IN OUT NUMBER,NAME OUT VARCHAR2,


HDATE OUT VARCHAR2,SAL OUT NUMBER) IS
BEGIN
SELECT FIRST_NAME||' '||LAST_NAME,TO_CHAR(HIRE_DATE,'DDTH MONTH YYYY'),SALARY*12,
MANAGER_ID INTO NAME,HDATE,SAL,ENO FROM EMPLOYEES WHERE EMPLOYEE_ID=ENO;
DBMS_OUTPUT.PUT_LINE('Employee Details:'|| NAME ||' '||HDATE||' '||SAL||CHR(9)||
ENO);
END;

DECLARE
A NUMBER:=206;
B VARCHAR2(50);
C VARCHAR2(100);
D NUMBER;
BEGIN
AR_PRO4(A,B,C,D);
END;

IN SQL * PLUS
VAR N NUMBER
VAR A VARCHAR2(50)
VAR B VARCHAR2(100)
VAR C NUMBER
BEGIN
:N:=205;
END;
/

EXEC AR_PRO3(:N,:A,:B,:C)

CREATE OR REPLACE PROCEDURE add_dept(


p_name departments.department_name%TYPE:='Unknown',
p_loc departments.location_id%TYPE DEFAULT 1700) IS

BEGIN
INSERT INTO departments (department_id, department_name, location_id)
VALUES (departments_seq.NEXTVAL, p_name, p_loc);
END add_dept;

EXECUTE add_dept
EXECUTE add_dept ('ADVERTISING', p_loc => 1200)
EXECUTE add_dept (p_loc => 1200)

drop procedure ar_pro1

create or replace function ar_fun(a number,b number,c varchar2) return number is


d number;
begin
if c='+' then
d:=a+b;
elsif c='-' then
d:=a-b;
elsif c='*' then
d:=a*b;
elsif c='/' then
d:=a/b;
end if;
return d;
end;

select ar_fun(373,3773,'+') from dual;


select ar_fun(salary,nvl(commission_pct,1),'*') from employees;

exec dbms_output.put_line(ar_fun(2633,33,'/'))

var n number
exec :n:=ar_fun(4646,474,'-')
print n

declare
a number:=366;
b number:=3636;
c varchar2(1):='+';
d number;
begin
ar_fun(a,b,c,d);
dbms_output.put_line(d);
end;

select level,employee_id,manager_id,job_id,last_name from employees


connect by prior employee_id=manager_id
start with manager_id is null
order by 1

grant execute on <sub_program name> to username with grant option;

select * from user_tab_privs_recd;

select * from user_tab_privs_made;

create or replace function ar_fun1 (a number) return number is


b number;
begin
b:=a*a*a;
insert into dept values(10,'Accounts',101,101);
return b;
end;

declare
a number:=9;
b number;
begin
b:=ar_fun1(a);
dbms_output.put_line(b);
end;

create or replace package ar_pack is


procedure p1;
procedure p1(eno number,n_sal number);
procedure p1(eno number,det out varchar2);
procedure p2(eno number);
function f1(eno number) return date;
function f2(eno number) return number;
d date;
c varchar2(200);
n number;
end;

create or replace package body ar_pack is


procedure p1 is
cursor c1 is select * from employees;
begin
for i in c1 loop
dbms_output.put_line(i.last_name||' '||i.job_id||i.salary);
end loop;
end p1;
procedure p1(eno number,n_sal number) is
begin
select salary into n from employees where employee_id=eno;
if n <=5000 then
update employees set salary=salary+n_sal where employee_id=eno;
else
dbms_output.put_line('Employee Get More Than 5000');
end if;
end p1;
procedure p1(eno number,det out varchar2) is
begin
select first_name||' '||last_name||' '||job_id||' '||salary into det
from employees where employee_id=eno;
dbms_output.put_line(det);
end p1;
procedure p2(eno number) is
begin
select first_name||' '||last_name||' '||job_id||' '||salary||' '||hire_date into c
from employees where employee_id=eno;
dbms_output.put_line('Employee Details :' || c);
end p2;
function f1(eno number) return date is
begin
select hire_date into d from employees where employee_id=eno;
return d;
end f1;
function f2(eno number) return number is
begin
select salary*12 into n from employees where employee_id=eno;
return n;
end f2;
end ar_pack;
HR login

create or replace package ar_pack1 is


p_rec exception;
c_rec exception;
pragma exception_init(p_rec,-02291);
pragma exception_init(c_rec,-02292);
type emp_typ is table of employees%rowtype index by pls_integer;
end;

grant execute on ar_pack1 to user9;


user9 login

declare
a hr.ar_pack1.emp_typ;
begin
select * bulk collect into a from employees;
for i in a.first..a.last loop
dbms_output.put_line(a(i).last_name);
end loop;
end;

create or replace package ar_pack3 is


procedure p1 (eno number);
n number(9,2);
end;

create or replace package body ar_pack3 is


procedure p1(eno number) is
a varchar2(25);
begin
select last_name into a from employees where employee_id=eno;
dbms_output.put_line(a);
end p1;
begin
select price into n from dollar_price where sno=1;
end ar_pack3;

declare
a number;
cursor c1 is select table_name from user_tables;
begin
for i in c1 loop
execute immediate ' select count(*) from ' ||i.table_name into a;
dbms_output.put_line(i.table_name|| chr(9) || a);
end loop;
end;

create or replace PROCEDURE create_index


(index_in IN VARCHAR2, tab_in IN VARCHAR2,
col_in IN VARCHAR2)
IS
cur INTEGER := DBMS_SQL.OPEN_CURSOR;
fdbk INTEGER;
DDL_statement VARCHAR2(200)
:= 'CREATE INDEX ' || index_in || ' ON ' || tab_in ||
' ( ' || col_in || ')';
BEGIN
DBMS_SQL.PARSE (cur, DDL_statement, DBMS_SQL.NATIVE);
fdbk := DBMS_SQL.EXECUTE (cur);
DBMS_SQL.CLOSE_CURSOR (cur);

END;

create or replace PROCEDURE create_index1


(index_in IN VARCHAR2, tab_in IN VARCHAR2,
col_in IN VARCHAR2)
IS
DDL_statement VARCHAR2(200)
:= 'CREATE INDEX ' || index_in || ' ON ' || tab_in ||
' ( ' || col_in || ')';
BEGIN
execute immediate ddl_statement;
END;
create or replace view ar_vu as select
e.employee_id,e.last_name,e.job_id,e.hire_date,e.email,e.department_id edno,
d.department_id dno,d.department_name,d.location_id from employees e , departments
d where e.department_id=d.department_id;

insert into ar_vu values(207,'James','IT_PROG',sysdate,'james@com.com',30,


290,'ADMIN',1700);

create or replace trigger ar_trig instead of insert on ar_vu for each row
begin
insert into departments(department_id,department_name,location_id) values
(:new.dno,:new.department_name,:new.location_id);
insert into employees(employee_id,last_name,job_id,hire_date,email,department_id)
values
(:new.employee_id,:new.last_name,:new.job_id,:new.hire_date,:new.email,:new.edno);
end;

Das könnte Ihnen auch gefallen