IF NOT EXISTS not working

Why does this return 'do nothing' when I have set the variable @CompName to NULL?  I have a table Company that does not have a NULL Compname or a empty Compname in it (I've triple verified).

                                    DECLARE @CompName varchar(70)=NULL
                                    IF EXISTS (SELECT Company FROM Company WHERE Compname=coalesce(@CompName,''))
                                      Select @Compname
                                    else
                                    print 'Do Nothing'

I've also tried IF EXISTS (SELECT * FROM Company WHERE @CompName is not null and Compname=@CompName)
and it still returns 'do nothing'

I ultimately am doing an insert into the Company table but do not want to do an insert if the CompName already exists or if CompName is null
nolasaintsgalAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Nakul VachhrajaniTechnical Architect, Capgemini IndiaCommented:
The way IF EXISTS works is that if the condition being evaluated is FALSE, then the "ELSE" portion is executed.

As you have already confirmed, the Company table does not have a Company Name that's NULL or empty (i.e. ''). Hence, no records are returned when the condition (SELECT * FROM Company WHERE Compname=coalesce(@CompName,'')) is evaluated.

No records mean that the IF EXISTS evaluates to FALSE and hence the "ELSE" portion is executed and returns you the "do nothing".

What you would want to do is change the condition to "IF NOT EXISTS" and that should work fine.
0
nolasaintsgalAuthor Commented:
It still isn't doing what I want to achieve.  If @CompName is NULL  OR if @Compnamee equals a value in the Company table then do nothing, otherwise insert the new @Compname value.  Do I need to break it up like this?

If (@Compname is not null)
begin
If not exists (Select compname from company where compname=@Company)
Insert into company
else
do nothing
0
Mark WillsTopic AdvisorCommented:
If wanting to insert if not already there, that *should* work....

try doing :  select compname from company where len(isnull(compname,'') < 2)

And to test your logic....
create table #company (id int identity, compname varchar(30))

insert #company values ('Company 1')


declare @Company varchar(30) = NULL

IF EXISTS (select NULL from #company where @Company is not null and compname = @Company)
   select 'FOUND'
else 
   select 'NOT FOUND'
-- shows NOT FOUND

Open in new window

Noe to test a known quantity
 
declare @Company varchar(30) = 'Company 1'

IF EXISTS (select NULL from #company where @Company is not null and compname = @Company)
   select 'FOUND'
else 
   select 'NOT FOUND'

-- shows FOUND

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
nolasaintsgalAuthor Commented:
Thank you both for your input!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.