Practical 8: Starting with PLSQL-%Type and Bind Variables

Total Page:16

File Type:pdf, Size:1020Kb

Practical 8: Starting with PLSQL-%Type and Bind Variables

1

Practical 8: Starting with PLSQL-%type for i in 1..a.last and bind variables loop --Using %type atribute dbms_output.put_line(a(i)||a.last||a.first|| declare a.prior(i)||a.next(i)||a.count); v_ename emp.ename%type; end loop; n_empno emp.empno%type; end; n_sal emp.sal%type; / begin --record type v_ename:=&ename; declare select empno,sal into n_empno,n_sal from type emp_record_type is record emp where ename=upper(v_ename); dbms_output.put_line('Name: '|| ( ename varchar2(20), v_ename||' Empno: '||n_empno|| ' Salary: job varchar2(10), '||n_sal); sal number(7,2)); end; emp_record emp_record_type; / begin v_ename emp_record.ename%type; update emp set sal = sal+100 where v_job emp_record.job%type; sal>2500; n_sal emp_record.sal%type; dbms_output.put_line('No. of rows updated are '||sql%rowcount); begin rollback; v_ename:=&v_ename; end; select job,sal into v_job,n_sal from emp / where ename=v_ename; --Bind variables variable bind_1 number; dbms_output.put_line('Name: '|| exec :bind_1:=10; v_ename|| ' Job: '||v_job||' Salary: '||n_sal); begin dbms_output.put_line('No. of records dbms_output.put_line('The value of bind affected: '||sql%rowcount); variable bind_1 is '||:bind_1); end; end; / / select :bind_1+20 from dual; Practical 10: Cursors --explicit cursor steps Practical 9:Collections and Records declare --index by table cursor cur_emp is select * from emp declare where sal>2000; type abc is table of number index by rec_cur_emp cur_emp%rowtype; binary_integer; a abc; begin i binary_integer; open cur_emp; begin loop a(1):=10; fetch cur_emp into rec_cur_emp; a(2):=20; dbms_output.put_line(rec_cur_emp.empn a(3):=30; o||’ ‘||rec_cur_emp.ename); a(4):=40; 2 exit when cur_emp%notfound; --Another way of calling a function end loop; select area_circle_func(2) from dual; close cur_emp; --procedure end; create or replace procedure area_circle(r --cursor for loop number) is declare pi constant number(3,2):=3.14; cursor cur_emp is select * from emp area number(8,2); where sal>2000; begin begin area:=pi*r*r; for rec_cur_emp in cur_emp loop dbms_output.put_line(‘The area of circle dbms_output.put_line(rec_cur_emp.empn with radius ‘||r||’ is ‘||area); o||’ ‘||rec_cur_emp.ename); End; end loop; / end; --calling a procedure Practical 11: Functions and Procedures declare --function radius number(1); create or replace function begin area_circle_func(r number) return area_circle(&radius); number is Dbms_output.put_line(‘Back to main a number; program’); pi constant number(3,2):=3.14; End; begin / a:=pi*r*r; --Another way of calling procedure dbms_output.put_line(‘In the function:The Exec Area_circle(&radius); area of circle with radius ‘||r||’ is ‘||a); Exec Area_circle(5); return a; end; -- Procedure with in out parameter / Create or replace procedure --calling a function area_circle_in_out(r in number, a out declare number) is radius number(1); Pi constant number(3,2):=3.14; a number; Area number(8,2); begin Begin radius:=&radius; a:=pi*r*r; a:=area_circle_func(radius); Dbms_output.put_line(‘In the dbms_output.put_line(‘The area of circle procedure:The area of circle with radius with radius ‘||radius||’ is ’||a ||‘.Back to ‘||r||’is’||a); main program’); End; End; / / --Calling procedure 3

Declare dbms_output.put_line(‘employee number radius number(1); is ‘||v_empno|| ‘ and name is ‘||v_ename); a number; dbms_output.put_line(‘salary is ‘||v_sal); Begin v_sal:=v_sal+v_sal/a; Area_circle_in_out(&radius,a); if v_sal<2000 then raise lo_sal; Dbms_output.put_line(‘Area of circle with end if; radius ‘||radius||’is’||a ||‘.Back to main dbms_output.put_line(‘new salary is ‘|| program’); v_sal); End; exception / when lo_sal then Practical 12: Exceptions dbms_output.put_line(‘in these times of --Predefined named Exceptions inflation increase the salary!!!’); set serveroutput on when others then declare dbms_output.put_line(‘some error!!!god v_empno emp.empno%type; knows what???’); v_ename emp.ename%type; end; begin / select empno,ename into v_empno, --user defined exceptions: attaching v_ename from emp where error code empno=&v_empno; Declare dbms_output.put_line(‘Employee number V_empno emp.empno%type; is ‘||v_empno || ‘ and name is ‘||v_ename); V_ename emp.ename%type; exception V_sal emp.sal%type; when no_data_found then A number; dbms_output.put_line(‘OOPS!!! NO SUCH Lo_sal exception; RECORD’); Begin end; A:=&a; --user defined named exceptions Select empno,ename,sal into Declare v_empno,v_ename,v_sal from emp where v_empno emp.empno%type; empno=&v_empno; v_ename emp.ename%type; Dbms_output.put_line(‘Employee number v_sal emp.sal%type; is ‘||v_empno|| ‘ and name is ‘||v_ename); a number; Dbms_output.put_line(‘Salary is ‘||v_sal); lo_sal exception; V_sal:=v_sal+v_Sal/a; begin If v_sal<2000 then raise lo_sal; a:=&a; End if; select empno,ename,sal into Dbms_output.put_line(‘New Salary is ‘|| v_empno,v_ename,v_sal from emp where v_sal); empno=&v_empno; Exception 4

When lo_sal then end if; Raise_application_error(-20001,‘In these if updating then times of Inflation INCREASE THE insert into tbl_audit values('EMP','Update',sysdate); SALARY!!!’); end if; When others then if deleting then Dbms_output.put_line(‘Some Error!!!God insert into tbl_audit knows What???’); values('EMP','Delete',sysdate); End; end if; --pragma exception_init : assigning end; name and number to unnamed --for row level trigger exceptions Create table empcheck(event Declare varchar2(20),level_check Dup_pr_key exception; varchar2(20),date_check date); Pragma exception_init(dup_pr_key,-1); -- row level trigger Begin create or replace trigger Insert into emp(empno,ename) before_update_row_emp_sal values(1111,’ABCD’); before update of sal on emp for each row Dbms_output.put_line(‘One record begin inserted’); insert into empcheck values('Before Insert into emp(empno,ename) update',’Row', sysdate); values(1111,’EFGH’); dbms_output.put_line('Some updation Dbms_output.put_line(‘One more record done on Emp Sal column'); inserted’); end; Exception update emp set sal=sal+100; When dup_pr_key then select * from empcheck; Dbms_output.put_line(sqlcode||‘ :How come more employees with the same rollback; employee number???’); End; Create or replace trigger Practical 13: Triggers trig_emp_upper_name before insert or update on emp --For trigger for each row Create table tbl_audit(tname begin varchar2(20),dml_op if inserting then varchar2(20),date_of_dml date); :new.ename:=upper(:new.ename); --statement /table level trigger end if; create or replace trigger trig_emp_audit if updating then after insert or delete or update on emp :new.ename:=upper(:new.ename); begin end if; if inserting then end; insert into tbl_audit values('EMP','Insert',sysdate);

Recommended publications