Sie sind auf Seite 1von 6

lacture chapter 6/7 of sg1

--------------------declare
begin
create table emp_d as select * from emp;
end;
/
============================
dynamic delete statement
============================
set serveroutput on
declare
c_id number;
v_result number;
begin
c_id:=dbms_sql.open_cursor;
dbms_sql.parse(c_id,'delete from '||'&tname',dbms_sql.native);
v_result:=dbms_sql.execute(c_id);
dbms_output.put_line(v_result);
dbms_sql.close_cursor(c_id);
end;
/
============================
dynamic delete statement with exception
============================
declare
c_id number;
v_result number;
begin
c_id:=dbms_sql.open_cursor;
dbms_sql.parse(c_id,'delete from '||'&tname',dbms_sql.native);
v_result:=dbms_sql.execute(c_id);
dbms_output.put_line(v_result);
dbms_sql.close_cursor(c_id);
exception
when others then
dbms_output.put_line(sqlerrm);
end;
/
===============================
native dynamic (execute immediate using only for the single or 1 row output (sel
ect,insert,delete,update))
===============================
set serveroutput on
declare
v_statement varchar2(32767);
begin
v_statement:='delete from '||'&tname';
execute immediate v_statement;
exception
when others then
dbms_output.put_line(sqlerrm);
end;
/

create table dept_copy as select * from dept;


alter table dept_copy;
drop column dept_total;
-- Table altered.
-----------------------------bind variable (:1,:2,:3)
-----------------------------set serveroutput on
declare
v_dept number(6):=&deptno;
v_name varchar2(20):='&name';
v_loc varchar2(20):='&loc';
v_statement varchar2(32767);
begin
v_statement:='insert into dept_copy values(:1,:2,:3)';
execute immediate v_statement using v_dept,v_name,v_loc;
exception
when others then
dbms_output.put_line(sqlerrm);
end;
/
===========================================
create the function using native dynamic
===========================================
set serveroutput on
create or replace function f_rowcount (tname varchar2) return number
as
v_statement varchar2(32767);
v_count number;
begin
v_statement:='select count (*) from '|| tname;
execute immediate v_statement into v_count;
return v_count;
end;
/
------call the functions using nynamic sql
------select f_rowcount ('emp') from dual;
select table_name,f_rowcount(table_name) from user_tables;
==============================================
native dynamic (execute open for using for the multi row output (select,insert,d
elete,update))
==============================================
create or replace procedure list_employees(p_value varchar2,p_choice char) as
type emp_rf is ref cursor;
emp_cv emp_rf;
emprec emp%rowtype;
stmt varchar2(32767);
begin
if p_choice='d' then

stmt:='select * from emp where deptno=:1';


elsif p_choice='j' then
stmt:='select * from emp where job=:1';
elsif p_choice='m' then
stmt:='select * from emp where mgr=:1';
end if;
open emp_cv for stmt using p_value;
loop
fetch emp_cv into emprec;
exit when emp_cv%notfound;
dbms_output.put_line(emprec.empno||'--'||emprec.ename||'--'||emprec.sal);
end loop;
end;
declare
begin
list_employees(10,'d');
end;
/
declare
begin
list_employees('CLERK','j');
end;
/
declare
begin
list_employees(7839,'m');
end;
/
==============================
GET THE COMPLETE DETAILS OF THE GIVEN ARRGUMENT
==============================
select dbms_metadata.get_ddl('TABLE','EMPN','VRG') from dual;
select dbms_metadata.get_ddl('PROCEDURE','LIST_EMPLOYEES','VRG') from dual;
=================
specifying invoker's rights
=================
grant execute on f_rowcount to scott;
select table_name,vrg.f_rowcount(table_name) from user_tables;
------------------authid current_user
------------------set serveroutput on
create or replace function f_rowcount (tname varchar2) return number
authid current_user
as
v_statement varchar2(32767);
v_count number;
begin

v_statement:='select count (*) from '|| tname;


execute immediate v_statement into v_count;
return v_count;
end;
/
select table_name,vrg.f_rowcount(table_name) from user_tables;
=====================
bulk binding
=====================
declare
type dept_tabtype is
table of dept%rowtype;
depts dept_tabtype;
begin
select * bulk collect into depts
from dept;
for i in 1..depts.count loop
dbms_output.put_line(depts(i).deptno||'--'||depts(i).dname);
end loop;
end;
/
========================================
========================================
declare
type emp_tabtype is
table of emp%rowtype;
emps emp_tabtype;
type emp_id is table of number index by binary_integer;
v_emp_id emp_id;
begin
select * bulk collect into emps
from emp;
for i in 1..emps.count loop
dbms_output.put_line(emps(i).empno||'--'||emps(i).ename||'--'||emps(i).sal||'--'
||emps(i).deptno);
emps(i).sal:=emps(i).sal*1.5;
v_emp_id(i):=emps(i).empno;
end loop;
forall i in 1..emps.count
update emp
set row=emps(i) where empno=v_emp_id(i);
end;
/
=============================================
how to send the email
=============================================
CREATE OR REPLACE FUNCTION get_file(filename VARCHAR2, dir VARCHAR2 := 'D_TEMP')
RETURN VARCHAR2 IS
contents VARCHAR2(32767);
file BFILE := BFILENAME(dir, filename);
BEGIN
DBMS_LOB.FILEOPEN(file, DBMS_LOB.FILE_READONLY);

contents := UTL_RAW.CAST_TO_VARCHAR2(DBMS_LOB.SUBSTR(file));
DBMS_LOB.CLOSE(file);
RETURN contents;
END;
/

BEGIN
UTL_MAIL.SEND_ATTACH_VARCHAR2(
sender => 'gulzarsk@hotmail.com',
recipients => 'gulzarsk@hotmail.com',
message =>
'<HTML><BODY>See attachment</BODY></HTML>',
subject => 'Oracle Notes',
mime_type => 'text/html',
attachment => get_file('1.txt'),
att_inline => false,
att_mime_type => 'text/plain',
att_filename => '1.txt');
END;
/
CREATE OR REPLACE FUNCTION scott.get_image(filename VARCHAR2, dir VARCHAR2 := 'D
_TEMP')
RETURN RAW IS
image RAW(32767);
file BFILE := BFILENAME(dir, filename);
BEGIN
DBMS_LOB.FILEOPEN(file, DBMS_LOB.FILE_READONLY);
image := DBMS_LOB.SUBSTR(file);
DBMS_LOB.CLOSE(file);
RETURN image;
END;
/
BEGIN
UTL_MAIL.SEND_ATTACH_RAW(
sender => 'gulzarsk@yahoo.com',
recipients => 'gulzarsk@vsnl.com',
message => '<HTML><BODY>See attachment</BODY></HTML>',
subject => 'MyNotes',
mime_type => 'text/html',
attachment => scott.get_image('spiderman.jpg'),
att_inline => true,
att_filename => 'spiderman.jpg');
END;
/

==================================================================
ed to edit the current/last query
into in select
into in fatch
into in fatch

show parameter smtp_out_server;


http://www.pearsonvue.com/oracle
http://certview.oracle.com

Das könnte Ihnen auch gefallen