1

I have a report which shows purchase indents over a period of time created by indentor. Here, @Name parameter is used to filter indentor. But, the problem is, if @Name parameter is blank then the report is returning null records. I want to show the report with all Indentors if nothing is selected in @Name filter. Below is the Query. I'm new to SQL.

SELECT INH.No_, INH.[Approved Date],  
INH.Indentor, INL.No_ AS ItemCode, 
INL.Description, INL.Description2, 
INL.Req_Quantity, INL.[Unit of Measure], 
PL.[Document No_], PH.[Order Date], PL.Quantity AS OrderedQuantity, PL.[Quantity Received]
FROM [Company$Indent Header] AS INH
INNER JOIN
[Company$Indent Line] AS INL
ON INH.No_ = INL.[Document No_]
INNER JOIN
[Company$Purchase Line] AS PL
ON INL.[Document No_] = PL.[Indent No_] AND INL.[Line No_] = PL.[Indent Line No_]
INNER JOIN
[Company$Purchase Header] AS PH 
ON PL.[Document No_] = PH.No_
WHERE (INH.Indentor = @Name) AND (INL.No_ <> '') AND 
(INH.[Approved Date] BETWEEN @StartDate AND @EndDate)
ORDER BY ItemCode
4

3 回答 3

1

您的查询是如何构建的。通过代码?因为如果@Name 为空,我将简单地(INH.Indentor = @Name)从下一段中省略。WHERE (INH.Indentor = @Name) AND我想SQL中没有条件是否要考虑WHEREclausule中的条件,如果我错了,请纠正我。

于 2013-08-01T05:07:20.467 回答
1

使用这样的东西

WHERE (INH.Indentor = COALESCE(@Name,INH.Indentor)

并确保在参数检查后存储过程的开头

if len(@Name) = 0
set @Name = null

这样如果@Name 为空,它将设置为空,然后 COALESCE(@Name,INH.Indentor) 将检查@Name 是否为空,然后检查现有值

于 2013-08-01T05:25:16.977 回答
0

试试这个——

SELECT
      INH.No_
    , INH.[Approved Date]
    , INH.Indentor
    , ItemCode = INL.No_ 
    , INL.[description]
    , INL.Description2
    , INL.Req_Quantity
    , INL.[Unit of Measure]
    , PL.[Document No_]
    , PH.[Order Date]
    , OrderedQuantity = PL.Quantity 
    , PL.[Quantity Received]
FROM dbo.[Company$Indent Header] INH
JOIN dbo.[Company$Indent Line] INL ON INH.No_ = INL.[Document No_]
JOIN dbo.[Company$Purchase Line] PL ON INL.[Document No_] = PL.[Indent No_] AND INL.[Line No_] = PL.[Indent Line No_]
JOIN dbo.[Company$Purchase Header] PH ON PL.[Document No_] = PH.No_
WHERE INH.Indentor = ISNULL(@Name, INH.Indentor)
    AND INL.No_ != ''
    AND INH.[Approved Date] BETWEEN @StartDate AND @EndDate
ORDER BY ItemCode
于 2013-08-01T05:36:31.623 回答