Solved

SQL: IF NOT EXISTS

Posted on 2014-10-15
1
339 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

Backup Solution for AWS

Read about how CloudBerry Backup fully integrates your backups with Amazon S3 and Amazon Glacier to provide military-grade encryption and dramatically cut storage costs on any platform.

Question has a verified solution.

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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
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

735 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