Click here to Skip to main content
Sign Up to vote bad
good
See more: C#
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 23 Jan '13 - 18:00
Edited 23 Jan '13 - 20:46
adriancs30.9K

Comments
Sergey Alexandrovich Kryukov - 24 Jan '13 - 0:00
Huh? —SA
PIEBALDconsult - 24 Jan '13 - 0:10
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:59
Good point... —SA

5 solutions

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
  Permalink  
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.
  Permalink  
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[^]
  Permalink  
private bool IsValidEmail(string sEmailid)
{
    Regex EmailExpression = new Regex(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
    if(!EmailExpression.IsMatch(sEmailid))
        return false;
    else
        return true;
}
  Permalink  
use this function
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
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)
}
  Permalink  

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 OriginalGriff 216
1 Sergey Alexandrovich Kryukov 169
2 Tadit Dash 154
3 Richard MacCutchan 145
4 Santhosh G_ 115
0 Sergey Alexandrovich Kryukov 10,338
1 OriginalGriff 7,965
2 CPallini 4,201
3 Rohan Leuva 3,522
4 Maciej Los 3,159


Advertise | Privacy | Mobile
Web01 | 2.6.130523.1 | Last Updated 24 Jan 2013
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid