Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How do I ignore errors generated from a duplicate index insert within a trigger

Posted on 2007-11-23
2
Medium Priority
?
521 Views
Last Modified: 2008-02-01
Hi,

I have a legacy Adaptive Server Anywhere 6 database which i need to add a trigger to.

Within this trigger I need to add some data to a table but I dont care if the row already exists.  I dont really want to do a "select 1 from..." to test it's presence as I feel this imposes an overhead on the database.

Is there any way I can simply trap the duplicate primary key index error and ignore it?

Thanks.

James.
0
Comment
Question by:JAMES
2 Comments
 
LVL 19

Accepted Solution

by:
grant300 earned 2000 total points
ID: 20339897
Well, as a rule, you are better off with clean SQL than you are counting on handling an error condition.  Error handling is generally not highly optimized code in any application and DBMSs are no exception.  If you do a test, you will find that handling the error is many times slower than doing the IF EXISTS (SELECT * FROM MYTABLE WHERE....) RETURN

In addition, if you are going to insert a row, you are going to need the index page(s) in memory anyway.  The test SELECT makes sure they are there so the actual insert does not pay for the I/O twice.  Of course, if the index pages are already in memory, the overhead of the IF EXISTS (SELECT.... is pretty low to begin with.  I am also not sure what sequence the INSERT operation uses.  If it inserts (and logs) the row before it goes and tries to update the index before failing, you are eating a whole bunch of extra overhead.

Let's see, what else....  You have a problem if the trigger is fired on a multi-row verb.  ASA supports row-level triggers however, it is generally faster to write triggers as you would for ASE which only fires a trigger once per verb.  Set operations are usually the best way to go.

Finally, if your trigger causes an SQL error, you may find that ASA (this is an old version so excuse the imprecise answer) will stop execution of the trigger at that point and push the error up the stack.  This could make it tough or impossible to complete the base transaction.

So go ahead and do the IF EXISTS (SELECT.... operation in the trigger.

Two other thoughts:  If you are going to update the row if it exists, the cleanest way to write the logic is to always attempt the UPDATE and then look at the effected row count and/or status to see if it succeeded.  If it did, you can exit; if not, go ahead and do the INSERT.  This works because an UPDATE that finds no row to UPDATE is not an error and all it will have done is search the index for the row.

The other tidbit of information is that you should always to an EXISTS rather than a NOT EXISTS for performance reasons.  In this case, IF EXISTS (SELECT.....) RETURN is the quickest way out.

Regards,
Bill
0
 

Author Comment

by:JAMES
ID: 20340076
Wow - thank you very much for taking that time to write your comments.

"IF EXISTS" is what I will be using :-)

Many thanks again.

James.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In today's business world, data is more important than ever for informing marketing campaigns. Accessing and using data, however, may not come naturally to some creative marketing professionals. Here are four tips for adapting to wield data for insi…
Creating a Cordova application which allow user to save to/load from his Dropbox account the application database.
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question