我在存储过程中有一个Where子句,它只返回where子句
中2个可能的返回值之一GO
/****** Object: StoredProcedure [dbo].[RPT_HC_ShiftRates] Script Date: 01/23/2018 10:51:55 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER Procedure [dbo].[RPT_HC_ShiftRates]
AS --variables
BEGIN
select c.Company
, c.OurReference
, Shifts1 as [Split shifts]
, Shifts2 as [Invoiced at one rate]
, s.PortalApproval
, s.Rates
, s.Breaks
, c.ClientID
from ClientSectorDefinedColumns s
join clients c on c.ClientID = s.ClientID
left OUTER JOIN ClientBranches AS CB ON C.ClientID = CB.BranchClientId
where (Shifts1 = 'y' or Shifts2 = 'y' or s.Rates is not null or s.Breaks is not null)
and (c.OurReference like 'P00%' or c.OurReference = 'HEA%')
and C.ClientID in (select objectid from SectorObjects where SectorId in (58, 59, 60, 61, 62, 63, 64, 65, 66, 47 ))
END
我怀疑引起问题的一行是:
and (c.OurReference like 'P00%' or c.OurReference = 'HEA%')
这只是返回LIKE' P00%'而它应该返回一个或另一个。
我是SQL的新手,只完成了20761B,但在我的培训材料中找不到答案。
感谢您的帮助!
答案 0 :(得分:1)
显然你打算进行模式匹配。 在条件的第一部分,你以正确的方式做到了:
c.OurReference like 'P00%'
但在第二部分中,您意外使用了=
而不是LIKE
:
c.OurReference = 'HEA%'
这样的错误很容易被忽视。
条件的右侧确实包含百分号(%
),
但是在没有LIKE
的情况下,SQL Server不会将其解释为通配符。
相反,角色按原样匹配'。
如果列OurReference
字面上 HEA%
,则该条件才会成功。
在您的数据库中,情况永远不会如此;没有记录会与条件的那部分相匹配。