Regarding query in sql server

I  have written a script which is like

declare   @type int

set @type=(select type from person where personid=12 and departmentid=5)

if(@type!=1)

begin
do something
end

If the query does not return anything it is not entering into the if loop.

What do I need to write in the if condition for it to enter even if the query does not return anything?
MRManthaniAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

ErnariashCommented:
You have @type as null:
if(@type!=1 or @type is null)
Or
if(IsNull(@type, 0)!=1)  
 
0
MRManthaniAuthor Commented:
I have tried it. It is not working

set @type=(select type from person where personid=12 and departmentid=5)
if(@type!=1)
The query does not return any value,does not return any rows.
 What do I need to keep in 'if condition' if i want it to enter into dosomething if the value of @type is not 1?

0
ErnariashCommented:
Please try this:
declare   @type int
select @type=type from person where personid=12 and departmentid=5
if(IsNull(@type, 0) <> 1)  
begin
--do something
select 1
end

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
ErnariashCommented:
Or simple using the if

if NOT EXISTS (select 1 from Person where Employeeid=12 and departmentid=5 and Type=1)  
begin
--do something
print 1
end

Open in new window

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
Microsoft SQL Server 2005

From novice to tech pro — start learning today.