Sunday 28 October 2012

pl sql record and pl sql table simple example



pl sql table and record example withe clear explanation:
=======================================

create or replace procedure plsql_table_ex_proc is
/*type empno is table of emp.empno%type index by binary_integer;
type ename is table of emp.ename%type index by binary_integer;
type job is table of emp.job%type index by binary_integer;
type sal is table of emp.sal%type index by binary_integer;
type deptno is table of dept.deptno%type index by binary_integer;
type grade is table of salgrade.grade%type index by binary_integer;
*/
type sample_rec is record
(
v_empno emp.empno%type,
v_ename emp.ename%type,
v_job emp.job%type ,
v_sal emp.sal%type,
v_deptno dept.deptno%type,
v_grade salgrade.grade%type
);
type sample_tab is table of sample_rec index by binary_integer;
data_table sample_tab;
cursor data_cur is select empno,ename,job,sal,d.deptno,grade
from emp e,dept d,salgrade s
where e.deptno=d.deptno
and (e.sal between s.losal and s.hisal);
begin
open data_cur;
fetch data_cur bulk collect into data_table;
forall i in 1..data_table.count
insert into (select * from emp_dept_salgrade) values data_table(i);
dbms_output.put_line('no of records inserted : '||data_table.count);
close data_cur;
select empno,ename,job,sal,d.deptno,grade
bulk collect into data_table
from emp e,dept d,salgrade s
where e.deptno=d.deptno
and (e.sal between s.losal and s.hisal);
for i in 1..data_table.count
loop
insert into emp_dept_salgrade(empno,ename,sal,job,deptno,grade)
values ( data_table(i).v_empno,data_table(i).v_ename,data_table(i).v_sal,data_table(i).v_job,
data_table(i).v_deptno,data_table(i).v_grade);
end loop;
dbms_output.put_line('no of records inserted : '||data_table.count);
end;
/

Thursday 25 October 2012

SQL*Loader-522: lfiopn failed for file

SQL*Loader-522: lfiopn failed for file

this error cames when we port data through sql*loader from csv file.
solution: This error came because you don't have write access with bin folder.Check you have write permissions on bin folder to place log files of sql*loader.

Wednesday 10 October 2012

Sql and plsql Material



For Sql and Pl Sql Material click on below link

http://documents.club-oracle.com/downloads.php?do=file&id=6989

Forall example with plsql table type in Oracle


set echo on

DECLARE

   TYPE emp_aat IS TABLE OF emp%ROWTYPE
      INDEX BY PLS_INTEGER;
   aa_emps emp_aat;

BEGIN

   FORALL i IN 1 .. aa_emps.COUNT
      UPDATE emp
      SET    sal = aa_emps(i).sal * 1.1
      WHERE  empno = aa_emps(i).empno;

END;
/

Forall Example with Record Type In Oracle


set serveroutput on
set echo on

DECLARE

   TYPE subset_rt IS RECORD
   ( empno    emp.empno%TYPE
   , ename    emp.ename%TYPE
   , hiredate emp.hiredate%TYPE
   , deptno   emp.deptno%TYPE );

   TYPE subset_aat IS TABLE OF subset_rt
      INDEX BY PLS_INTEGER;

   aa_subset subset_aat;

BEGIN

   /* Some "source" data... */
   SELECT ROWNUM, owner, created, 20
   BULK   COLLECT INTO aa_subset
   FROM   all_objects
   WHERE  ROWNUM <= 10;

   /* Record-based insert and subset of columns... */
   FORALL i IN 1 .. aa_subset.COUNT
      INSERT INTO (SELECT empno, ename, hiredate, deptno FROM emp)
      VALUES aa_subset(i);

   DBMS_OUTPUT.PUT_LINE(TO_CHAR(SQL%ROWCOUNT) || ' rows inserted.');

END;
/

Monday 8 October 2012

counting no of characters in a given string in Oracle

We can find no of characters in a string  based on this query in two different ways:
=======================================================

select NVL(LENGTH(REGEXP_REPLACE(ENAME, '[^A]')),0) REG_COUNT,
       length(ename) - length(replace(ename, 'A')) COUNT,
       ENAME,
       SAL,
       JOB,
       DEPTNO
  FROM SCOTT.EMP;