?
Solved

SQL: IF NOT EXISTS

Posted on 2014-10-15
1
Medium Priority
?
346 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 2000 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

Percona Live Europe 2017 | Sep 25 - 27, 2017

The Percona Live Open Source Database Conference Europe 2017 is the premier event for the diverse and active European open source database community, as well as businesses that develop and use open source database software.

Question has a verified solution.

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

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how the fundamental information of how to create a table.
Suggested Courses

741 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