当我向@keyword变量提供空值时出现此错误。我找到了一个解决方案,通过将@keyword值设置为“”“来解决。问题是当值设置为“”“时,当您运行查询时没有显示结果时,它必须显示所有记录。我怎么能这样做?
declare @keyword nvarchar(50)
set @keyword='""'
SELECT u.Id as AId FROM Users u WHERE FREETEXT((FirstName,Lastname,MiddleName),@keyword)
UNION
SELECT c.AId FROM Certification c WHERE FREETEXT((Certification,School),@keyword)
UNION
SELECT ad.AId FROM ApplicantDetails ad WHERE FREETEXT((City,Province,StateorRegion),@keyword)
UNION
SELECT eb.AId FROM EducationalBackground eb WHERE FREETEXT((School,fieldofStudy),@keyword)
UNION
SELECT ed.AId FROM EmploymentDetails ed WHERE FREETEXT((Position,DescriptionofDuties,CompanyName,City,Province,StateorRegion),@keyword)
UNION
SELECT e.AId FROM Expertise e WHERE FREETEXT((Expertise),@keyword)
UNION
SELECT ge.AId FROM GeographicalExperience ge WHERE FREETEXT(([Description]),@keyword)
UNION
SELECT t.AId FROM Training t WHERE FREETEXT((Training,School),@keyword)
答案 0 :(得分:5)
我明白了......
IF ISNULL(@keyword,'') = '' SET @keyword = '""' ;
SELECT u.Id as AId FROM Users u WHERE @keyword = '""'
OR FREETEXT((FirstName,Lastname,MiddleName),@keyword)
UNION
SELECT c.AId FROM Certification c WHERE @keyword = '""'
OR FREETEXT((Certification,School),@keyword)
UNION
SELECT ad.AId FROM ApplicantDetails ad WHERE @keyword = '""'
OR FREETEXT((City,Province,StateorRegion),@keyword)
UNION
SELECT eb.AId FROM EducationalBackground eb WHERE @keyword = '""'
OR FREETEXT((School,fieldofStudy),@keyword)
UNION
SELECT ed.AId FROM EmploymentDetails ed WHERE @keyword = '""'
OR FREETEXT (Position,DescriptionofDuties,CompanyName,City,Province,StateorRegion),@keyword)
UNION
SELECT e.AId FROM Expertise e WHERE @keyword = '""'
OR FREETEXT((Expertise),@keyword)
UNION
SELECT ge.AId FROM GeographicalExperience ge WHERE @keyword = '""'
OR FREETEXT(([Description]),@keyword)
UNION
SELECT t.AId FROM Training t WHERE @keyword = '""'
OR FREETEXT((Training,School),@keyword)
答案 1 :(得分:0)
在我的情况下,我有一个带有回车换行符的空字段,即CRLF(回车)。所以Null和修剪长度为0的测试是不够的。我添加了一个带空格的替换CRLF,因此修剪后的长度不会将其捕获为0.
SELECT @searchString = REPLACE(@searchString, CHAR(13) + CHAR(10), ' ')
IF(@searchString IS NOT NULL AND LEN(LTRIM(RTRIM(@searchString))) > 0)
BEGIN
-- do the search
END