我在 SQL Server 2008 R2 中创建了一个返回 varchar (4000) 的标量函数。此 varchar (4000) 是连接表的多行和多列的结果。我将在选择报告源中使用此功能。详细信息是具有特定销售数量和价格的产品。
当我使用 select 功能时会出现问题。结果被截断。他要返回 13 行,只返回 5。如果我在 sql management studio 函数中使用 print 命令返回 13 行。
函数如何返回正确的数字行?
print dbo.fnReturnProducts ('{25899A98-5E68-E641-8C9D-7F341F17F2EF}') -- returns 13 lines
select dbo.fnReturnProducts ('{25899A98-5E68-E641-8C9D-7F341F17F2EF}') -- returns 5 lines
alter function [dbo].[fnReturnProducts](@OrderId uniqueidentifier) returns varchar(4000)
begin
declare @Products as varchar(4000)
declare @ProductName as varchar(43)
declare @ProductOrder as varchar(3)
declare @Quantity varchar(30)
declare @UnitPrice varchar(30)
declare @TotalPrice varchar(30)
select @Products = ''
DECLARE aux_cursor CURSOR FOR
select
ProductName as ProductName,
dbo.FormatNumber (ProductOrder, 0, '(', 0) as ProductOrder,
dbo.FormatNumber (Quantity, 2, '', 0) as Quantity,
dbo.FormatNumber (UnitPrice , 2, '$', 0) as UnitPrice,
dbo.FormatNumber (Quantity * UnitPrice, 2, '$', 0) as TotalPrice
from vwOrderDetails
Where OrderId = @OrderId
order BY vwOrderDetails.ProductOrder
OPEN aux_cursor
FETCH NEXT FROM aux_cursor
INTO
@ProductName,
@ProductOrder,
@Quantity,
@UnitPrice,
@TotalPrice
WHILE @@FETCH_STATUS = 0
BEGIN
if len(@Products) > 0
begin
select @Products = @Products + char(10)
end
select @Products = @Products + '(' + @ProductOrder + ') ' + @Quantity + ' - ' + @ProductName + ' a ' + @UnitPrice + ' = ' + @TotalPrice
FETCH NEXT FROM aux_cursor
INTO
@ProductName,
@ProductOrder,
@Quantity,
@UnitPrice,
@TotalPrice
END
CLOSE aux_cursor
DEALLOCATE aux_cursor
RETURN @Products
end
go