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>NULL; -- NULL statement does nothing, allows this block to executed and testedEND;/7.3.8 Assigning Values to a Variable With SELECT INTOAnother way to assign values to a variable is by selecting (or fetching) database valuesinto it. In Example 7–8, 10% of an employee's salary is selected into the bonusvariable. Now you can use the bonus variable in another computation or insert itsvalue into a database table.In the example, DBMS_OUTPUT.PUT_LINE is used to display output from the PL/<strong>SQL</strong>program. For more information, see "Inputting and Outputting Data with PL/<strong>SQL</strong>" onpage 7-9.Example 7–8Assigning Values to Variables by SELECTing INTODECLARE -- declare and assign valuesbonus NUMBER(8,2);emp_id NUMBER(6) := 100; -- declare variable and assign a test valueBEGIN-- retreive a value from the employees table and assign to the bonus variableSELECT salary * 0.10 INTO bonus FROM employeesWHERE employee_id = emp_id;DBMS_OUTPUT.PUT_LINE ( 'Employee: ' || TO_CHAR(emp_id)|| ' Bonus: ' || TO_CHAR(bonus) ); -- display dataEND;/7.3.9 Inputting and Outputting Data with PL/<strong>SQL</strong>Most PL/<strong>SQL</strong> input and output is through <strong>SQL</strong> statements, to store data in databasetables or query those tables. All other PL/<strong>SQL</strong> I/O is done through APIs that interactwith other programs. For example, the DBMS_OUTPUT package has procedures such asPUT_LINE. To see the result outside of PL/<strong>SQL</strong> requires another program, such as<strong>SQL</strong>*Plus, to read and display the data passed to DBMS_OUTPUT. <strong>SQL</strong>*Plus does notdisplay DBMS_OUTPUT data unless you first issue the <strong>SQL</strong>*Plus command SETSERVEROUTPUT ON. For information on <strong>SQL</strong>*Plus SET command, see <strong>SQL</strong>*Plus SETCommands .Example 7–9 show the use of DBMS_OUTPUT.PUTLINE. Note the use of SETSERVEROUTPUT ON to enable output.Example 7–9Using DBMS_OUTPUT to Display Output-- enable SERVEROUTPUT in <strong>SQL</strong>*Plus to display with DBMS_OUTPUT.PUT_LINE-- this enables SERVEROUTPUT for this <strong>SQL</strong>*Plus session onlySET SERVEROUTPUT ONDECLAREanswer VARCHAR2(20); -- declare a variableBEGIN-- assign a value to a variableanswer := 'Maybe';-- use PUT_LINE to display data from the PL/<strong>SQL</strong> blockDBMS_OUTPUT.PUT_LINE( 'The answer is: ' || answer );END;/PL/<strong>SQL</strong>: Usage Information 7-9

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

Saved successfully!

Ooh no, something went wrong!