日期:2014-05-17  浏览次数:20411 次

本人初学不太懂求解
查询并返回product产品表中产品名称以C开头的产品的条数
要求:产品名称以C开头的条件用变量来表示,查询出来的产品条数也用变量表示,并最终需要返回'The sum of the product is' +查询出来的产品条数。
sql

------解决方案--------------------

--个人觉得你的这句回 “The sum of the product is  ” 多此一举
--当然,你非要这句,那就这么输出了
declare @num int
declare @result varchar(100)
select @num=count(1) from product where 产品名称 like 'C%'
set @result='The sum of the product is '+cast(@num as varchar(100))
select @result as N'结果'

------解决方案--------------------
是这样吗:



declare @prod_name varchar(20);
declare @result varchar(100);

set @prod_name = 'c'
set @result = 'The sum of the product is '

select @result = @result + CAST(COUNT(*) as varchar)
from product
where prod_name like @prod_name +'%'

------解决方案--------------------
declare @searchPatter nvarchar(10) = 'c%'
declare @count int

select @count = COUNT(*) from product
where 产品名称 like @searchPatter

select @count
------解决方案--------------------
引用:

--个人觉得你的这句回 “The sum of the product is  ” 多此一举
--当然,你非要这句,那就这么输出了
declare @num int
declare @result varchar(100)
select @num=count(1) from product where 产品名称 like 'C%'
set @result='The sum of the product is '+cast(@num as varchar(100))
select @result as N'结果'
+1