International Developer Logo Last Updated 19.11.08 at 11.48
 
TUTORIALS

Writing Portable SQL


Daniel winter   11.09.06

Use CASE expressions instead of IF statements

CASE statements can be embedded directly within SQL statements, thereby dramatically reducing the amount of code as well as making the query significantly more legible. In many situations, a single CASE statement can replace the use of a cursor which loops over an IF statement. Consider the following example.

 

UPDATE Products

   SET sale_price = CASE WHEN retail_price >= 50.00

     THEN retail_price * 0.80;

     ELSE retail_price * 0.90

       END;

 

Avoid data formatting

Data formatting is the job of the display logic, not the data retrieval code. If the application needs to display the name of the day sourced from a date field in the database, select and return the full date to the application. The display logic can then manipulate the data as required. Obviously, this rule applies specifically to SELECT statements. In the case of INSERT and UPDATE queries, data will need to be formatted in the manner expected by the particular database in question (see Portable Data Types, below).

 


Application issues arising from the use of portable SQL

For the most part, fully portable SQL can be written using simple statements that only utilise SQL-92 conventions. However, things get a little tricky when queries need to be optimised or other advantages offered by modern-day database engines are on offer. For example, a statement such as the following will run on any SQL-92 compliant server.

 

SELECT [cols] FROM [tables]

  [WHERE conditions]

  [GROUP BY cols]

  [HAVING conditions]

  [ORDER BY cols]




   Previous Page  1 2 3 4 5 6 7 8 Next Page   

HAVE YOUR SAY
This article is rated  Rate this article