Oracle PL-SQL Fundamentals Part 2 Practise Queries
Oracle PL-SQL Fundamentals Part 2 Practise Queries
commit;
commit;
NOTE : ---------------------------
----------------------------------
4. Warning settings
select dbms_warning.get_warning_setting_string from dual;
Go to Tools --> preferences --> expand databse and click on PL/SQL Compiler and
Enable ALL
Now when you run the above code, It would show errors on emp_dept_id = '2' -bind
type conversion
and Raise; - unreachable code
END;
/
example 2:
select max_emp_sal from dual;
OUTPUT :
emp_id: 10
department_id: 1
location: -- ITS NULL BECASE OUT MOADE IS ALWAYS IN WRITE ONLY MODE SO THE
VALUES CANT BE READ
status_initially: -1
>clear screen;
set serveroutput on;
DECLARE
v_emp_id number := 10;
v_dept_id number := 1;
v_location varchar2(10) := 'CA';
v_status number := -1;
BEGIN
UPDATE_EMP(
v_emp_id,
v_dept_id,
v_location,
v_status
);
dbms_output.put_line('emp_id: '||v_emp_id);
dbms_output.put_line('department_id: '||v_dept_id);
dbms_output.put_line('location: '||v_location);
dbms_output.put_line('status_: '||v_status);
END;
OUTPUT:
emp_id: 10
department_id: 1
location: XXX
status_: 9
NOTE : OUT AND IN OUT values changes if anonymous block runs perfectly
if any exception is raised, then the value doesnt change
below is the example
create or replace procedure update_emp(p_emp_id in number,p_dept_id number,
p_location out varchar2, p_status in out number)
as
v_val_exp exception;
BEGIN
--p_dept_id := 9; cant change p_dept_id value coz the mode in IN and it means
read only
p_location := 'XXX';
clear screen;
set serveroutput on;
DECLARE
v_emp_id number := 10;
v_dept_id number := 1;
v_location varchar2(10) := 'CA';
v_status number := -1;
BEGIN
UPDATE_EMP(
v_emp_id,
v_dept_id,
v_location,
v_status
);
dbms_output.put_line('emp_id: '||v_emp_id);
dbms_output.put_line('department_id: '||v_dept_id);
dbms_output.put_line('location: '||v_location);
dbms_output.put_line('status_: '||v_status);
exception
when others then
dbms_output.put_line('Exception block hits');
dbms_output.put_line('emp_id: '||v_emp_id);
dbms_output.put_line('department_id: '||v_dept_id);
dbms_output.put_line('location: '||v_location);
dbms_output.put_line('status_: '||v_status);
END;
Output:
v_val_exp exception block hit...!
Exception block hits
emp_id: 10
department_id: 1
location: CA
status_: -1
You see even tho exception was raised after location value was updated, still
location value did not change.
remember unless the whole block executes succesfully, the
15. If you use nocopy then even if the exception is raised, the OUT and INOUT
values will not be reverted
syntax:
create or replace procedure update_emp(p_emp_id in number,p_dept_id number,
p_location out nocopy varchar2, p_status in out nocopy number)
16. Creating Package
--print emp id of all the people whose salary is greater than n (n is passed to
procedure as a param)
OUTPUT:
10
20
50
--To provide grant to another schema (Example_Schema) to run the above package
grant execute on assignment_pack to Example;
--here max_emp_sal is a function name that returns the max salary among the
employee