How to use two seperate IF NOT EXISTS clauses before an insert statement

I am performing an insert using an IF NOT EXISTS CLAUSE. All is great, however what I would really like to do, is to use two IF NOT EXISTS CLAUSES.
However. I can't get it to work. Does anyone know of the syntax that would allow me to use two IF NOT EXISTS CLAUSES before an insert is performed on
a table.

Before:


IF NOT EXISTS (SELECT * FROM EmailsRecebidos
                   WHERE De = @_DE
                   AND Assunto = @_ASSUNTO
                   AND Data = @_DATA)
   BEGIN
       INSERT INTO EmailsRecebidos (De, Assunto, Data)
       VALUES (@_DE, @_ASSUNTO, @_DATA)
   END

-- After (Help, because This does not work)

   IF NOT EXISTS (SELECT * FROM EmailsRecebidos
                   WHERE De = @_DE
                   AND Assunto = @_ASSUNTO
                   AND Data = @_DATA)

   AND IF NOT EXISTS (SELECT * FROM EmailsRecebidos
                   WHERE pg = @p
                   AND Assunto = @_ASSUNTO
                   AND Monost = @m)
   BEGIN
       INSERT INTO EmailsRecebidos (De, Assunto, Data)
       VALUES (@_DE, @_ASSUNTO, @_DATA)
   END
LVL 2
brgdotnetcontractorAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ryan ChongCommented:
quick try:

 IF NOT EXISTS (SELECT * FROM EmailsRecebidos 
                   WHERE De = @_DE
                   AND Assunto = @_ASSUNTO
                   AND Data = @_DATA)

    IF NOT EXISTS (SELECT * FROM EmailsRecebidos 
                   WHERE pg = @p
                   AND Assunto = @_ASSUNTO
                   AND Monost = @m)
   BEGIN
       INSERT INTO EmailsRecebidos (De, Assunto, Data)
       VALUES (@_DE, @_ASSUNTO, @_DATA)
   END

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
chaauCommented:
... Or drop the second if:

   IF NOT EXISTS (SELECT * FROM EmailsRecebidos
                   WHERE De = @_DE
                   AND Assunto = @_ASSUNTO
                   AND Data = @_DATA)

   AND NOT EXISTS (SELECT * FROM EmailsRecebidos
                   WHERE pg = @p
                   AND Assunto = @_ASSUNTO
                   AND Monost = @m)
   BEGIN
       INSERT INTO EmailsRecebidos (De, Assunto, Data)
       VALUES (@_DE, @_ASSUNTO, @_DATA)
   END 

Open in new window

0
Vikas GargBusiness Intelligence DeveloperCommented:
Hello ,

You can try this

IF NOT EXISTS (SELECT * FROM EmailsRecebidos 
                   WHERE De = @_DE
                   AND  pg = @p
                   AND Assunto = @_ASSUNTO
                   AND Data = @_DATA
                   AND Monost = @m)

   BEGIN
       INSERT INTO EmailsRecebidos (De, Assunto, Data)
       VALUES (@_DE, @_ASSUNTO, @_DATA)
   END

Open in new window

0
Vitor MontalvãoMSSQL Senior EngineerCommented:
You don't want two IF's. You want two NOT EXISTS clauses but only one IF.
Chaau's solution should work perfectly.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.