2013-03-21 77 views
0

我在SQL Server 2008 R2中創建了一個返回varchar(4000)的標量函數。 此varchar(4000)是連接表的多行和多列的結果。 我將在報表的選擇源中使用此功能。 細節是特定銷售數量和價格的產品。SQL Server標量函數返回VARCHAR截斷

當我使用select函數時,會發生問題。 結果被截斷。 他當時要返回13行並且只返回5. 如果我在sql管理工作室函數內使用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 
+0

從SSMS外部調用時它被截斷了嗎?好奇地檢查它是否僅僅是截斷它的結果窗口... – 2013-03-21 21:58:55

+0

print dbo.fnReturnProducts('{25899A98-5E68-E641-8C9D-7F341F17F2EF}') - 返回13行 – user2197049 2013-03-22 12:00:46

+0

號SSMS內部和應用程序。當我使用選擇結果返回截斷。 – user2197049 2013-03-22 12:04:44

回答

0

試試這個:

select dbo.fnReturnProducts ('{25899A98-5E68-E641-8C9D-7F341F17F2EF}') into #tablename 
select * from #tablename 
+0

您的解決方案沒有奏效。 – user2197049 2013-03-22 13:21:49

0

這不是一個答案,但是從SSMS(和應用程序)的結果。 查看區別:僅打印和選擇命令。 在我使用select(我的問題)的應用程序內。

print dbo.fnReturnProducts ('{25899A98-5E68-E641-8C9D-7F341F17F2EF}') -- returns 13 lines 

(1) 1.00 - MAC INST PARATI GAL/CAIPIRA 85GR   a $.75 = $.75 
(2) 1.00 - MAC INST PARATI CAR/LEG.85GR    a $.75 = $.75 
(3) 1.00 - MAC INST PARATI GAL/CAIPIRA 85GR   a $.75 = $.75 
(4) 1.00 - TRINK TANGERINA 35GR      a $.69 = $.69 
(5) 1.00 - TRINK TANGERINA 35GR      a $.69 = $.69 
(6) 1.00 - MAC ORSI BAM.OVOS PARAFUSO 500GR   a $1.56 = $1.56 
(7) 1.00 - MAC INST PARATI CAR/LEG.85GR    a $.75 = $.75 
(8) 1.00 - ORSI ACHOC PO 400GR       a $2.16 = $2.16 
(9) 1.00 - ORSI ACHOC PO 400GR       a $2.16 = $2.16 
(10) 1.00 - BISC.PANCO ROSQ NATA 500G     a $3.53 = $3.53 
(11) 1.00 - ST PROTEX ERVA DOCE 90GR     a $1.76 = $1.76 
(12) 1.00 - OLEO SOJA CORCOVADO 900ML    UN a $3.59 = $3.59 
(13) 1.00 - OLEO SOJA CORCOVADO 900ML    UN a $3.59 = $3.59 

select dbo.fnReturnProducts ('{25899A98-5E68-E641-8C9D-7F341F17F2EF}') -- returns 5 lines 

(1) 1.00 - MAC INST PARATI GAL/CAIPIRA 85GR   a $.75 = $.75 
(2) 1.00 - MAC INST PARATI CAR/LEG.85GR    a $.75 = $.75 
(3) 1.00 - MAC INST PARATI GAL/CAIPIRA 85GR   a $.75 = $.75 
(4) 1.00 - TRINK TANGERINA 35GR