Presentation is loading. Please wait.

Presentation is loading. Please wait.

Oracle 8i Building PL/SQL Blocks. Creating PL/SQL Blocks CURSORS PROCEDURES FUNCTIONS PACKAGES TRIGGERS CURSORS PROCEDURES FUNCTIONS PACKAGES TRIGGERS.

Similar presentations


Presentation on theme: "Oracle 8i Building PL/SQL Blocks. Creating PL/SQL Blocks CURSORS PROCEDURES FUNCTIONS PACKAGES TRIGGERS CURSORS PROCEDURES FUNCTIONS PACKAGES TRIGGERS."— Presentation transcript:

1 Oracle 8i Building PL/SQL Blocks

2 Creating PL/SQL Blocks CURSORS PROCEDURES FUNCTIONS PACKAGES TRIGGERS CURSORS PROCEDURES FUNCTIONS PACKAGES TRIGGERS

3 Structures 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;

4 Declaration Section Datatypes: NUMBER –10 -130  10 125 –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  10 125 –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

5 Characters 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

6 More 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

7 Variables 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;

8 SELECT INTO DECLARE my_variable NUMBER(4); my_second_var NUMBER(4); BEGIN my_variable := 100; SELECT unit_price INTO my_second_var FROM product WHERE product_id = 50; Dbms_output.Put_line(my_variable); Dbms_output.Put_line('The unit price is: ' || my_second_var); END; DECLARE my_variable NUMBER(4); my_second_var NUMBER(4); BEGIN my_variable := 100; SELECT unit_price INTO my_second_var FROM product WHERE product_id = 50; Dbms_output.Put_line(my_variable); Dbms_output.Put_line('The unit price is: ' || my_second_var); END;

9 Scope of Variables DECLARE v1 NUMBER; v2 NUMBER; /******************** The new block begins.... *******************/ BEGIN v1 := 100; v2 := 200; DECLARE v3 NUMBER; BEGIN v3 := v2 + v1; END; --v3 cannot be accessed FROM here END; DECLARE v1 NUMBER; v2 NUMBER; /******************** The new block begins.... *******************/ BEGIN v1 := 100; v2 := 200; DECLARE v3 NUMBER; BEGIN v3 := v2 + v1; END; --v3 cannot be accessed FROM here END;

10 Constants DECLARE c_new_sal CONSTANT NUMBER (4) := 500; v_current_sal NUMBER(9,2); BEGIN SELECT salary INTO v_current_sal FROM sales_person WHERE 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; DECLARE c_new_sal CONSTANT NUMBER (4) := 500; v_current_sal NUMBER(9,2); BEGIN SELECT salary INTO v_current_sal FROM sales_person WHERE 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;

11 Records 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;

12 ROWTYPE record_name table_name%ROWTYPE; Example: DECLARE product_rec product%ROWTYPE; record_name table_name%ROWTYPE; Example: DECLARE product_rec product%ROWTYPE;

13 Passing 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];

14 %type example DECLARE comp_name customer.company_name%type; cont customer.contact_name%type; BEGIN SELECT company_name, contact_name INTO comp_name,cont FROM customer WHERE customer_id = 300; Dbms_output.Put_line ('company: '|| comp_name); Dbms_output.Put_line ('contact: '||cont); END; DECLARE comp_name customer.company_name%type; cont customer.contact_name%type; BEGIN SELECT company_name, contact_name INTO comp_name,cont FROM customer WHERE customer_id = 300; Dbms_output.Put_line ('company: '|| comp_name); Dbms_output.Put_line ('contact: '||cont); END;

15 RECORD example DECLARE TYPE cust_type IS RECORD( comp_name customer.company_name%type, cont customer.contact_name%type); cust_rec cust_type; BEGIN SELECT company_name, contact_name INTO cust_rec FROM customer WHERE customer_id = 300; Dbms_output.Put_line ('company: '|| cust_rec.comp_name); Dbms_output.Put_line ('contact: '|| cust_rec.cont); END; DECLARE TYPE cust_type IS RECORD( comp_name customer.company_name%type, cont customer.contact_name%type); cust_rec cust_type; BEGIN SELECT company_name, contact_name INTO cust_rec FROM customer WHERE customer_id = 300; Dbms_output.Put_line ('company: '|| cust_rec.comp_name); Dbms_output.Put_line ('contact: '|| cust_rec.cont); END;

16 ROWTYPE DECLARE cust_rec customer%ROWTYPE; BEGIN SELECT * INTO cust_rec FROM customer WHERE customer_id = 300; Dbms_output.Put_line ('company: '|| cust_rec.company_name); Dbms_output.Put_line ('contact: '|| cust_rec.contact_name); END; DECLARE cust_rec customer%ROWTYPE; BEGIN SELECT * INTO cust_rec FROM customer WHERE customer_id = 300; Dbms_output.Put_line ('company: '|| cust_rec.company_name); Dbms_output.Put_line ('contact: '|| cust_rec.contact_name); END;

17 Manipulating Data DECLARE prod_rec product%ROWTYPE; BEGIN INSERT INTO product VALUES (100, 'CD', 'American Suppliers', 19.99); SELECT * INTO prod_rec FROM product WHERE 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; DECLARE prod_rec product%ROWTYPE; BEGIN INSERT INTO product VALUES (100, 'CD', 'American Suppliers', 19.99); SELECT * INTO prod_rec FROM product WHERE 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;


Download ppt "Oracle 8i Building PL/SQL Blocks. Creating PL/SQL Blocks CURSORS PROCEDURES FUNCTIONS PACKAGES TRIGGERS CURSORS PROCEDURES FUNCTIONS PACKAGES TRIGGERS."

Similar presentations


Ads by Google