我有以下存储过程
ALTER PROC spInParam
(
@partyCode NVARCHAR(50)
)
AS
BEGIN
DECLARE @totalAmount float
DECLARE @setLoop int
DECLARE @setCNT int
print 'Party Code '+@partyCode;
set @totalAmount=(select TotalAmount from BillParticular where partyCode=@partyCode)
set @setLoop=(select count(TotalAmount) from BillParticular where partyCode=@partyCode)
SET @setCNT=0;
WHILE @setCNT<=@setLoop
BEGIN
print 'Total Bill Amt.'+CONVERT(nvarchar(50),@totalAmount);
set @setCNT=@setCNT+1;
END
return CONVERT(NVARCHAR(50),@totalAmount)
END
在这个存储过程中,
select TotalAmount from BillParticular where partyCode=@partyCode
此查询返回多个值。(即特定的有两个 TotalAmounts @partyCode
)我怎样才能把它们放在循环中?
我为此设置了while循环,如代码所示。
这个存储过程编译得很好。但是在执行时,它给了我以下错误:
Party Code 0L036
Msg 512, Level 16, State 1, Procedure spInParam, Line 11
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
请指导我在现有程序中应该做什么。
如果我在任何地方犯错,请告诉我。
请帮我。
样本输出:
Party Code: 0L036
Total Bill Amount:1074432000
Total Bill Amount:2108208000
账单明细表:
编辑:
DECLARE @partyCode nvarchar(50);
WITH records
AS
(
SELECT 'Total Bill Amount : ' + CONVERT(nvarchar(50), TotalAmount) str_output
FROM BillParticular
WHERE PartyCode = @partyCode
)
SELECT 'Party Code ' + @partyCode str_output
UNION ALL
SELECT str_output FROM records
返回NULL