โครงสร้างเบื้องต้น
DECLARE
.
.
.
BEGIN
.
.
.
END;
การประกาศตัวแปรใน PL SQL
v_val1 varchar2(100);
v_val2 number;
v_val3 bolean := true;
v_mo_num cm_trans_po_order.mobile_no%type;
การสร้าง cursor ใน PL SQL
cursor cur_comp is
select * from table where condition;
การประกาศตัวแปร record เพี่อไว้เก็บค่าของ cursor
rec_comp cur_comp%rowtype ;
ตัวอย่างการใช้ cursor
Open cur_comp;
Loop fetch cur_comp into rec_comp;
Exit when cur_comp%notfound;
v_val := rec_comp.comp_code;
End Loop;
Close cur_comp;
ตัวอย่างการใช้ if ใน oracle
if condition then
command;
end if;
ตัวอย่างการใช้ dbms ใน oracle
เป็นคำสั่งสำหรับการส่งค่าออกไป มักใช้ในการ debug
dbms_output.put_line('message' v_val);
การสร้าง function ใน PL SQL
CREATE OR REPLACE FUNCTION function_name(
in_val1
, in_val2 in varchar2
, in_val3 in varchar2 := 'C') return varchar2 is
ประกาศตัวแปร
BEGIN
commande;
END function_name;
CREATE OR REPLACE Procedure procedure_name() is
BEGIN
command;
END procedure_name;
ตัวอย่างการสร้าง Type ใน Oracle
create or replace type c_number_10 is table of NUMBER(10)
ตัวอย่างการสร้าง Trigger
create or replace trigger CM_TRG_CM_REGISTER_MST
before UPDATE OF REGISTER_CODE
on cm_register_master
for each row
begin
update cm_location_master set register_code = :new.register_code
where register_code = :old.register_code;
end;