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 in Database by Kithuzzz
• 12,240 points
9 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 in Database by Kithuzzz
• 12,240 points
41 views
0 votes
0 answers

Check if table exists in SQL Server

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

Aug 21 in Database by Kithuzzz
• 12,240 points
33 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 in Database by Kithuzzz
• 12,240 points
28 views
0 votes
1 answer

SQL Server: IF EXISTS ; ELSE

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

answered Sep 19 in Database by narikkadan
• 20,880 points
24 views
0 votes
0 answers

Why is SQL Server Agent not starting?

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

Aug 9 in Database by Kithuzzz
• 12,240 points
20 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 in Database by Kithuzzz
• 12,240 points
66 views
0 votes
0 answers

SQL Server: Invalid Column Name

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

Aug 19 in Database by Kithuzzz
• 12,240 points
117 views
0 votes
1 answer

What is a stored procedure?

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

answered Feb 4 in Database by Neha
• 8,920 points
216 views
0 votes
1 answer

Function vs. Stored Procedure in SQL Server

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

answered Feb 17 in Database by Neha
• 8,920 points

edited Feb 17 by Neha 459 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 in Database by Vaani
• 7,020 points
53 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