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.

Creating a PL/<strong>SQL</strong> Procedure2.6 Creating a PL/<strong>SQL</strong> ProcedureCreate a procedure that lists all books with a specified rating. You can then call thatprocedure with an input parameter (a number from 1 to 10), and the output will be allthe titles of all books with that rating.To create the procedure, if you are not already connected, connect to the database asthe user for the schema you are using for this tutorial. Right-click the Procedures nodein the schema hierarchy on the left side, select Create Procedure, and enter thefollowing information using the Create PL/<strong>SQL</strong> Procedure dialog box.Object Name: list_a_ratingClick OK. A Source window for the new procedure is opened. Enter (or copy andpaste) the following procedure text:PROCEDURE list_a_rating(in_rating IN NUMBER) ASmatching_title VARCHAR2(50);TYPE my_cursor IS REF CURSOR;the_cursor my_cursor;BEGINOPEN the_cursor FOR 'SELECT title FROM books WHERErating = :in_rating' USING in_rating;DBMS_OUTPUT.PUT_LINE('All books with a rating of ' || in_rating || ':');LOOPFETCH the_cursor INTO matching_title;EXIT WHEN the_cursor%NOTFOUND;DBMS_OUTPUT.PUT_LINE(matching_title);END LOOP;CLOSE the_cursor;END;This procedure uses a cursor (named the_cursor) to return only rows where the bookhas the specified rating (in_rating parameter), and uses a loop to output the title ofeach book with that rating.Click the Save icon to save the procedure.As a usage example, after creating the procedure named list_a_rating, you could usethe following statement to return all books with a rating of 10:CALL list_a_rating(10);To run this procedure within Database Designer, right-click LIST_A_RATING in theConnections navigator hierarchy display and select Run. In the Run PL/<strong>SQL</strong> dialogbox, change IN_RATING => IN_RATING to IN_RATING => 10, and click OK. TheLog window display will now include the following output:All books with a rating of 10:Moby DickSoftware WizardryRelated TopicsTutorial: Creating Objects for a Small DatabaseScript for Creating and Using the Library Tutorial Objects<strong>SQL</strong> <strong>Developer</strong> User Interface2-8 <strong>SQL</strong> <strong>Developer</strong> Online Help

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

Saved successfully!

Ooh no, something went wrong!