ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Check if Email Exists then update else insert record in Database in SQL Server<p>Hi <a class="username" rel="chetan"> chetan</a>,</p> <p>You have to use SQL <span style="text-decoration: underline;">EXISTS</span> function to check whether record exist or not based on Email. Then do your rest insert and update process.</p> <p>Check the below sample query. Now please take its reference and correct your query.</p> <p><strong><span style="text-decoration: underline;">SQL</span></strong></p> <pre class="brush: sql">CREATE PROCEDURE ProcedureName &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; @Email NVARCHAR(50), &nbsp;&nbsp; &nbsp;&nbsp; @ContactPerson NVARCHAR(50) AS BEGIN &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SET NOCOUNT ON; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; IF EXISTS(SELECT ContactPerson FROM cmp WHERE Email = @Email) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; BEGIN &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; --Your Update Query. &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;UPDATE cmp &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;SET ContactPerson = @ContactPerson &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;WHERE Email = @Email &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; END &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ELSE &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; BEGIN &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; --Your Insert Query. &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;INSERT INTO cmp (ContactPerson,Email) VALUES (@ContactPerson,@Email) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; END END</pre> <p>For more details refer below article.</p> <h1 class="header"><a title="Stored Procedure: Return True if record exists and False if record does not exist in SQL Server'a" href="https://www.aspsnippets.com/Articles/Stored-Procedure-Return-True-if-record-exists-and-False-if-record-does-not-exist-in-SQL-Server.aspx">Stored Procedure: Return True if record exists and False if record does not exist in SQL Server</a></h1>https://www.aspforums.net:443/Threads/654277/Check-if-Email-Exists-then-update-else-insert-record-in-Database-in-SQL-Server/https://www.aspforums.net:443/Threads/654277/Check-if-Email-Exists-then-update-else-insert-record-in-Database-in-SQL-Server/Tue, 10 Jul 2018 01:47:47 GMTCheck if Email Exists then update else insert record in Database in SQL Server<p>Hi <a class="username" rel="chetan"> chetan</a>,</p> <p>Please refer below Query and Change your Procedure.</p> <p><span style="text-decoration: underline;"><strong>SQL</strong></span></p> <pre class="brush: sql">DECLARE @comapanyinfo AS TABLE(ID INT IDENTITY, Dataid INT, companyname VARCHAR(20)) INSERT INTO @comapanyinfo VALUES(1,&#39;Test&#39;) INSERT INTO @comapanyinfo VALUES(2,&#39;Test 1&#39;) DECLARE @emailinfo AS TABLE(ID INT IDENTITY, Dataid INT, email VARCHAR(20)) INSERT INTO @emailinfo VALUES(1,&#39;test@test.com&#39;) INSERT INTO @emailinfo VALUES(2,&#39;test1@test.com&#39;) DECLARE @Email VARCHAR(20) DECLARE @CompanyName VARCHAR(50) SET @CompanyName = &#39;test 5&#39; SET @Email = &#39;test1@test.com&#39; DECLARE @Dataid INT SELECT @Dataid = Dataid FROM @emailinfo WHERE Email = @Email IF @Dataid IS NULL BEGIN --Email Not Exist. SELECT @Email Email,@CompanyName Name END ELSE BEGIN --Email Exist. UPDATE @comapanyinfo SET companyname = @CompanyName WHERE Dataid = @Dataid SELECT * FROM @comapanyinfo END</pre> <p><span style="text-decoration: underline;"><strong>Output</strong></span></p> <table border="1"> <tbody> <tr> <td>ID</td> <td>Dataid</td> <td>companyname</td> </tr> <tr> <td>1</td> <td>1</td> <td>Test</td> </tr> <tr> <td>2</td> <td>2</td> <td>test 5</td> </tr> </tbody> </table>https://www.aspforums.net:443/Threads/654277/Check-if-Email-Exists-then-update-else-insert-record-in-Database-in-SQL-Server/https://www.aspforums.net:443/Threads/654277/Check-if-Email-Exists-then-update-else-insert-record-in-Database-in-SQL-Server/Tue, 10 Jul 2018 06:23:19 GMT