Received: from localhost (unknown [200.46.204.184]) by postgresql.org (Postfix) with ESMTP id 613CA2E0093 for ; Mon, 11 Feb 2008 12:23:14 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.184]) (amavisd-maia, port 10024) with ESMTP id 36434-02 for ; Mon, 11 Feb 2008 12:22:58 -0400 (AST) Received: from smtpout24.attiva.biz (smtpout24.attiva.biz [85.37.16.25]) by postgresql.org (Postfix) with ESMTP id EABEF2E008E for ; Mon, 11 Feb 2008 12:22:57 -0400 (AST) Received: from FBCMFE02B07.fbc.local ([192.168.30.95]) by smtpout24.attiva.biz with Microsoft SMTPSVC(6.0.3790.1830); Mon, 11 Feb 2008 17:22:56 +0100 Received: from dream ([88.34.161.130]) by FBCMFE02B07.fbc.local with Microsoft SMTPSVC(6.0.3790.1830); Mon, 11 Feb 2008 17:22:51 +0100 Message-ID: <024301c86cca$591c9c60$3102a8c0@dream> Reply-To: "Shavonne Marietta Wijesinghe" From: "Shavonne Marietta Wijesinghe" To: "Gregory Stark" Cc: References: <010901c86ca6$3b66e870$3102a8c0@dream> <873arzscnu.fsf@oxford.xeocode.com> Subject: Re: Check before INSERT INTO Date: Mon, 11 Feb 2008 17:22:50 +0100 Organization: Studio FORM MIME-Version: 1.0 Content-Type: text/plain; format=flowed; charset="iso-8859-1"; reply-type=original Content-Transfer-Encoding: 7bit X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 6.00.2900.3138 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3198 X-OriginalArrivalTime: 11 Feb 2008 16:22:51.0596 (UTC) FILETIME=[5935F0C0:01C86CCA] X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200802/79 X-Sequence-Number: 30170 Thanks for the reply Grogory. I am trying to do a INSERT INTO. Here is my table. n_gen, n_sheet, tot_n_sheet are defined as Primary Key (Serial not null) id | n_gen | n_sheet | tot_n_sheet ----------+-----------+-----------+------------- a | 1 | 1 | 1 b | 2 | 1 | 2 x | 2 | 2 | 2 u | 3 | 1 | 1 r | 4 | 1 | 3 a | 4 | 2 | 3 s | 4 | 3 | 3 So there are 2 users inserting in to the db. In my ASP page i have a field that shows the value of n_gen +1. So when the 2 users both login at the same time, with different sessions, they both see "7" in the n_gen field. But when they click on the sumbit button only one record is inserted and the other is lost. I though it was possible to change the SQL string before it does the update.. But i can't seem to find a solution for it.. Any idea ?? Thanks, Shavonne ----- Original Message ----- From: "Gregory Stark" To: "Shavonne Marietta Wijesinghe" Cc: Sent: Monday, February 11, 2008 5:03 PM Subject: Re: Check before INSERT INTO > "Shavonne Marietta Wijesinghe" writes: > >> The user updates the DB via ASP. When 2 users click on the submit button >> at the >> same time, only 1 record is inserted. (ERROR: duplicate key violates >> unique >> constraint "my_shevi_pkey") >> >> For example they both send a string like below. >> strSQL = INSERT INTO my_shevi VALUES ('a', 4, 1, 1); >> >> I thought of adding a test before executing the insert into. > > It's not clear to me what you're trying to do. If you're trying to update > an > existing record then you might want something like example 37-1 on this > page: > > http://www.postgresql.org/docs/8.2/interactive/plpgsql-control-structures.html > > If you want to pick the first available n_gen then you're going to have to > repeatedly try inserting until you don't get that error. That will perform > quite poorly when you get to large values. You could do a "SELECT > max(n_gen) > WHERE..." first but even that will be quite a lot of work for your > database. > > Perhaps you should rethink n_gen and use a serial column to generate your > primary key instead. > >> Set SQLN_GEN = oConn.Execute("SELECT upper(N_GEN), upper(N_SHEET), >> upper(TOT_N_SHEET) FROM " & TableName & " WHERE N_GEN='" & n_gen & "' AND >> N_SHEET='" & n_sheet & "' AND TOT_N_SHEET='" & tot_n_sheet & "'") > > For what it's worth your script is a security hole. Look into using query > parameters which in ASP will probably be represented by "?". The method > above > will allow hackers to get direct access to your database and do nasty > things. > > -- > Gregory Stark > EnterpriseDB http://www.enterprisedb.com > Ask me about EnterpriseDB's RemoteDBA services!