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>■■■■■■■■■■■■■■Declaring Variables and ConstantsUsing Identifiers in PL/<strong>SQL</strong>Assigning Values to a Variable With the Assignment OperatorUsing LiteralsDeclaring and Assigning Variables With DEFAULT or NOT NULLAssigning Values to a Variable With SELECT INTOInputting and Outputting Data with PL/<strong>SQL</strong>Using %ROWTYPE and %TYPE Attributes to Declare Data TypesUsing PL/<strong>SQL</strong> Control StructuresUsing Local PL/<strong>SQL</strong> Subprograms in PL/<strong>SQL</strong> BlocksWorking With PL/<strong>SQL</strong> Data StructuresProcessing Queries with PL/<strong>SQL</strong>Using Dynamic <strong>SQL</strong> in PL/<strong>SQL</strong>Using Bind Variables7.3.1 Using PL/<strong>SQL</strong> Block StructureAs Example 7–1 shows, a PL/<strong>SQL</strong> block has three basic parts: a declarative part(DECLARE), an executable part (BEGIN .. END), and an exception-handling(EXCEPTION) part that handles error conditions. For a discussion of exceptionhandling, see "Handling PL/<strong>SQL</strong> Errors" on page 7-21.Only the executable part is required. The optional declarative part is written first,where you define types, variables, and similar items. These items are manipulated inthe executable part. Exceptions raised during execution can be dealt with in theexception-handling part.Note the comments that are added to the PL/<strong>SQL</strong> code. See "Using Comments" onpage 7-4. Also, not the use of DBMS_OUTPUT.PUT_LINE to display output. See"Inputting and Outputting Data with PL/<strong>SQL</strong>" on page 7-9.Example 7–1Simple PL/<strong>SQL</strong> Block-- the following is an optional declarative partDECLAREmonthly_salaryNUMBER(6);number_of_days_worked NUMBER(2);pay_per_dayNUMBER(6,2);-- the following is the executable part, from BEGIN to ENDBEGINmonthly_salary := 2290;number_of_days_worked := 21;pay_per_day := monthly_salary/number_of_days_worked;-- the following displays output from the PL/<strong>SQL</strong> blockDBMS_OUTPUT.PUT_LINE('The pay per day is ' || TO_CHAR(pay_per_day));-- the following is an optional exception part that handles errorsEXCEPTIONWHEN ZERO_DIVIDE THENpay_per_day := 0;PL/<strong>SQL</strong>: Usage Information 7-3

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

Saved successfully!

Ooh no, something went wrong!