101communication LLC CertCities.com -- The Ultimate Site for Certified IT Professionals
   Certification Communities:  Home  Microsoft®  Cisco®  Oracle®  A+/Network+"  Linux/Unix  More  
    CertCities.com is proud to present our sponsor this month: Thomson Prometric
Editorial
Choose a Cert
News
Exam Reviews
Features
Columns
Salary Surveys
Free Newsletter
Cert on the Cheap
Resources
Forums
Practice Exams
Cert Basics
Links Library
Tips
Pop Quiz
Industry Releases
Windows Certs
Job Search
Conferences
Contributors
About Us
Search


Advanced Search
CertCities.com

CertCities.com
Let us know what you
think! E-mail us at:
.. Home .. Certifications .. Oracle .. Columns ..Column Story Thursday, August 21, 2003

TechMentor Conference & Expo PDF Brochure - Download It Now!

Save 30% on CertCities.com's Guide to IT Certification on the Cheap

 SELECTions   Damir Bersinic
Damir Bersinic



 9i and the Evolution of Oracle SQL
In this column, Damir looks at the new MERGE statement and INSERT data options of Oracle 9i.
by Damir Bersinic  
6/20/2001 -- I guess I had to deal with it sooner or later: Oracle has just announced the release of their most recent database incarnation, Oracle 9i. For those of you who are members of the Oracle Technology Network (http://otn.oracle.com) and running Oracle on Sun Solaris, HP/UX or Linux, you can now download this latest version from the OTN Web site.

The questions I usually get asked about any new release of software tend to be along the lines of "Is it worth it to upgrade?" or "What are some of the new features in the product?" and so on. To answer these at one time would take more space than I'm allowed to use for this column, but these questions need to be answered. So this column is the first in a series that will look at all the new features of 9i and how you can get the most out of them

Plus ça Change, plus C'est la Même Chose
Oracle has made significant strides in bringing their variant of the SQL language up to the standards accepted by the industry. This includes many of the features that the ANSI SQL99 standard has suggested that vendors should implement. Some of these include support for the ANSI standard join syntax, use of the DEFAULT keyword to assign default values explicitly to columns of a table when an INSERT or UPDATE takes place, and many others. However, being true to form and introducing innovations that benefit its users, Oracle has also extended the SQL language to include functionality beyond the standard. After all, as any RDBMS vendor will tell you, the standard is only a guideline and things can always be done better.

The MERGE Statement
One SQL extension that is welcome is the new MERGE statement. The idea behind this statement is simple: Check to see if a row already exists in a table based upon the rows in another table and, if it does, UPDATE the existing row. If not, INSERT a new row into the table.

An example might help here:

MERGE INTO RetiredEmployees AS Retired
        USING Employees AS Emps
        ON (Retired.EmpId = Emps.EmpID) 
WHEN MATCHED THEN
        UPDATE SET EffectiveDate=Emps.TerminationDate
WHEN NOT MATCHED THEN
        INSERT VALUES (Emps.EmpID, Emps.LastName, 
        Emps.FirstName, Emps.Address, Emps.HomePhone, 
        Emps.HireDate, Emps.TerminationDate); 

In this example, Oracle will join the RetiredEmployees table with the Employees table. If a match is found (i.e., the EmpID exists in both tables), an UPDATE of the RetiredEmployees row will be made setting the effective retirement date to the value of the TerminationDate column in the Employees table. If no match is found, a row will be inserted into the RetiredEmployees table.

You probably don't want to experience this kind of MERGE statement in your company as it may indicate that your job is not that secure, but it's a good example of how the new MERGE statement is useful. To perform the same task prior to Oracle 9i, you either had to write a PL/SQL block to accomplish the task or use two SQL statements -- one to do the join and perform the UPDATE, and the other to perform the INSERT based upon the results of an outer join between the two tables. Using a single statement to accomplish both tasks at once certainly makes life easier.

You Want To INSERT Data into More Than One Table in a Single Statement?
In many data warehousing environments, when data is transferred from the source system it will need to be transformed or massaged to fit the structure of the data warehouse. This is normal since data warehousing design differs greatly from the design of an OLTP system. However, because of this inherent design difference, it is sometimes necessary to take data from a single OLTP source table, or a source data file derived from the OLTP system, and split it into several different tables in the data warehouse.

Prior to Oracle 9i this meant that you had to load the data into a temporary table in the database and then perform several INSERT statements into the appropriate tables, or create a complex control file for a SQL*Loader load. In Oracle 9i you now have the capability to INSERT data into several tables at the same time when using a subquery on the source table (i.e., you are using an INSERT … SELECT statement).

Oracle 9i has four variations of the multi-table INSERT:

  • the unconditional INSERT
  • the conditional ALL INSERT
  • the conditional FIRST INSERT
  • the pivoting INSERT

The unconditional INSERT will add data from the source subquery into all of the tables specified in the INSERT ALL statement, as in this example:

        INSERT ALL
        INTO SalaryHistory VALUES (empid, datehired, salary) 
        INTO ManagerHistory VALUES (empid, mgrid, SYSDATE) 

        SELECT EmployeeID EMPID, HireDate DATEHIRED, 
                Sal SALARY, ManagerID MGRID
        FROM Employees
        WHERE DeptID = 100; 
In this case, for every row retrieved by the subquery, a row will be inserted into the SalaryHistory table and a row will also be inserted into the ManagerHistory table. If you did not want all rows to be added to both tables, you can perform a conditional ALL INSERT, as in the following example:
   

        INSERT ALL
        WHEN Salary > 30000
    INTO SalaryHistory VALUES (empid, datehired, salary) 
    WHEN MgrID = 200
        INTO ManagerHistory VALUES (empid, mgrid, SYSDATE) 

        SELECT EmployeeID EMPID, HireDate DATEHIRED, 
                  (Sal*12) SALARY, ManagerID MGRID
        FROM Employees
        WHERE DeptID = 100; 

In this example, rows will be inserted into the SalaryHistory table only when the value of the Salary is greater than 30000 (the annual salary of the employee is more than $30,000). Rows will not be inserted into the ManagerHistory table unless the manager ID is 200.

A conditional FIRST INSERT differs from the conditional ALL INSERT in that you may specify multiple WHEN conditions on the INSERT statement, but only the first WHEN condition that evaluates to true will result in an INSERT taking place. For example, if we changed the previous example to a conditional FIRST INSERT, if the value of Salary was greater than 30000, the INSERT into the SalaryHistory table would take place; if not, then if the MgrID of the employee was 200, an INSERT into the ManagerHistory table would take place; otherwise no INSERT would take place. The code would change to the following:

        INSERT FIRST 
        WHEN Salary > 30000
        INTO SalaryHistory VALUES (empid, datehired, salary) 
        WHEN MgrID = 200
        INTO ManagerHistory VALUES (empid, mgrid, SYSDATE) 
        ELSE 
        INTO EmpHistory VALUES (empid, mgrid, datehiread, salary) 

        SELECT EmployeeID EMPID, HireDate DATEHIRED, 
                  (Sal*12) SALARY, ManagerID MGRID
        FROM Employees
        WHERE DeptID = 100; 

As you can see, the ability to insert data into multiple tables from a single source is quite handy. However, in the above examples, the source and target tables existed in the same database. How about if you needed to import data from an external source and then insert its records into multiple tables, or take a single row in a table and split it into several INSERT statements that would insert several rows into the same table? To accomplish that you may need to load the data into temporary tables and then issue the above statements, right? Not necessarily.

Check back next time when I talk about pivoting INSERTS and how to create external tables in Oracle 9i.


Damir Bersinic is an independent consultant, trainer, and author of Oracle 8i DBA: SQL & PL/SQL Certification Bible (ISBN 0764548328), Oracle 8i DBA: Architecture and Administration Certification Bible (ISBN 0764548174) from Wiley & Sons, as well as a number of titles on Windows, Active Directory and SQL Server. He holds several industry designations including Oracle OCP DBA (in 4 Oracle versions), MCSE, MCDBA, MCT and CTT+. He can be reached at .

 

More articles by Damir Bersinic:

Post your comment below, or better yet, go to our Discussion Forums and really post your mind.
Current CertCities.com user Comments for "9i and the Evolution of Oracle SQL"
7/13/01 - sunthorn  says: good
8/12/01 - Dimitar  says: Hello, I would like to ask one question - is the Oracle 9i DBA Certification very marketable? Oracle is the most used database but I have not seen it being listed as the most popular certifications on the cercities website. Can you explain to me the dilemma? Regards, Dimitar Popov.
8/13/01 - Damir  says: Dimitar, If you are asking about the Oracle 9i DBA certification - you're a little early. It just got announced and the exams are still in Beta. However, the Oracle 8i DBA certification is pretty popular but a lot more difficult to achieve than an MCSE or a A+ cert. While the mass appeal may not be as great at present, there is a demand for people that have proven Oracle 8i DBA skills, which the certification helps to provide. Damir
12/16/01 - Vijayalaxmi  says: The article is really good and very helpful in knowing the new features of Oracle 9i. Its explained very neatly and easy to understand. Hope to get some more information on Oracle 9i features in SQL and PLSQL .
2/28/02 - Anonymous says: How do i do the same process in oracle 8i like what modifications i have to do to the control file to update another table with the data while loading it into a main table. Thanks for your response.
8/3/02 - Sudhir Nikam  from India says: If I have to test an application which was built on Oracle 8i for compatibility on Oracle 9i which are the focus areas for testing?
8/16/02 - P.V.Sudhakar  from Hyderabad, India says: I have to do the same in Oracle 8.1.7, but i am unable to get a clear solution of doing it. One way i thought is doing SELECT ..... FROM A WHERE empid IN (SELECT ...... FROM B), but feel like it's a bit complex, whereas, the one you specified looks simple. So, could you specify any other way or solution to do the same as i need to check it for around more than 100 tables. With Best Regards, Sudhakar
9/26/02 - Erik  says: Multi-Table Update : Hi Damir, Love your article. Is there also a multi-table update??
10/2/02 - Sunil Sharma  from Bombay India says: this document is really good and simple to understand about the new features of oracle 9i. hope we may be able to find more & more documents like this in days to come.
3/14/03 - Nick  from UK says: Hi, I have recently set foot into a analyst role, providing reporting functions and process development for an electricity company who have an Oracle based database. Is there a multiple table update statement for 8i?
4/6/03 - dilip .d  from india says: this article has explained the new features of oracle9i in simple and easy language. i hope the author will continue this process for the beginers in the same way
4/6/03 - dilip.d  from india says: hi damir i have cleared 3 oracle 8i dba papers(administration,backup and recovery,networking) i want to to write rest of papers in a same version i want to know 8i version is still valid
Add your comment here:
Name: (optional)
Location: (optional)
E-mail Address: (optional)
Comments:  
 
top

Sponsored Link:
Don’t let your IT Investment Go to Waste: Get Certified with Thomson Prometric!

Home | Microsoft | Cisco | Oracle | A+/Network+ | Linux/Unix | MOUS | List of Certs
Advertise | Certification Basics | Conferences | Contact Us | Contributors | Features | Forums | Links | News | Pop Quiz | Industry Releases | Reviews | Tips
Search | Site Map | MCPmag.com | TCPmag.com | OfficeCert.com | TechMentor Conferences | 101communications | Privacy Policy
This Web site is not sponsored by, endorsed by or affiliated with Cisco Systems, Inc., Microsoft Corp., Oracle Corp., The Computing Technology Industry Association, Linus Torvolds, or any other certification or technology vendor. Cisco® and Cisco Systems® are registered trademarks of Cisco Systems, Inc. Microsoft, Windows and Windows NT are either registered trademarks or trademarks of Microsoft Corp. Oracle® is a registered trademark of Oracle Corp. A+®, i-Net+™, Network+™, and Server+™ are trademarks and registered trademarks of The Computing Technology Industry Association. (CompTIA). Linux™ is a registered trademark of Linus Torvalds. All other trademarks belong to their respective owners.
All content copyright 2000-03 101communications LLC, unless otherwise noted. All rights reserved.
Reprints allowed with written permission from the publisher. For more information, e-mail