Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
how to check whether the email already in databse means insert should not happended if it doest exist then we need to insert so we need to write if condition in stored procedure.

<modified>
How to check whether the email is already existed in database or not. New insert should not happened if the email is existed. Do we need to write something like "if else " condition in store procedure to check this out?
</modified>
Posted
Updated 23-Jan-13 20:46pm
v5
Comments
Sergey Alexandrovich Kryukov 24-Jan-13 0:00am    
Huh?
—SA
PIEBALDconsult 24-Jan-13 0:10am    
You really can't, because there are optional (though rare) things that can be added. For example:
Bob@WidgetCo.com and Bob@[192.168.123.456] may be the same address even though they don't look the same.
Read the spec for more information.
Sergey Alexandrovich Kryukov 24-Jan-13 2:59am    
Good point...
—SA

SQL
select count(*) from `table_name` where `email` = "this@email.com";


this will return a value.

if the value is 1 means, the email is existed once.
if 2 means, existed twice.
if 0 means...

there, you can check the email is used or not.
 
Share this answer
 
SQL
CREATE PROCEDURE [dbo].[InsertName_sp]
(
  @Email varchar(50)
)
AS
IF EXISTS(SELECT 'True' FROM Resource WHERE Email= @Email)
BEGIN

  SELECT 'This Email already exists!'
END
ELSE
BEGIN

  SELECT 'Email Added'
  INSERT into Resource(Email) VALUES(@Email)
END
 
Share this answer
 
Oh, so it's a database, that makes more sense. Which database? Does it support EXISTS?

Personally, I've never had a use for EXISTS, but it may be better in the stated situation.

This way doesn't use EXISTS; it's probably better if you have a number of emails to test and insert:

INSERT INTO SomeTable (SomeText)
SELECT email
FROM (SELECT @email email) A
LEFT OUTER JOIN SomeTable B
ON A.email=B.SomeText
WHERE B.SomeText IS NULL


I would definitely avoid an IF/ELSE; just too untidy.

If you are using SQL Server, you may also be interested in the MERGE statement
http://technet.microsoft.com/en-us/library/bb510625.aspx[^]
 
Share this answer
 
v2
C#
private bool IsValidEmail(string sEmailid)
{
    Regex EmailExpression = new Regex(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
    if(!EmailExpression.IsMatch(sEmailid))
        return false;
    else
        return true;
}
 
Share this answer
 
use this function
C#
function validateEmail(email) {
    var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\
".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA
-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
    return re.test(email);
}

or this
SQL
function ValidateEmail(mail)
{
 if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(myForm.emailAddr.value))
  {
    return (true)
  }
    alert("You have entered an invalid email address!")
    return (false)
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900