Wrox Programmer Forums
|
Classic ASP Databases Discuss using ASP 3 to work with data in databases, including ASP Database Setup issues from the old P2P forum on this specific subtopic. See also the book forum Beginning ASP.NET Databases for questions specific to that book. NOT for ASP.NET 1.0, 1.1, or 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Classic ASP Databases section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 20th, 2004, 02:12 AM
Authorized User
 
Join Date: May 2004
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default primary key violation

How to avoid primary key violation error
without using Select Query to loop through for checking EOF before the insert

regards,
Murali

 
Old May 20th, 2004, 02:15 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

What is the methodology you are using to check that? Can you post some code of it to have a better idea about that?

-Vijay G
 
Old May 20th, 2004, 07:36 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Are you connecting to just one database or do you have any JOINS?

 
Old May 20th, 2004, 08:18 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Hey,

If you are using SQL, specify the ID as an identity. If you are using Oracle, use a sequence number (which requires a trigger). Then when you do the insert, specify no ID value. These are obviously numeric ID's, by the way.

Then when you do the insert, you specify no value for the ID parameter, and it auto updates to the next value.

Brian
 
Old May 20th, 2004, 10:14 PM
Authorized User
 
Join Date: May 2004
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi vijay,brian
say for sample Tablename called PRODUCT_MASTER i have 3 columns in Product_Master
3 columns are composite primary key.
Now my Question is How to avoid primary key violation without loop through the table
strSQL= "select * from Product_Master where Column1='value1' and column2='value2' and column3='value3'"
set rs=conobj.execute(strSql)
if rs.eof then
conobj.execute("insert Statement")
else
response.write "Message"
end if
Note i dont want like this above code becoz it will take much more time to loop through even thoug we have index in the column i need to trap the error number and display the message




 
Old May 21st, 2004, 02:37 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

What is the database you are using? Is that MSSQL server db or Oracle or ACCESS or anything else? That should show us light in which way to proceed with.

If you are using MSSQL, then you can do it this way.

Write a procedure something like this.
Create proc proc_name(Param_list_for_Insert_Values)
as
If (Select count(*) from TableName where column1=Value1 and column2=Value2 and column3=value3)>0
  Begin
     Select -1 as Result
     Return
  End
Else
  Begin
     BEGIN TRAN
     Insert TableName Values(.....)
     If @@ERROR<>0
         ROLLBACK TRAN
     ELSE
         COMMIT TRAN
     Select @@ERROR as Result
  End

You can call this procedure in you ASP code, and pass the insert values as parameters, which on execution would return

-1 --> if already record exists with those composite key values.
any +ve number --> if transaction fails due to any other reason.
ZERO --> If insert succeeds.

This is a commonly used methodology. Since you do this at the backend using stored proc, performance is better than what you were trying to do from the code you posted there and you don't have to loop through your select to see if violation occurs.

Anyone, pls post if you have a better solution to this.

Hope that helps.
Cheers!

-Vijay G
 
Old May 21st, 2004, 03:34 AM
Authorized User
 
Join Date: May 2004
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Vijay,
i am using msSQL
If u use select statement in storeproc to count also it will take much more time even i have clustured index.. coz i've got 2 lakhs of data in my table so i hav got one idea----
would u please suggest me whether i can proceed with that.
" we can collect the ado Error Numbers and write a function and include it in the page & trap the Err Number and check with the (Include ) function " this is my idea ...........
But I need ur help to proceed with that..................
thanx in Advance

 
Old May 21st, 2004, 12:25 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Hello,

You could insert the data, in a transactional mode (begin transaction, commit transaction), and check the @@errors property if there is an error, then rollback the transaction. Using this method may actually return an error which you could catch in your code.

However, for errors in ASP, You would have to use On Error Goto, and I don't think ASP supports that... It's been a while for me using ASP, so I'm probably not the best person to make a recommendation.

Brian
 
Old May 21st, 2004, 09:24 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hi Muralikeane,

Actually the volume of data I have in my environment is more than 30 lakhs of records. The volume of transactions that happens is high, if not that huge. I would see a maximum of around 300 to 800 records being inserted per minute. Not only that, there are other applications that do other jobs like checking for the expiry of the time limit and moving those records to another table called archive and many other update activities too happen. At an average, a minimum of more than a lakh of records being processed per day. If that had been posing me problems, I wouldn't have suggested you that. Also I have seen this as a widely used methodology. So you can go ahead using it.

As suggested in my previous post, when you go for a stored proc, it is a compiled planned query that exists in your sql server, which increases the performance, than doing as you have given in your post.

Quote:
quote:strSQL= "select * from Product_Master where Column1='value1' and column2='value2' and column3='value3'"
set rs=conobj.execute(strSql)
if rs.eof then
conobj.execute("insert Statement")
else
response.write "Message"
end if
In the above way, you will have to connect to DB twice. Once to check for primary key violation condition and the second time you would actually have to pass the Insert statement with values, then you got to check if that fails or succeeds. So this has multiple hops to actually finish off the transaction and in every hop you got to connect to DB from your frontend. Also, when you send a statement to DB for processing, be it select or insert or update whatever, it got to be compiled to check for errors, and then gets executed, which takes more time to be processed.

Whereas in the stored proc method that I suggested, you will have to call the proc once with all parameters, and the responibiltiy is with the sql server to decide if to insert and commit or to roll back, and return the status of transaction all in one hop. You dont have to connect to database multiple times to do a single valid transaction. So I would strongly suggest you to go with it.

If that can wait, please look around if you get any post supporting this and can go by majority

Cheers!

-Vijay G





Similar Threads
Thread Thread Starter Forum Replies Last Post
Violation of PRIMARY KEY constraint??? twyce C# 1 September 9th, 2008 01:23 AM
Foreign key not updating with Primary key xavier1945 BOOK: Access 2003 VBA Programmer's Reference 2 July 4th, 2007 09:48 PM
Violation of primary key dhoward VB.NET 2002/2003 Basics 4 May 31st, 2007 10:19 AM
Primary Key Violation error code hasanali00 BOOK: ASP.NET Website Programming Problem-Design-Solution 1 April 19th, 2005 07:41 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.