Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Download
Standard view
Full view
of .
Look up keyword or section
Like this
15Activity

Table Of Contents

0 of .
Results for:
No results containing your search query
P. 1
Performing SQL Operations From PLSQL

Performing SQL Operations From PLSQL

Ratings: (0)|Views: 98|Likes:
Published by ypraju

More info:

Published by: ypraju on Dec 20, 2009
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as DOC, PDF, TXT or read online from Scribd
See more
See less

10/22/2011

pdf

text

original

 
By PenchalaRaju.YanamalaPerforming SQL Operations from PL/SQL6 Performing SQL Operations from PL/SQL
This chapter shows how PL/SQL supports the SQL commands, functions, and operators thatlet you manipulate Oracle data.This chapter contains these topics:
Overview of SQL Support in PL/SQL
By extending SQL, PL/SQL offers a unique combination of power and ease of use. You canmanipulate Oracle data flexibly and safely because PL/SQL fully supports all SQL datamanipulation statements (except EXPLAIN PLAN), transaction control statements, functions,pseudocolumns, and operators. PL/SQL also conforms to the current ANSI/ISO SQLstandard.In addition to static SQL discussed in this chapter, PL/SQL also supports dynamic SQL,which enables you to execute SQL data definition, data control, and session controlstatements dynamically. SeeChapter 7, "Performing SQL Operations with Native DynamicSQL".
Data Manipulation
To manipulate Oracle data you can include DML operations, such as INSERT, UPDATE, andDELETE statements, directly in PL/SQL programs, without any special notation, as shown inExample 6-1. You can also include the SQL COMMIT statement directly in a PL/SQLprogram; see"Overview of Transaction Processing in PL/SQL". See alsoCOMMITin the Oracle Database SQL Reference.Example 6-1 Data Manipulation With PL/SQLCREATE TABLE employees_temp AS SELECT employee_id, first_name, last_nameFROM employees;DECLAREemp_id employees_temp.employee_id%TYPE;emp_first_name employees_temp.first_name%TYPE;emp_last_name employees_temp.last_name%TYPE;BEGININSERT INTO employees_temp VALUES(299, 'Bob', 'Henry');UPDATE employees_temp SET first_name = 'Robert' WHERE employee_id = 299;
 
DELETE FROM employees_temp WHERE employee_id = 299RETURNING first_name, last_name INTO emp_first_name, emp_last_name;COMMIT;DBMS_OUTPUT.PUT_LINE( emp_first_name || ' ' || emp_last_name);END;/To find out how many rows are affected by DML statements, you can check the value of SQL%ROWCOUNT as shown inExample 6-2.Example 6-2 Checking SQL%ROWCOUNT After an UPDATECREATE TABLE employees_temp AS SELECT * FROM employees;BEGINUPDATE employees_temp SET salary = salary * 1.05 WHERE salary < 5000;DBMS_OUTPUT.PUT_LINE('Updated ' || SQL%ROWCOUNT || ' salaries.');END;/Wherever you would use literal values, or bind variables in some other programminglanguage, you can directly substitute PL/SQL variables as shown inExample 6-3.Example 6-3 Substituting PL/SQL VariablesCREATE TABLE employees_temp AS SELECT first_name, last_name FROM employees;DECLAREx VARCHAR2(20) := 'my_first_name';y VARCHAR2(25) := 'my_last_name';BEGININSERT INTO employees_temp VALUES(x, y);UPDATE employees_temp SET last_name = x WHERE first_name = y;DELETE FROM employees_temp WHERE first_name = x;COMMIT;END;/With this notation, you can use variables in place of values in the WHERE clause. To usevariables in place of table names, column names, and so on, requires the EXECUTEIMMEDIATE statement that is explained in"Using the EXECUTE IMMEDIATE Statement inPL/SQL".For information on the use of PL/SQL records with SQL to update and insert data, see"Inserting PL/SQL Records into the Database"and"Updating the Database with PL/SQLRecord Values".For additional information on assigning values to PL/SQL variables, see"Assigning a SQLQuery Result to a PL/SQL Variable".
 
When issuing a data manipulation (DML) statement in PL/SQL, there aresome situations when the value of a variable is undefined after the statementis executed. These include:If a FETCH or SELECT statement raises any exception, thenthe values of the define variables after that statement are undefined.If a DML statement affects zero rows, the values of the OUTbinds after the DML executes are undefined. This does not apply to a BULKor multirow operation.
Transaction Control
Oracle is transaction oriented; that is, Oracle uses transactions to ensure data integrity. Atransaction is a series of SQL data manipulation statements that does a logical unit of work.For example, two UPDATE statements might credit one bank account and debit another. It isimportant not to allow one operation to succeed while the other fails.At the end of a transaction that makes database changes, Oracle makes all the changespermanent or undoes them all. If your program fails in the middle of a transaction, Oracledetects the error and rolls back the transaction, restoring the database to its former state.You use the COMMIT, ROLLBACK, SAVEPOINT, and SET TRANSACTION commands tocontrol transactions. COMMIT makes permanent any database changes made during thecurrent transaction. ROLLBACK ends the current transaction and undoes any changes madesince the transaction began. SAVEPOINT marks the current point in the processing of atransaction. Used with ROLLBACK, SAVEPOINT undoes part of a transaction. SETTRANSACTION sets transaction properties such as read-write access and isolation level.See"Overview of Transaction Processing in PL/SQL".
SQL Functions
Example 6-4shows some queries that call SQL functions.Example 6-4 Calling the SQL COUNT Function in PL/SQLDECLAREjob_count NUMBER;emp_count NUMBER;BEGINSELECT COUNT(DISTINCT job_id) INTO job_count FROM employees;SELECT COUNT(*) INTO emp_count FROM employees;END;/
SQL Pseudocolumns
PL/SQL recognizes the SQL pseudocolumns CURRVAL, LEVEL, NEXTVAL, ROWID, andROWNUM. However, there are limitations on the use of pseudocolumns, including the

Activity (15)

You've already reviewed this. Edit your review.
1 hundred reads
jayavardhankoti liked this
Harsha Vardhan liked this
jayavardhankoti liked this
vidyaveda4 liked this
jriver liked this
dwilsondavid liked this
balasekhar123 liked this
anumulaanjireddy liked this

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->