语句写的不对,最后的查询cname like '@category%',相当于查询@category开头的字符串了。也就是说@category已经不是变量,成了字符串的一部分了。因为单引号的关系。
要改成cname like @category+'%'
存储过程的代码这样改一下就可以了
create procedure b_category;1
(@category varchar(50)=null)
as
begin
EXEC('select * from book where bno in (select bno from book_category where cname like ‘’‘ + @category + ’%‘’')‘)
end
先看是不是LIKE的问题,再看是不是参数的用法。简化一下,看以下SQL能返回结果吗
select bno from book_category where cname =@category
你的用法不对,这么写:
...
select bno from book_category where cname like @category
...
使用时:
execute b_category;1 ‘文学%’
语句有问题
将cname like '@category%' 更改:
cname like ' '''+@category+'"% '
create procedure b_category;1
(@category varchar(50)=null)
as
begin
exec('select * from book where bno in (
select bno from book_category where
cname like '''+@category+'%'')')
end