Search This Blog
Monday, November 14, 2011
Package persistent stage - Example
--------------------------------------------------------------------------------------- Every time ( in the same session ) that a procedure EMP is executed it returns the next 30 rows from table employees until the cursor returns all rows.
CREATE OR REPLACE PACKAGE EMP AS
/* TODO enter package declarations (types, exceptions, methods etc) here */
cursor c is select * from employees;
return_rows number:=30;
procedure PRINTEMPLOYEES ;
END EMP;
----------------------------------------------------------------------------------------
CREATE OR REPLACE PACKAGE BODY EMP AS
procedure PRINTEMPLOYEES
as
cr c%ROWTYPE;
fetched number:=0;
BEGIN
IF NOT c%ISOPEN THEN
OPEN c;
END IF;
loop
fetch c into cr;
EXIT WHEN c%ROWCOUNT > return_rows OR c%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(cr.last_name);
end loop;
return_rows:=return_rows+30;
END PRINTEMPLOYEES;
END EMP;
/
----------------------------------------------------------------------------------------
Friday, November 11, 2011
Check Alert.log for ORA- errors using PLSQL
create or replace procedure write_file
( DIR IN VARCHAR2,filename IN VARCHAR2,text IN varchar2)
is
file utl_file.file_type;
begin
file:=utl_file.fopen(DIR, filename,'a');
utl_file.put_line(file,text);
utl_file.new_line(file);
utl_file.fclose(file);
end;
/
create or replace
procedure read_file ( DIR IN VARCHAR2,filename IN VARCHAR2, report IN VARCHAR2)
is
file utl_file.file_type; --file handler
buffer char(1000);
--EOF boolean:=false;
begin
if not utl_file.is_open(file) then
file := utl_file.fopen(DIR,filename,'R');
end if;
loop
begin
utl_file.get_Line(file,buffer,32767);
--dbms_output.put_line(buffer);
if instr(buffer,'ORA-')=1 then
---dbms_output.put_line(buffer);
write_file(DIR, report,buffer);
end if;
exception
when no_data_found then
exit;
end;
end loop;
-- To run everynight at midnight starting tonight
exec dbms_job.submit
(:v_JobNo, 'proc1;', TRUNC(SYSDATE)+1, 'TRUNC(SYSDATE)+1');
How to call c shared library (.so) from PLSQL
The example uses Oracle Linux 5 32bits , Oracle 11gR2 EE
Call C
csum
int csum(int a, int b)
{
return (a+b);
}
gcc -c -fPIC csum.c -o csum.o
gcc -shared -Wl,-soname,csum.so.1 -o csum.so.1.0.1 csum.o
mv csum.so.1.0.1 /oracle/product/11.2.0/dbhome_1/lib
ln -sf /oracle/product/11.2.0/dbhome_1/lib/csum.so.1.0.1 /oracle/product/11.2.0/dbhome_1/lib/csum.so
ln -sf /oracle/product/11.2.0/dbhome_1/lib/csum.so.1.0.1 /oracle/product/11.2.0/dbhome_1/lib/csum.so.1
create or replace library calc_sum as '/oracle/product/11.2.0/dbhome_1/lib/csum.so';
/
create or replace function getCSUM( a binary_integer,b binary_integer) return binary_integer
as language c
library calc_sum
name "csum";
/
select getCSUM(10,2) from dual;
How to Call C and Java from PLSQL
{
return (a+b);
}
gcc -shared -Wl,-soname,csum.so.1 -o csum.so.1.0.1 csum.o
ln -sf /oracle/product/11.2.0/dbhome_1/lib/csum.so.1.0.1 /oracle/product/11.2.0/dbhome_1/lib/csum.so
ln -sf /oracle/product/11.2.0/dbhome_1/lib/csum.so.1.0.1 /oracle/product/11.2.0/dbhome_1/lib/csum.so.1
/
as language c
library calc_sum
name "csum";
/
ALTER JAVA CLASS "JSUM"
RESOLVER (("C:\app\Administrator\product\11.2.0\dbhome_1\jdk\bin\*" pm)(* public))
RESOLVE;
PLSQL Large objects example
Large objects exercise
As sys create the oracle directory EMP_DATA. Grant the appropriate privileges to user HR to read and write on this directory.
Modify your employees table (
Write a procedure to print your table's date. For the resume print its length,for photo just print the photo size.
Solution
As sys:
create or replace directory IMG as 'C:\Libraries\Pictures';
grant read,write on directory IMG to hr;
As hr:
drop table michael_emp purge;
create table michael_emp
(
emp_id number primary key,
emp_name varchar2(30) not null
);
insert into michael_emp values ( 1,'mmmmm');
insert into michael_emp values ( 2,'ggggg');
commit;
alter table michael_emp add emp_resume clob;
alter table michael_emp add emp_photo bfile;
create or replace procedure updateEMP
as
cursor c is select emp_id,emp_name from michael_emp FOR UPDATE;
cr c%ROWTYPE;
begin
for cr in c loop
update michael_emp set emp_resume=RPAD(cr.emp_name||' resume is: ',32000,'.') ,
emp_photo=bfilename('EMP_DATA','anonymous.jpg')
where current of c;
end loop;
commit;
end;
CREATE OR REPLACE PROCEDURE PRINTEMP AS
cursor c is select * from michael_emp ;
cr c%ROWTYPE;
BEGIN
for cr in c loop
dbms_lob.fileopen(cr.emp_photo);
dbms_output.put_line('Employee ID:'||cr.emp_id||' Name:'||cr.emp_name||' Resume size: '||dbms_lob.getlength(cr.emp_resume) ||' Photo size: '||dbms_lob.getlength(cr.emp_photo));
dbms_lob.fileclose(cr.emp_photo);
end loop;
END PRINTEMP;
Create a small plsql parser
CREATE OR REPLACE FUNCTION pl_parser ( SQL_ IN VARCHAR2) RETURN varchar2
AS
cursor_name INTEGER;
ecode NUMBER;
emesg VARCHAR2(200);
BEGIN
cursor_name := dbms_sql.open_cursor;
DBMS_SQL.PARSE(cursor_name,SQL_,DBMS_SQL.NATIVE);
return 0;
EXCEPTION WHEN OTHERS THEN
ecode := SQLCODE;
emesg := SQLERRM;
return SQLERRM;
END;
/