Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

PL/SQL Blocks


Published on

  • Be the first to comment

  • Be the first to like this

PL/SQL Blocks

  1. 1. Anar Godjaev PL/SQL Blocks declare --<Variables, Cursors, user defined exception> begin --<Sql statements> --<PL/SQL statements> exception When <exception name> then --<Actions to perform when errors, occur> end; EX: declare v_variablevarchar2(15); begin selectcolumn_name intov_variable fromtable_name; exception whenothers then null; end; create or replace procedure <procedure_name> as --<declare any variables here> begin null; end; create or replace function <function_name> return <datatype> as --<declare any veriables here> begin return<value>; end; Variable Types: scalar (single value) -- varchar2, number, date, subtype composite (records) -- record, plsql table reference (pointer) -- ref cursor LOB (large objects) -- BLOB, CLOB, LOB Bind variables -- variables defined outside plsql block, ex: .NET, Java, sqlplus Scalar data types:
  2. 2. Anar Godjaev Char, varchar2, long, number, binary_integer, boolean, date, timestamp, timestamp withtimezone, timestamp with local time zone declare str_jobvarchar2(15); int_countbinary_integer := 0; n_tot_salnumber(9,2); cons_numconstant number(3,2) := 8.25; b_validboolean not null default true; _name employees.last_name%TYPE; date_dtdate := sysdate+7; subtypetype_my_varchar varchar2(100); type_my_charmy_varchar; EX: declare n_deptnonumber(4); --departments.department_no%TYPE n_location_idnumber(4); --departments.locations_id%TYPE begin selectdepartment_id, location_id intov_deptno, v_location_id fromdepartments where department_name= 'Sales'; end; Composite data types: Records: rec_tbl_based is table_name%ROWTYPE; rec_cur_based is cur_name%ROWTYPE; typetype_rec_var_based is (field1 datatype1, field2 datatype2, .. ) rec_my_recordtype_rec_var_based selectcol_name into rec_my_record.col_name from table_name; selectrec_my_record.col_name from dual; EX: declare rec_empemployees%ROWTYPE; n_employee_numbernumber := 1024; begin select* into emp_recfrom employees whereemployee_id= n_employee_number; insert into retired_emps (emp_no, ename, job, mng, hire_date, leave_date, sql, comm, rept_no) values (emp_rec.employee_id, emprec.lastname,...); commit; end;
  3. 3. Anar Godjaev EX: declare typeemp_record_typeis record ( last_namevarchar2(25), job_idvarchar2(10), salarynumber(8,2) ); emp_recordemp_record_type; begin emp_record.last_name:= 'ERGEM'; dbms_output.put_line(emp_record.last_name); end; Plsql Tables: typetype_tbl_city_codes is table of varchar2(50); typetype_tbl_city_codes is table of t_city.code%TYPE; typetype_tbl_based_on_cur is table of type_rec_var_based [index by binary integer]; tbl_city_codestype_tbl_city_codes; select code into tbl_city_codes(15) from t_city where city_id=15; selecttbl_city_codes(15) from dual; typeename_table_type is table of employees.lastname%TYPE index by binary_integer; ename_tableename_table_type; EX: declare -- declare the table type typeename_table_typeis table of varchar2(100);--employees.last_name%TYPE index by binary integer; typehiredate_table_typeis table of dateindex by binary_integer; -- declare the table type variable ename_tableename_table_type; hiredate_tablehiredate_table_type; begin -- initialize the table by a call to the constructor ename_table:= ename_table_type(); -- extend the table ename_table.extend(1); -- assign a value ename_table(1) := 'cameron'; -- indexed tables do not need initializing and extending
  4. 4. Anar Godjaev -- hiredate_table := hiredate_table_type(); -- hiredate_table.extend(10); hiredate_table(8) := sysdate + 7; ifename_table.exists(1) then --insert into ... dbms_output.put_line(ename_table(1)); dbms_output.put_line(hiredate_table(8)); end if; end; Pl/SQL table attributes: Exists [(n)] Count First Last Prior [(n)] Next [(n)] Trim [(n)] -- rename one element from end table Delete [(n)] -- remove elements EX: declare typeemp_table_typeis table of employees%ROWTYPE index by binary_integer; my_emp_tableemp_table_type; v_countnumber(3) := 104; begin fori in 10.. v_countloop select* into my_emp_table(i) from employees whereemployee_id= i; end loop; fori in my_emp_table.first .. my_emp_table.last loop dbms_output.put_line(my_emp_table.lastname(i)); end loop; end; Variables in nested blocks: declare d_birth_date date; begin declare d_birth_date date; begin ... Outer.d_birth_date:=to_Date(’03-NOV-2013’,’DD-MON-YYYY’); end;
  5. 5. Anar Godjaev ... end; EX: <<outer>>-- label the outer block Declare n_sum_salnumber(10,2); n_dept_nonumber not null := 60; --n_employee_idemployees.employee_id%TYPE; Begin declare n_sum_salnumber(10,2) := 1111; begin /* select sum(salary) into outer.n_sum_sal from employees wheredepartment_id = n_dept_no; */ -- call to outer block variable outer.n_sum_sal:= 2532.22; end; dbms_output.put_line('The outer sum salary is: ' || to_char(n_sum_sal)); /* selecthire_date from employees whereemployee_id = n_employee_id; */ end;