Home > Oracle Database / Applications Tips > Oracle database developer tips > Easy Oracle PL/SQL programming: Assignments, initializations and NULLs
Oracle Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

ORACLE DATABASE DEVELOPER TIPS

Easy Oracle PL/SQL programming: Assignments, initializations and NULLs


John Garmany
12.11.2006
Rating: -4.17- (out of 5)


Oracle tips, scripts, and expert advice
Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google


This is an excerpt from the Rampant TechPress book Easy Oracle PL/SQL Programming: Get Started Fast with Working PL/SQL Code Examples by John Garmany (ISBN: 0-9759135-7-3).

We have already seen that the assignment operator is the colon equal (:=) syntax and we have seen how this is used to assign values to a variable both within the code and upon declaration. It is a good practice to initialize variables to a value when they are declared. This is especially important with number variables to avoid null math errors.

Many programming languages avoid NULLs by requiring that a variable be initialized before use. However, in a database, NULLs are commonly used and are not considered errors. Anytime a row is inserted into a table with only some of the column values defined, NULLs are used for the undefined columns. Thus NULLs are values in the database.

The value of NULL is 'undefined.' That means it is not zero, or one, or anything. It is undefined. Because the value of NULL is undefined it can be tricky to work with.

Null math is using any NULL value in a mathematical equation. Null math always returns a NULL. This is a simple concept, but NULL math is a very common PL/SQL bug.

The example above fails when dbms_output.out_line attempts to convert n_1 + 5 from a number to a character because it has the value of NULL.

In the example below, the value of n_2 is shown. Notice that there is no value when n_1 is added because it is NULL.

This is a very common code bug and it can be hard to locate. So with PL/SQL, the developer must contend not only with division by zero, but also null math.

NULLs can also...


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google



RELATED CONTENT
Oracle database developer tips
How do you create a link between two databases inside a stored procedure?
PL/SQL do's and don't's: Five questions with Steven Feuerstein
Mike Ault's Oracle "good practices": Oracle coding
Introduction to BPEL
SQL puzzles and answers: Finding equal sets
Developer and DBA: Working together for greater efficiency
NULLs in WHERE clauses can be deceptive
The Top 10 (more or less) J2EE best practices
Build a servlet-based application that executes SQL statements against a database
XML an increasingly valuable skill, analysts say

Using Oracle PL/SQL
Oracle tutorial library: SearchOracle.com's learning guides
SELECT statement syntax and examples
Oracle PL/SQL tutorial
PL/SQL datatypes in Oracle
Stored procedures in PL/SQL
PL/SQL functions and triggers in Oracle
How to concatenate rows into a single CLOB in PL/SQL
How to open a ref cursor in a PL/SQL procedure
Working with substitution variables and using EXECUTE IMMEDIATE in PL/SQL
Oracle's free SQL Developer adds database migration tool

RELATED GLOSSARY TERMS
Terms from Whatis.com − the technology online dictionary
PL/SQL  (SearchOracle.com)

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary


be problematic with comparisons. Since NULL is undefined the developer must insure that the correct comparison is used. In the example below, n_1 is a null while n_2 is a number.

A comparison results in a true or a false. Here are some comparisons using the variables above.

No matter how n_1 is used in the comparison, it is still an undefined comparison and therefore false, except if the "not equal" comparison is used. The "not equal" comparison returns true because undefined can never be equal to a value. This is important in the if/then and loop flow control discussed in chapter two. If the developer selects the wrong comparison, it may return a "false" due to a variable not being initialized or return a "true" using the "not equal" comparison. If the variable n_x has not been initialized then the following will always branch to the ELSE clause.

The variable v_line will always be 'always end up here' because n_x < 10 will always be false.

In the above example, v_line will always be 'this is true' because n_x <> 10 will always be true. If this is the behavior desired then the code will work. It will be very confusing to the code maintainer, who has to figure out what the initial coder was trying to accomplish. You should use PL/SQL's natural language structure to make code understandable and avoid "tricks" that accomplish a task but are confusing or difficult to understand. If the code is confusing to read, it's going to be hard to maintain. Good PL/SQL developers get in the habit of using comments to explain what actions are being performed and why you are making calculations. Next we will examine nested blocks and learn about the "scope" of PL/SQL variables.

Rate this Tip
To rate tips, you must be a member of SearchOracle.com.
Register now to start rating these tips. Log in if you are already a member.




DISCLAIMER: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.



Oracle Development Solutions - SQL, J2EE, XML, SOA
HomeNewsTopicsTipsAsk the ExpertsMultimediaWhite PapersProductsBlogs
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 2003 - 2009, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts