循环查询出的记录可以一次输出吗?
像这样的一个存储过程
Create Proc [dbo].ShowCateName
(
@CateCode varchar(250),--长度是四的倍数
@CateName varchar(100) output
)
as
DECLARE @i int
Declare @Lstr int
Set @Lstr = Len(@CateCode)/4
Set @i=0
WHILE @i < @Lstr
BEGIN
Set @i =@i +1
Select @CateName=A.CateName From Category A Where A.CateCode=Substring(@CateCode,1,@i*4)
IF @i <@Lstr
Print @CateName+ '--> '
Else
Print @CateName
END
现在是相当于查询Len(@CateCode)/4次,各查询结果不能一次输出,Select几次就输出几个结果,有办法一次输出吗?
------解决方案--------------------先进行字符串连接,再输出
------解决方案--------------------ALTER Proc [dbo].ShowCateName @CateCode varchar(250),@CateName varchar(100) output as
DECLARE @i int,@Lstr int
Set @Lstr = Len(@CateCode)/4
Set @i=0
SET @CateName= ' '
WHILE @i < @Lstr
BEGIN
Set @i =@i +1
Select @CateName=@CateName+A.CateName From Category A Where A.CateCode=Substring(@CateCode,(@i-1)*4+1,4)
IF @i <@Lstr
Print @CateName+ '--> '+Substring(@CateCode,(@i-1)*4+1,@i*4)
Else
Print @CateName
END