如何在存储过程中将变量的值设置为IS NOT NULL?

时间:2016-12-09 09:33:03

标签: sql-server tsql stored-procedures

我有一个名为@businessUnit的变量,它是存储过程的参数。我在具有多个条件和一些内连接的select语句中使用它。

我想要的是@businessUnit = 0的参数值然后我想从表中获取所有记录

例如,

SELECT DISTINCT
[Resource ID] = r.Resource_ID,
[First Name]  = r.Resource_First_Name,
[Last Name]= r.Resource_Last_Name,
[Country] = c.Country_Name,
[Location] = l.Location_Short_Name,
[Function] = t.Trade_Name,
[Business Unit] = bu.BusinessUnit_Name,
[Product] = p.Product_Name,
[Assignment Start Date] = ra.Resource_Allocation_From_Date,
[Assignment End Date] = ra.Resource_Until_Date,
[% Training] = ra.Resource_Training,
[% Admin] = ra.Resource_Admin,
[% Roadmap] = ra.Resource_Roadmap,
[% Defect] = ra.Resource_Defect,
[% Commission] = ra.Resource_Commission

INTO #ResourceAllocated

FROM mResource r
INNER JOIN mResource ON r.IsDeleted = 0 
INNER JOIN mResourceAllocation ra ON  ra.BusinessUnit_ID = @businessUnit and ra.IsDeleted = 0 and r.Resource_ID = ra.Resource_ID and @Date >= ra.Resource_Allocation_From_Date and @Date <= ra.Resource_Until_Date
INNER JOIN mBusinessUnit bu ON bu.IsDeleted = 0 and bu.BusinessUnit_ID = @businessUnit
INNER JOIN mCountry c ON r.Resource_Country_ID = c.Country_ID 
INNER JOIN mLocation l ON r.Resource_Location_ID = l.Location_ID 
INNER JOIN mTrade t ON  r.Resource_Trade_ID = t.Trade_ID
INNER JOIN mProduct p ON ra.Product_ID = p.Product_ID   and p.IsDeleted = 0


GROUP BY bu.BusinessUnit_Name,
r.Resource_ID,
r.Resource_First_Name,
r.Resource_Last_Name,
c.Country_Name,
l.Location_Short_Name,
t.Trade_Name,
p.Product_Name,
ra.Resource_Allocation_From_Date,
ra.Resource_Until_Date,
ra.Resource_Training,
ra.Resource_Admin,
ra.Resource_Roadmap,
ra.Resource_Defect,
ra.Resource_Commission

上面的查询会给我一个指定@businessUnit

的记录

当用户传递@businessUnit = 0时,我想要它的所有记录 感谢

2 个答案:

答案 0 :(得分:0)

一个简单的OR条件就足够了:

SELECT * FROM mytable
  WHERE (@Id = 0 AND Id IS NOT NULL) 
     OR (@Id > 0 AND Id = @Id)

因此,当@Id = 0仅记录将返回Id IS NOT NULL的位置时。当@Id > 0时,将仅返回具有相应Id值的记录。

你提到Id是一个IDENTITY列,因此它不会有任何NULL值。如果您只想在@Id = 0时返回所有记录,请使用:

 SELECT * FROM mytable
  WHERE (@Id = 0) 
     OR (@Id > 0 AND Id = @Id)

根据您修改过的问题,我建议将mBusinessUnit上的JOIN更改为:

INNER JOIN mResourceAllocation ra ON ((@businessUnit = 0 AND ra.BusinessUnit_ID IS NOT NULL) OR (@businessUnit > 0 AND ra.BusinessUnit_ID = @businessUnit)) 
and ra.IsDeleted = 0
and r.Resource_ID = ra.Resource_ID
and @Date >= ra.Resource_Allocation_From_Date
and @Date <= ra.Resource_Until_Date

根据评论,您可以在WHERE子句中指定连接条件,如下所示:

FROM mResource r
INNER JOIN mResource ON r.IsDeleted = 0 
INNER JOIN mResourceAllocation ra ON ra.IsDeleted = 0 and r.Resource_ID =   ra.Resource_ID and @Date >= ra.Resource_Allocation_From_Date and @Date <= ra.Resource_Until_Date
INNER JOIN mBusinessUnit bu ON bu.IsDeleted = 0 and bu.BusinessUnit_ID = @businessUnit
INNER JOIN mCountry c ON r.Resource_Country_ID = c.Country_ID 
INNER JOIN mLocation l ON r.Resource_Location_ID = l.Location_ID 
INNER JOIN mTrade t ON  r.Resource_Trade_ID = t.Trade_ID
INNER JOIN mProduct p ON ra.Product_ID = p.Product_ID   and p.IsDeleted = 0
WHERE (@businessUnit = 0 AND ra.BusinessUnit_ID IS NOT NULL) OR (@businessUnit > 0 AND ra.BusinessUnit_ID = @businessUnit)

答案 1 :(得分:0)

试试这个

SELECT * FROM mytable 
WHERE 
(Id = @id and @id<>0) 
   or  
(Id is not null and @id=0)