toad simplifies ansi joins

Upload: lrb2404

Post on 06-Apr-2018

222 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/3/2019 Toad Simplifies ANSI Joins

    1/7

    Toad Simplifies ANSI Joins

    Starting with Oracle 9i, Oracle recommends that SQL developers use the ANSI joinsyntax instead of the Oracle proprietary (+) syntax. There are several reasons for this

    recommendation, including:

    Easier to segregate and read (without mixing up join versus restriction code)

    Easier to construct join code correctly (especially in the case of outer joins)

    Portable syntax will work on all other ANSI compliant databases, such as MS

    SQL Server, DB2, MySQL, PostgreSQL, et al

    Since its the universally accepted standard, its the general target for all future

    database and third party vendors tools

    The proprietary Oracle outer-join (+) syntax can only be used in one direction at atime, it cannot perform a full outer join

    Plus these additional limitations from the Oracle documentation:o The (+) operator can be applied only to a column, not to an arbitrary

    expression. However, an arbitrary expression can contain one or morecolumns marked with the (+) operator.

    o A condition containing the (+) operator cannot be combined with another

    condition using the OR logical operator.

    o A condition cannot use the IN comparison condition to compare a column

    marked with the (+) operator with an expression.

    o A condition cannot compare any column marked with the (+) operator

    with a sub-query.

    Thus its time to embrace the ANSI join syntax and move into the 21st

    century

    So lets build a relatively simple business oriented query using the MOVIES demodatabase shown in Toad Data Modeler below.

  • 8/3/2019 Toad Simplifies ANSI Joins

    2/7

    Suppose the video store business wants to find out which movie category, movie year,

    movie title, movie copy format, inventory of copies and rental count for just the movies

    from the 70s, 80s, and 90s are in OLD shape and have been rented very few times.Below is the Oracle proprietary outer join syntax for such a SELECT statement.

  • 8/3/2019 Toad Simplifies ANSI Joins

    3/7

    OK so this probably looks quite a lot like code you write in your shop, especially if

    youve been doing Oracle coding for a long time.

    But there are several places where coders could very easily make a mistake:

    Did they remember to do an outer join to get the rental count?

    Did they remember to do an outer join with categories for those without titles?

    Did they remember to do an outer join with movie copies for those without tapes?

    Did they remember to include (+) in non-outer-join WHERE clause conditions?

    The point is that our old tried and true Oracle proprietary syntax is just too much of aheadache to risk all the coding problems that can so easily occur. Plus, we have Oracles

    recommendation to switch to the ANSI syntax.

    So how can we leverage Toad to make this transition (i.e. learning) process easier?

    The answer is simple: Toads Query Builder (formerly called the SQL Modeler) can

    import our SQL text file and parse it into a diagram as shown:

  • 8/3/2019 Toad Simplifies ANSI Joins

    4/7

    Then we simply press the Query Builders A toolbar icon to ask it to be converted to

    ANSI join syntax. And Viola were done.

  • 8/3/2019 Toad Simplifies ANSI Joins

    5/7

    OK looks like there are still a few bugs with Toads reverse engineering parser and its

    join code translation. But Toad still did 80+% of the SQL rewrite for us automatically.

    We just need to send it back over to the editor and spruce it up as shown:

  • 8/3/2019 Toad Simplifies ANSI Joins

    6/7

    Wow even though I tried very hard to carefully construct an originally correct example

    using Oracles syntax, I nonetheless missed a case (see, final SELECT returns seven rows

    instead of only six). Just goes to show you that its true ANSI join syntax makes codingmistakes less likely to occur

    The final proof is shown below, as we can now run the exact same query in Toad for

    SQL Server and get the very same results!!!

  • 8/3/2019 Toad Simplifies ANSI Joins

    7/7