Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server ASP
|
SQL Server ASP Discussions about ASP programming with Microsoft's SQL Server. For more ASP forums, see the ASP forum category.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server ASP 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 April 14th, 2005, 10:05 AM
Authorized User
 
Join Date: Jul 2003
Posts: 56
Thanks: 0
Thanked 0 Times in 0 Posts
Default How do I check a db before inserting a record?

Hi there

Can anyone help me with the following (and hopefully easy) problem:

I need to do an online survey - everyone participating in the survey has a unique ID number. People are only allowed to enter the survey once.

I need some code which, before inserting their data into the database, checks the database first for their ID number. If their ID number exists in the database then the data isn't inserted, if it doesn't then the information is added.

Would this be done in a stored procedure, and if so, how?

Thanks in advance

Lucy

 
Old April 14th, 2005, 12:38 PM
Friend of Wrox
 
Join Date: Jan 2004
Posts: 303
Thanks: 0
Thanked 0 Times in 0 Posts
Default

IF EXISTS (SELECT * FROM MyTable WHERE IdNumber = 'IdNumberHere')

ELSE
INSERT INTO Statement Here
 
Old April 22nd, 2005, 06:06 AM
Registered User
 
Join Date: Apr 2005
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

hi,

a better way is to make the ID column in that survey table "unique". If you do so, you will not be able to insert any row with that ID number..

If you do so, when you try to insert a row with an existing ID, you will get an error (exception).

As i understood, it will be even better if you make that ID column primary key in your table (then, that column will automatically be unique)

hope it helps

 
Old April 25th, 2005, 10:47 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

I think you should check first if the ID exists in the DB before even trying to run the INSERT. Why have the user enter data only to not be inserted?






Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with Inserting new record with detailsview gsrai31 ASP.NET 2.0 Basics 0 November 23rd, 2008 06:29 PM
Problem inserting value from Check Box vivekshah C# 3 June 8th, 2006 08:17 AM
Inserting A Record Set nikotromus Access VBA 3 September 14th, 2004 01:23 PM
Inserting record in Visual foxpro stella2004 SQL Language 2 September 3rd, 2004 06:59 AM
Select record after inserting it whyulil Classic ASP Databases 13 April 26th, 2004 06:05 AM





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