12.07.2015 Views

Oracle SQL Developer

Oracle SQL Developer

Oracle SQL Developer

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Utilizing the Main Features of PL/<strong>SQL</strong>7.3.11.4 Sequential Control With GOTOThe GOTO statement lets you branch to a label unconditionally. The label, anundeclared identifier enclosed by double angle brackets, must precede an executablestatement or a PL/<strong>SQL</strong> block. When executed, the GOTO statement transfers control tothe labeled statement or block, as shown in Example 7–19.Example 7–19Using the GOTO StatementDECLARE -- declare variablesp VARCHAR2(30);n PLS_INTEGER := 37; -- test any integer > 2 for prime, here 37BEGIN-- loop through divisors to determine if a prime numberFOR j in 2..ROUND(SQRT(n))LOOPIF n MOD j = 0 THEN -- test for primep := ' is NOT a prime number'; -- not a prime numberGOTO print_now;END IF;END LOOP;p := ' is a prime number';DBMS_OUTPUT.PUT_LINE(TO_CHAR(n) || p); -- display dataEND;/7.3.12 Using Local PL/<strong>SQL</strong> Subprograms in PL/<strong>SQL</strong> BlocksSubprograms are named PL/<strong>SQL</strong> blocks that can be called with a set of parametersfrom inside a PL/<strong>SQL</strong> block. PL/<strong>SQL</strong> has two types of subprograms: procedures andfunctions.Example 7–20 is an example of a declaration of a PL/<strong>SQL</strong> procedure in a PL/<strong>SQL</strong>block. Note that the v1 and v2 variables are declared as IN OUT parameters to asubprogram. An IN OUT parameter passes an initial value that is read inside asubprogram and then returns a value that has been updated in the subprogram.Example 7–20 Declaring a Procedure With IN OUT ParametersDECLARE -- declare variables and subprogramsfname VARCHAR2(20) := 'randall';lname VARCHAR2(25) := 'dexter';PROCEDURE upper_name ( v1 IN OUT VARCHAR2, v2 IN OUT VARCHAR2) ASBEGINv1 := UPPER(v1); -- change the string to uppercasev2 := UPPER(v2); -- change the string to uppercaseEND;BEGINDBMS_OUTPUT.PUT_LINE(fname || ' ' || lname ); -- display initial valuesupper_name (fname, lname); -- call the procedure with parametersDBMS_OUTPUT.PUT_LINE(fname || ' ' || lname ); -- display new valuesEND;/Example 7–21 is an example of a declaration of a PL/<strong>SQL</strong> function in a PL/<strong>SQL</strong> block.Note that the value returned by the function is used directly in the DBMS_OUTPUT.PUT_LINE statement. Note that the v1 and v2 variables are declared as INparameters to a subprogram. An IN parameter passes an initial value that is readPL/<strong>SQL</strong>: Usage Information 7-15

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

Saved successfully!

Ooh no, something went wrong!