[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 231
  • 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

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

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