sql server 多条件查询语句 出错
@QuestionParentDirection nvarchar(50)=null,
@QuestionChildDirection nvarchar(50)=null,
@QuestionStyle nvarchar(20)=null,
@SourceCompany nvarchar(50)=null,
@QuestionDateA nvarchar(10)=null,
@QuestionDateB nvarchar(10)=null,
@KeyWord nvarchar(50)=null
AS
SELECT QuestionParentDirection,QuestionChildDirection, QuestionStyle, QuestionTitle, QuestionAnswer, SourceCompany, QuestionDate
FROM Question
WHERE(Visibility = 1)
AND (@QuestionParentDirection is null or RTRIM(QuestionParentDirection) = @QuestionParentDirection)
AND (@QuestionChildDirection is null or RTRIM(QuestionChildDirection)=@QuestionChildDirection)
AND (@QuestionStyle is null or RTRIM(QuestionStyle) = @QuestionStyle)
AND (@KeyWord is null or (RTRIM(QuestionAnswer) like '%'+@KeyWord+'%' or RTRIM(QuestionTitle) like '%'+@KeyWord+'%'))
AND (@SourceCompany is null or RTRIM(SourceCompany) = @SourceCompany)
AND (QuestionDate BETWEEN @QuestionDateA AND @QuestionDateB)
为什么有的语句执行不了呢,没有报错,但是查询不到结果,请大神看看哪不对
[解决办法]
晕,你就给堆代码要人看,业务也不知道,怎么搞啊,你把where中的每个and条件,逐个注销,看看是哪个导致没数据的,语法上没错误。应该就是你的条件没选对或者数据有问题而已
SELECT QuestionParentDirection , QuestionChildDirection , QuestionStyle , QuestionTitle , QuestionAnswer , SourceCompany , QuestionDate FROM Question WHERE ( Visibility = 1 ) AND ( @QuestionParentDirection IS NULL OR RTRIM(QuestionParentDirection) = @QuestionParentDirection ) AND ( @QuestionChildDirection IS NULL OR RTRIM(QuestionChildDirection) = @QuestionChildDirection ) AND ( @QuestionStyle IS NULL OR RTRIM(QuestionStyle) = @QuestionStyle ) AND ( @KeyWord IS NULL OR ( RTRIM(QuestionAnswer) LIKE '%' + @KeyWord + '%' OR RTRIM(QuestionTitle) LIKE '%' + @KeyWord + '%' ) ) AND ( @SourceCompany IS NULL OR RTRIM(SourceCompany) = @SourceCompany ) AND ( QuestionDate BETWEEN @QuestionDateA AND @QuestionDateB )
[解决办法]
从最内部的联接查询,或者说从最初的联接查询开始,一步一步看是否有数据。