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
Solved

SQL: IF NOT EXISTS

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
RESTORE A BACKUP IN SQL 2012 from SQL 2008 9 64
sql server query 12 24
SQL Quer 4 21
Run Stored Procedure uisng ADO 5 20
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.
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…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

839 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