plsql examples

5
create or replace function employee_details ( p_empnum number , p_salary number )return number is v_first_name varchar2 ( 100 ); v_last_NAME VARCHAR2 ( 100 ); V_SALARY NUMBER ; eMP_SAL EXCEPTION; begin select first_name , last_NAME , SALARY into v_first_name , v_last_NAME , V_SALARY from system. emp where employee_id = p_empnum ; --salary=17000 IF V_SALARY < p_salary THEN raise eMP_SAL ; else DBMS_OUTPUT.PUT_LINE ( 'Name of Employee:-' ||v_first_name || v_last_NAME ); return 1 ; END IF; --and rownum = 1; exception when eMP_SAL then DBMS_OUTPUT.PUT_LINE ( 'from NO EMP Exception SALARY ID LESS THAT' ||p_salary ); return 0 ; when no_DATA_FOUND then DBMS_OUTPUT.PUT_LINE ( 'from no_datefound Exception SALARY ID LESS THAT' || p_salary ); DBMS_OUTPUT.PUT_LINE ( 'SQLCODE' || SQLCODE); DBMS_OUTPUT.PUT_LINE ( 'SQLERRM' || SQLERRM); return 0 ; when others then DBMS_OUTPUT.PUT_LINE ( 'from Others Exception SALARY ID LESS THAT' ||p_salary ); end; declare v_first_name varchar2 ( 100 ); v_last_NAME VARCHAR2 ( 100 ); V_SALARY NUMBER ; eMP_SAL EXCEPTION; begin

Upload: crecykengmailcom

Post on 22-Dec-2015

2 views

Category:

Documents


0 download

DESCRIPTION

PLSQL Examples

TRANSCRIPT

Page 1: PLSQL Examples

create or replace function employee_details (p_empnum number, p_salary number)return number is v_first_name varchar2(100);v_last_NAME VARCHAR2(100);V_SALARY NUMBER;eMP_SAL EXCEPTION; beginselect first_name , last_NAME, SALARYinto v_first_name , v_last_NAME, V_SALARYfrom system.empwhere employee_id=p_empnum;--salary=17000IF V_SALARY <p_salary THEN raise eMP_SAL;elseDBMS_OUTPUT.PUT_LINE('Name of Employee:-'||v_first_name || v_last_NAME); return 1;END IF; --and rownum = 1;

exceptionwhen eMP_SAL then DBMS_OUTPUT.PUT_LINE('from NO EMP Exception SALARY ID LESS THAT'||p_salary);return 0;when no_DATA_FOUND then DBMS_OUTPUT.PUT_LINE('from no_datefound Exception SALARY ID LESS THAT'||p_salary);DBMS_OUTPUT.PUT_LINE('SQLCODE'||SQLCODE);DBMS_OUTPUT.PUT_LINE('SQLERRM'||SQLERRM);return 0;when others then DBMS_OUTPUT.PUT_LINE('from Others Exception SALARY ID LESS THAT'||p_salary);end;

declare v_first_name varchar2(100);v_last_NAME VARCHAR2(100);V_SALARY NUMBER;eMP_SAL EXCEPTION; beginselect first_name , last_NAME, SALARYinto v_first_name , v_last_NAME, V_SALARYfrom system.empwhere employee_id=1300;--salary=17000

Page 2: PLSQL Examples

IF V_SALARY <5000 THEN raise eMP_SAL;END IF; --and rownum = 1;DBMS_OUTPUT.PUT_LINE('Name of Employee:-'||v_first_name || v_last_NAME);exceptionwhen eMP_SAL then DBMS_OUTPUT.PUT_LINE('from NO EMP Exception SALARY ID LESS THAT 5000');when no_DATA_FOUND then DBMS_OUTPUT.PUT_LINE('from no_datefound Exception SALARY ID LESS THAT 5000');DBMS_OUTPUT.PUT_LINE('SQLCODE'||SQLCODE);DBMS_OUTPUT.PUT_LINE('SQLERRM'||SQLERRM);when others then DBMS_OUTPUT.PUT_LINE('from Others Exception SALARY ID LESS THAT 5000');end;

cursor

beginupdate system.emp set salary = 2800where employee_id=:p_empnum;if SQL%FOUND then DBMS_OUTPUT.PUT_LINE('Row found:');elseDBMS_OUTPUT.PUT_LINE('Row not found:');end if;end;

commit

rollback

select * from system.emp where employee_id=:p_empnum;

declarecursor c_allemp isselect employee_id, DEPARTMENT_ID from system.empwhere DEPARTMENT_ID = 10;

v_deptno number;v_empid number;

Page 3: PLSQL Examples

begin

OPEN c_allemp; --- Open cursor

loop FETCH c_allemp INTO v_empid ,v_deptno ; -- fetch values EXIT WHEN c_allemp%NOTFOUND IS NULL;

if v_deptno = 10 then update system.emp set salary = 2000 where employee_id = v_empid; commit; elsif v_deptno = 20 then update system.emp set salary = 4000 where employee_id = v_empid; commit; else null; end if; end loop; CLOSE c_allemp; --- close cursor

end;

declarecursor c_allemp isselect employee_id, DEPARTMENT_ID, salary from system.emp;--where DEPARTMENT_ID = 10;v_salary number;

begin

Page 4: PLSQL Examples

DBMS_OUTPUT.PUT_LINE( 'hi'); for i in c_allemp loop if i.DEPARTMENT_ID = 20 then DBMS_OUTPUT.PUT_LINE('before salary:-'|| i.Salary); update system.emp set salary = salary +(salary /10) where employee_id = i.employee_id; select salary into v_salary from system.emp where employee_id = i.employee_id; DBMS_OUTPUT.PUT_LINE('after salary:-'|| v_salary); end if; end loop;

end;