Click here to Skip to main content
15,921,577 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
SQL
ALTER proc [dbo].[Mysp_tblUsers_insert]
@Name nvarchar(30),
@Email nvarchar(50),
@PhoneNumber char(11)
as
insert into tblUsers (Name,Email,PhoneNumber)
values (@Name,@Email,@PhoneNumber)
where not exists(select Email from tblUsers where @Email = Email )
Posted

1 solution

You cannot use a WHERE clause with an INSERT operation: INSERT always creates one or more new rows regardless of the rest of the database. (Unless other constraints forbid it in which case teh operation fails.)

Probably, you want:
SQL
IF NOT EXISTS (SELECT Email FROM tblUsers WHERE Email = @Email)
   BEGIN
       INSERT INTO tblUsers ([Name],Email,PhoneNumber)
       VALUES (@Name,@Email,@PhoneNumber)
   END
 
Share this answer
 

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