Solved

whow to capture SQL errors in Coldfusion

Posted on 2014-11-06
2
98 Views
Last Modified: 2014-11-29
I have a bunch of queries which insert data into my SQL server and sometimes I get a duplicate record which
throws an error like this

Error Executing Database Query.[Macromedia][SQLServer JDBC Driver][SQLServer]Violation of PRIMARY KEY constraint 'PK_tbl_CNR_D'. Cannot insert duplicate key in object 'dbo.tbl_CNR_D'. The duplicate key value is (C2491035056, 01, 1428310095, ). The specific sequence of files included or processed is: C:\inetpub\wwwroot\parse.cfm, line: 452

how can I capture this in my code and act accordingly ?

I want to avoid to have to fire of a query to see if recordcount GT 0 to avoid this
0
Comment
Question by:AlexPonnath
2 Comments
 
LVL 36

Accepted Solution

by:
SidFishes earned 500 total points
ID: 40433265
Well you can parse the error in cfcatch.detail, evaluate if it's the duplicate error and then handle it however you want but that really is no "better" than handling it properly in the first place, by checking to see if the id already exists in a query.

There's going to be 2 hits to the db in either case

Call with error > call with corrected id
Call with id check > call to insert

To me it doesn't make sense (and it's a lot more complex) to try to handle the error than avoiding it in the first place.
0
 
LVL 28

Expert Comment

by:Pravin Asar
ID: 40434199
With insert/update query you can add result attribute to check number of rescords being updated.
To avoid duplicates, you may check the existence of record matching the pk or fk.

If record exists, may be you want to run update query.

for example

<cfquery datasource="#request.dsn#" name="saveChanges" result="updateResult">
UPDATE table_name
SET
column1 = '#value1#',
column2 = '#value2#'
WHERE column3 = 1
</cfquery>

<cfset recordsChanged = updateResult.recordCount>
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

The technique is by far very Simple! How we can export the ColdFusion query results to DOC file?  Well before writing this I researched a lot in Internet but did not found a good Answer anyways!  So i thought now i should share my small snippet w…
Today, I was working on some optimization and spam-stopping techniques when I encountered Ben Nadel's post to reduce spam feature using Math (http://www.bennadel.com/blog/197-How-I-Stop-Spammers-On-My-ColdFusion-Blog.htm). While this method is not o…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

920 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now