NOTE! Now it is even easier to check if a record exists in your database, using App Connect Form Validator. Check the tutorial here: Check if a record exists in your database
I've seen this question posed many times in the ASP Q&A Messageboard... Typically this is done to determine if a user name already exists, common to many login required sites. Many people query the database and then check the values against the returned values through a loop of some sort. If they don't find it, they then add the record.
To me, this seems like unnecessary
overhead. I've also seen it done by selecting the record, and then checking for EOF, like so:
<% |
Each of these methods require at least two trips to the database. Why not let SQL do it all for you? You can check if the user exists and add him if he doesn't in ONE call! You can either do this with a stored procedure or from ASP.
The stored procedure:
CREATE PROCEDURE InsertName |
First, we check if the record exists with the EXISTS
keyword. EXISTS
executes the query we tell it to (the SELECT
) and returns a boolean value. If it finds the record, we return 'This record already exists!'
to our recordset and do nothing else. If it doesn't exist, we execute our INSERT
statement, and then return 'Record Added'
to our recordset. The --
is a comment in SQL, and is equivalent to VBScript's '
or REM
.
With the sotred procedure solution, our ASP code would look like:
<% |
Simple! And only one call. If you prefer not to use a stored procedure, you can easily do the same right from ASP.
<% |
You can also get more creative in your return values so that you could Response.Write
the return value right out to the page. Either way, you're using your resources more efficiently by executing only one database call.
Marco De Luca shares a another clever way to add a record to a table if it doesn't already exist... |
I was reading one of your recent articles on 'Adding a record to a
database table if it doesn't exist' and I figured out another way to do it. I don't know if this will benefit you, but here is the SQL statement for you.
It checks the USERS
table to see if a user name and password exist for a user, then it inserts the user name and password if the user doesn't exist. This might be useful as it works in MS Access. I believe your solution works in all other relational database systems other then MS Access.
---- |
Happy Programming!
Yet another way! (from Bill Wilkinson) |
I think this may be the easiest way yet, since you don't really have to change your SQL INSERT code, at all!
<% |
We use the wonderful error-catching ability of ADODB and VBS to both ignore (from the VBS perspective) and catch (from the ADODB view) a possible error on the INSERT of the possibly duplicate name/value.
The error number -2147217900 equates to &H80040E14 (0x80040E14 for you Java/C/C++/JavaScript people), which is of course the error that is thrown (at least by Access!) when you attempt such a duplicate insertion. That number may or may not be the same for other databases, but it's easy to check which error number you are getting (heck, the above code will tell you, giving an "Unexpected error" message) and modify the code to match.
And, as noted, you don't even have to change your INSERT query one little iota!
(this was taken from the website: http://www.aspfaqs.com/ASPscripts/PrintFAQ.asp?FAQID=106)
Comments
Be the first to write a comment
You must me logged in to write a comment.