• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 237
  • Last Modified:

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?
0
MRManthani
Asked:
MRManthani
  • 3
2 Solutions
 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now