Solved

SQL: IF NOT EXISTS

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

707 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now