Oracle 8i Building PL/SQL Blocks. Creating PL/SQL Blocks CURSORS PROCEDURES FUNCTIONS PACKAGES...
-
Upload
elfreda-rogers -
Category
Documents
-
view
218 -
download
1
Transcript of Oracle 8i Building PL/SQL Blocks. Creating PL/SQL Blocks CURSORS PROCEDURES FUNCTIONS PACKAGES...
Oracle 8iOracle 8i
Building PL/SQL BlocksBuilding PL/SQL Blocks
Creating PL/SQL BlocksCreating PL/SQL Blocks
• CURSORS
• PROCEDURES
• FUNCTIONS
• PACKAGES
• TRIGGERS
• CURSORS
• PROCEDURES
• FUNCTIONS
• PACKAGES
• TRIGGERS
Structures of a PL/SQL blockStructures of a PL/SQL block
• The Declaration section• The Execution section• The Exception section
DECLARE---BEGIN---EXCEPTION---END;
• The Declaration section• The Execution section• The Exception section
DECLARE---BEGIN---EXCEPTION---END;
Declaration SectionDeclaration Section
• Datatypes:• NUMBER
– 10-130 10125
– NUMBER, NUMBER(PRE),NUMBER(PRE,SC)– Default precision – 38– Scale: -84 to 127 (auto round)– Subtypes:
• DEC/DECIMAL/NUMERIC – fixed point• FLOAT – 128BIT• REAL – 64BIT• INT/INTEGER/SMALLINT
• Datatypes:• NUMBER
– 10-130 10125
– NUMBER, NUMBER(PRE),NUMBER(PRE,SC)– Default precision – 38– Scale: -84 to 127 (auto round)– Subtypes:
• DEC/DECIMAL/NUMERIC – fixed point• FLOAT – 128BIT• REAL – 64BIT• INT/INTEGER/SMALLINT
CharactersCharacters
• CHAR – max 2000 chars (default: 1)
• CHARACTER – same(ANSI)
• VARCHAR2 – max 4000 chars
• NCHAR
• NVARCHAR
• LONG – Up To 2GB
• CHAR – max 2000 chars (default: 1)
• CHARACTER – same(ANSI)
• VARCHAR2 – max 4000 chars
• NCHAR
• NVARCHAR
• LONG – Up To 2GB
More datatypesMore datatypes
• LONG RAW – 2GB – binary
• RAW – binary – 2000 bytes
• CLOB – char large object(same as LONG)
• NCLOB – unicode
• BLOB – binary large object
• BFILE – file pointer
• DATE
• LONG RAW – 2GB – binary
• RAW – binary – 2000 bytes
• CLOB – char large object(same as LONG)
• NCLOB – unicode
• BLOB – binary large object
• BFILE – file pointer
• DATE
VariablesVariables
DECLARE
my_variable NUMBER(4);
my_second_var NUMBER(4):=22;
my_variable:=77;
DECLARE
my_variable NUMBER(4);
my_second_var NUMBER(4):=22;
my_variable:=77;
SELECT INTOSELECT INTO
DECLAREmy_variable NUMBER(4);my_second_var NUMBER(4);
BEGINmy_variable := 100;SELECT unit_priceINTO my_second_varFROM productWHERE product_id = 50;Dbms_output.Put_line(my_variable);Dbms_output.Put_line('The unit price is: ' || my_second_var);
END;
DECLAREmy_variable NUMBER(4);my_second_var NUMBER(4);
BEGINmy_variable := 100;SELECT unit_priceINTO my_second_varFROM productWHERE product_id = 50;Dbms_output.Put_line(my_variable);Dbms_output.Put_line('The unit price is: ' || my_second_var);
END;
Scope of VariablesScope of Variables
DECLAREv1 NUMBER;v2 NUMBER;/********************The new block begins ....*******************/
BEGINv1 := 100;v2 := 200;DECLARE
v3 NUMBER;BEGIN
v3 := v2 + v1;END;--v3 cannot be accessed FROM here
END;
DECLAREv1 NUMBER;v2 NUMBER;/********************The new block begins ....*******************/
BEGINv1 := 100;v2 := 200;DECLARE
v3 NUMBER;BEGIN
v3 := v2 + v1;END;--v3 cannot be accessed FROM here
END;
ConstantsConstants
DECLAREc_new_sal CONSTANT NUMBER (4) := 500;v_current_sal NUMBER(9,2);
BEGINSELECT salaryINTO v_current_salFROM sales_personWHERE sales_person_id = 804;Dbms_output.Put_line ('Current salary: ' || v_current_sal);v_current_sal := v_current_sal + c_new_sal;Dbms_output.Put_line ('New salary: ' || v_current_sal);
END;
DECLAREc_new_sal CONSTANT NUMBER (4) := 500;v_current_sal NUMBER(9,2);
BEGINSELECT salaryINTO v_current_salFROM sales_personWHERE sales_person_id = 804;Dbms_output.Put_line ('Current salary: ' || v_current_sal);v_current_sal := v_current_sal + c_new_sal;Dbms_output.Put_line ('New salary: ' || v_current_sal);
END;
RecordsRecords
DECLARE
TYPE product_type IS RECORD
(f_product_id NUMBER(5),
f_product_name VARCHAR2(25),
f_supplier_name product.supplier_name%type,
f_unit_price product.unit_price%type);
product_rec product_type;
DECLARE
TYPE product_type IS RECORD
(f_product_id NUMBER(5),
f_product_name VARCHAR2(25),
f_supplier_name product.supplier_name%type,
f_unit_price product.unit_price%type);
product_rec product_type;
ROWTYPEROWTYPE
record_name table_name%ROWTYPE;
Example:
DECLARE
product_rec product%ROWTYPE;
record_name table_name%ROWTYPE;
Example:
DECLARE
product_rec product%ROWTYPE;
Passing ValuesPassing Values
• record_name.field_name := value;• If you used %ROWTYPE:
record_name.col_name := value;
• SELECT col1, col2 INTO record_name.field_name1, record_name.field_name2[WHERE clause];
• If you use %ROWTYPE:SELECT * INTO record_name FROM table_name [WHERE clause];
• record_name.field_name := value;• If you used %ROWTYPE:
record_name.col_name := value;
• SELECT col1, col2 INTO record_name.field_name1, record_name.field_name2[WHERE clause];
• If you use %ROWTYPE:SELECT * INTO record_name FROM table_name [WHERE clause];
%type example%type example
DECLAREcomp_name customer.company_name%type;cont customer.contact_name%type;
BEGINSELECT company_name, contact_nameINTO comp_name,contFROM customerWHERE customer_id = 300;Dbms_output.Put_line ('company: '|| comp_name);Dbms_output.Put_line ('contact: '||cont);
END;
DECLAREcomp_name customer.company_name%type;cont customer.contact_name%type;
BEGINSELECT company_name, contact_nameINTO comp_name,contFROM customerWHERE customer_id = 300;Dbms_output.Put_line ('company: '|| comp_name);Dbms_output.Put_line ('contact: '||cont);
END;
RECORD exampleRECORD example
DECLARETYPE cust_type IS RECORD(
comp_name customer.company_name%type,cont customer.contact_name%type);
cust_rec cust_type;BEGIN
SELECT company_name, contact_nameINTO cust_recFROM customerWHERE customer_id = 300;Dbms_output.Put_line ('company: '|| cust_rec.comp_name);Dbms_output.Put_line ('contact: '|| cust_rec.cont);
END;
DECLARETYPE cust_type IS RECORD(
comp_name customer.company_name%type,cont customer.contact_name%type);
cust_rec cust_type;BEGIN
SELECT company_name, contact_nameINTO cust_recFROM customerWHERE customer_id = 300;Dbms_output.Put_line ('company: '|| cust_rec.comp_name);Dbms_output.Put_line ('contact: '|| cust_rec.cont);
END;
ROWTYPEROWTYPE
DECLAREcust_rec customer%ROWTYPE;
BEGINSELECT *INTO cust_recFROM customerWHERE customer_id = 300;Dbms_output.Put_line ('company: '|| cust_rec.company_name);Dbms_output.Put_line ('contact: '|| cust_rec.contact_name);
END;
DECLAREcust_rec customer%ROWTYPE;
BEGINSELECT *INTO cust_recFROM customerWHERE customer_id = 300;Dbms_output.Put_line ('company: '|| cust_rec.company_name);Dbms_output.Put_line ('contact: '|| cust_rec.contact_name);
END;
Manipulating DataManipulating Data
DECLAREprod_rec product%ROWTYPE;
BEGININSERT INTO productVALUES (100, 'CD', 'American Suppliers', 19.99);SELECT *INTO prod_recFROM productWHERE product_id = 100;Dbms_output.Put_line (prod_rec.product_id);Dbms_output.Put_line (prod_rec.product_name);Dbms_output.Put_line (prod_rec.supplier_name);Dbms_output.Put_line (prod_rec.unit_price);COMMIT;
END;
DECLAREprod_rec product%ROWTYPE;
BEGININSERT INTO productVALUES (100, 'CD', 'American Suppliers', 19.99);SELECT *INTO prod_recFROM productWHERE product_id = 100;Dbms_output.Put_line (prod_rec.product_id);Dbms_output.Put_line (prod_rec.product_name);Dbms_output.Put_line (prod_rec.supplier_name);Dbms_output.Put_line (prod_rec.unit_price);COMMIT;
END;