Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

whow to capture SQL errors in Coldfusion

Posted on 2014-11-06
2
Medium Priority
?
109 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
[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 36

Accepted Solution

by:
SidFishes earned 1500 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 29

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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Hi, I will be creating today a basic tutorial on how we can create a Mail Custom Function and use it where ever we want. The main advantage about creating a custom function is that we can accommodate a range of arguments to pass to the Function and …
PROBLEM: How to add your own buttons to the bottom toolbar with paging info ( result count ). While creating a cfgrid, I ran into an issue where I wanted to embed my own custom buttons where the default ones ( insert / delete / etc… ) are for aes…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

719 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