我有一个文章表,如下图所示:特殊字符不工作
create table article(
artID int,
arttitle varchar(50)
)
我在下面插入4个记录:
insert into article values (1,'abcd');
insert into article values (2,'asfsdf asdf sdf ');
insert into article values (3,'asdfasdfa sd ');
insert into article values (4,'abcasdfsdd [Little]');
创建一个测试存储过程:
create procedure test
@aID int = null,
@atit varchar(50) = null
as
select * from article where artID = COALESCE(@aID,artID) and
arttitle like '%'+COALESCE(@atit,arttitle)+'%';
这里的问题是: 当我执行此sp
与aid = 1
它与该记录的结果和类似的aid
2和3. 但是,当我与aid = 4
执行时没有结果是由于那些square brackets [Little]
。
这里是存储过程的执行脚本:
exec test @aID = 4;
请帮我实现这一目标。谢谢! 这里是sqlfiddle link
这对我很有用,因此将其标记为答案。但我想知道为什么特殊字符没有被COALESCE处理。 –
它不是'coalesce',它是''''操作符。 –
@Evaldas:是的,如果我们想在类似的查询中使用'COALESCE',我们需要使用'ESCAPE'\''。 但是,至于问题,我认为只是为了检查'是否为空'就足以解决错误。 –