SQL Server Insert if not exists

0 votes

I want to add data to my table, but I only want to add data that is new to my database.

My code:

ALTER PROCEDURE [dbo].[EmailsRecebidosInsert]
  (@_DE nvarchar(50),
   @_ASSUNTO nvarchar(50),
   @_DATA nvarchar(30) )
AS
BEGIN
   INSERT INTO EmailsRecebidos (De, Assunto, Data)
   VALUES (@_DE, @_ASSUNTO, @_DATA)
   WHERE NOT EXISTS ( SELECT * FROM EmailsRecebidos 
                   WHERE De = @_DE
                   AND Assunto = @_ASSUNTO
                   AND Data = @_DATA);
END

The error is:

Msg 156, Level 15, State 1, Procedure EmailsRecebidosInsert, Line 11
Incorrect syntax near the keyword 'WHERE'.

Please can someone help me solve this? 

Aug 25, 2022 in Database by Kithuzzz
• 38,010 points
1,766 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In Database

0 votes
0 answers

SQL Server 2008 - IF NOT EXISTS INSERT ELSE UPDATE

I'm trying to create a time clock ...READ MORE

Aug 28, 2022 in Database by Kithuzzz
• 38,010 points
3,631 views
0 votes
0 answers

Check if table exists in SQL Server

I want this to be the last ...READ MORE

Aug 21, 2022 in Database by Kithuzzz
• 38,010 points
32,861 views
0 votes
0 answers

Check if table exists and if it doesn't exist, create it in SQL Server 2008

I'm using SQL Server 2008 to create ...READ MORE

Sep 2, 2022 in Database by Kithuzzz
• 38,010 points
612 views
0 votes
1 answer

SQL Server: IF EXISTS ; ELSE

Try this: update b set code = ...READ MORE

answered Sep 19, 2022 in Database by narikkadan
• 63,420 points
3,005 views
0 votes
0 answers

Why is SQL Server Agent not starting?

On top of Windows Server 2012, I ...READ MORE

Aug 9, 2022 in Database by Kithuzzz
• 38,010 points
462 views
0 votes
0 answers

SQL Server database restore error: specified cast is not valid. (SqlManagerUI)

For my production website, SQL Server 2008 ...READ MORE

Aug 20, 2022 in Database by Kithuzzz
• 38,010 points
1,095 views
0 votes
0 answers

SQL Server: Invalid Column Name

I am in the process of changing ...READ MORE

Aug 19, 2022 in Database by Kithuzzz
• 38,010 points
1,102 views
0 votes
1 answer

What is a stored procedure?

A stored procedure is a set of ...READ MORE

answered Feb 4, 2022 in Database by Neha
• 9,060 points
788 views
0 votes
1 answer

Function vs. Stored Procedure in SQL Server

Functions are calculated values that cannot make ...READ MORE

answered Feb 17, 2022 in Database by Neha
• 9,060 points

edited Feb 17, 2022 by Neha 18,231 views
0 votes
1 answer

Rename column SQL Server 2008

Use sp_rename EXEC sp_RENAME 'TableName.PreviousColumnName' , 'NewColumnName', 'COLUMN' See: SQL SERVER ...READ MORE

answered Feb 23, 2022 in Database by Vaani
• 7,020 points
401 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP