12.07.2015 Views

Oracle SQL Developer

Oracle SQL Developer

Oracle SQL Developer

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Utilizing the Main Features of PL/<strong>SQL</strong>7.3.10.2 Using the %TYPE Attribute to Declare VariablesThe %TYPE attribute provides the data type of a variable or table column. This isparticularly useful when declaring variables that will hold values of a table column.For example, suppose you want to declare variables as the same data type as columnsemployee_id and last_name in table employees. To declare variables namedempid and emplname that have the same data type as the table columns, use dotnotation and the %TYPE attribute. See Example 7–11.Example 7–11Using %TYPE With Table ColumnsDECLARE -- declare variables using %TYPE attributeempid employees.employee_id%TYPE; -- employee_id data type is NUMBER(6)emplname employees.last_name%TYPE; -- last_name data type is VARCHAR2(25)BEGINempid := 100301; -- this is OK because it fits in NUMBER(6)-- empid := 3018907; -- this is too large and will cause an overflowemplname := 'Patel'; -- this is OK because it fits in VARCHAR2(25)DBMS_OUTPUT.PUT_LINE('Employee Id: ' || empid); -- display dataDBMS_OUTPUT.PUT_LINE('Employee name: ' || emplname); -- display dataEND;/Declaring variables with %TYPE has two advantages. First, you need not know theexact data type of the table columns. Second, if you change the database definition ofcolumns, such as employee_id or last_name, the data types of empid andemplname in Example 7–11 change accordingly at run time.See Also:%TYPEPL/<strong>SQL</strong> User's Guide and Reference for information on7.3.11 Using PL/<strong>SQL</strong> Control StructuresControl structures are the most important PL/<strong>SQL</strong> extension to <strong>SQL</strong>. Not only doesPL/<strong>SQL</strong> let you manipulate <strong>Oracle</strong> data, it lets you process the data using conditional,iterative, and sequential flow-of-control statements such as IF-THEN-ELSE, CASE,FOR-LOOP, WHILE-LOOP, EXIT-WHEN, and GOTO.7.3.11.1 Conditional Control With IF-THENOften, it is necessary to take alternative actions depending on circumstances. TheIF-THEN statement lets you execute a sequence of statements conditionally. The formsof the statement can be IF-THEN, IF-THEN-ELSE, or IF-THEN-ELSEIF-ELSE. TheIF clause checks a condition, the THEN clause defines what to do if the condition istrue and the ELSE clause defines what to do if the condition is false or null.Example 7–12 shows a simple use of the IF-THEN statement.Example 7–12Using a Simple IF-THEN StatementDECLAREsales NUMBER(8,2) := 10100;quota NUMBER(8,2) := 10000;bonus NUMBER(6,2);emp_id NUMBER(6) := 120; -- use employee 120 for testingBEGINIF sales > (quota + 200) THENbonus := (sales - quota)/4;UPDATE employees SET salary = salary + bonus WHERE employee_id = emp_id;END IF;PL/<strong>SQL</strong>: Usage Information 7-11

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!