Solved

SQL: IF NOT EXISTS

Posted on 2014-10-15
1
343 Views
Last Modified: 2014-10-17
I need to create a one time script for a client to insert data into a column. However, before I do that, I need to check for a condition. My current script is below. How do I modify this script to ADD another condition to also check:

IF NOT EXISTS(SELECT NULL FROM CustomFieldDefinitions cfdef WHERE cfdef.FieldId = 279)

IF NOT EXISTS(SELECT NULL FROM customfielddata cfd WHERE cfd.FieldId = 279)
BEGIN
	INSERT INTO ABC (FieldId, Data, PrimaryId, SecondaryId)
	SELECT 279, 555, E.PartyId, NULL
	FROM Entity E
	WHERE E.EntityTypeId IN (2,3)
END
GO

Open in new window

0
Comment
Question by:pzozulka
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 500 total points
ID: 40382456
IF NOT EXISTS(SELECT NULL FROM customfielddata cfd WHERE cfd.FieldId = 279)
 AND NOT EXISTS(SELECT NULL FROM CustomFieldDefinitions cfdef WHERE cfdef.FieldId = 279)
BEGIN
      INSERT INTO ABC (FieldId, Data, PrimaryId, SecondaryId)
      SELECT 279, 555, E.PartyId, NULL
      FROM Entity E
      WHERE E.EntityTypeId IN (2,3)
END
GO
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question