Solved

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

Posted on 2007-11-23
2
518 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 19

Accepted Solution

by:
grant300 earned 500 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

Get Database Help Now w/ Support & Database Audit

Keeping your database environment tuned, optimized and high-performance is key to achieving business goals. If your database goes down, so does your business. Percona experts have a long history of helping enterprises ensure their databases are running smoothly.

Question has a verified solution.

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

Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
Many companies are looking to get out of the datacenter business and to services like Microsoft Azure to provide Infrastructure as a Service (IaaS) solutions for legacy client server workloads, rather than continuing to make capital investments in h…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

752 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