2010-09-29 45 views
0

調用UDF像這樣:TVF UDF不返回相同的數據作爲SELECT

SELECT 
     product_name, 
     SUM(quantity) AS SumQty, 
     SUM(face_value) AS SumFaceValue, 
     SUM(net_cost)AS SumNetCost, 
     SUM(face_value - net_cost) AS SumScripRebate, 
     organization_name 
FROM getSalesSummary(@GLSCOrgId, @BeginDate, @EndDate) getSalesSummary 
GROUP BY product_name, organization_name 
ORDER BY product_name 

收率:

"Chili's  1 25.00 22.75 2.25 Sample Organization 1 
    CVS/pharmacy 1 25.00 23.50 1.50 Sample Organization 1 
    Macy's  1 100.00 90.00 10.00 Sample Organization 1" 

使用UDF邏輯和測試結果與SELECT:

SELECT 
     product_name, 
     SUM(quantity) AS SumQty, 
     SUM(face_value) AS SumFaceValue, 
     SUM(net_cost) AS SumNetCost, 
     SUM(face_value - net_cost) AS SumScripRebate, 
     organization_name 
FROM @ReturnTable 
GROUP BY product_name, organization_name 
ORDER BY product_name 

收益率:

"Chili's  4 100.00 91.00 9.00 Sample Organization 1 
    CVS/pharmacy 1 25.00 23.50 1.50 Sample Organization 1 
    Macy's  1 100.00 90.00 10.00 Sample Organization 1" 

@ReturnTable是由UDF返回的表,並創建像這樣:

INSERT INTO @ReturnTable(product_name, 
         unit_price, 
         quantity, 
         face_value, 
         net_cost, 
         organization_name) 
(select * from @TablePartial UNION select * from @TableClosed) 

與SELECT和變量是返回正確的數據,但在調用UDF沒有得到與其他3個池莉測試記錄。我正在使用相同的參數數據。我對UDF很陌生,我不確定它爲什麼會返回與SELECT不同的數據。任何建議和/或答案?

+0

U可以生成表格,數據和功能的腳本和在這裏發佈它可以給更多的想法 – Thakur 2010-09-29 14:24:13

回答

1

你可能需要UNION ALLUNION

望着兩個結果集它彷彿4辣椒的行都是一樣的加起來。

Chili's  1 25.00 22.75 2.25 Sample Organization 1 
Chili's  1 25.00 22.75 2.25 Sample Organization 1 
Chili's  1 25.00 22.75 2.25 Sample Organization 1 
Chili's  1 25.00 22.75 2.25 Sample Organization 1 
------------------------------------------------------------- 
Chili's  4 100.00 91.00 9.00 Sample Organization 1 

使用UNION將刪除重複,留下一行。

+0

不會聯盟所有創建重複?另外,爲什麼這很重要?無論它是如何創建的,選擇表中的所有內容並返回表應該會得到相同的結果? – LobalOrning 2010-09-29 14:23:37

+0

'UNION ALL'不會創建重複項。它只是不會刪除重複。不可能從這裏推測爲什麼你會得到不同的結果。您需要爲您的問題添加足夠的細節,以便我們可以重現問題。 – 2010-09-29 14:25:42

+0

好吧,我會看看還有什麼我可以發佈,並嘗試聯盟所有 – LobalOrning 2010-09-29 14:28:40

0

我能想到的唯一的事情是它的UNION變化UNION ALLUNION將消除的DUP

運行這些查詢看出差別

select 1 as a 
union 
select 1 
union 
select 1 


select 1 as a 
union all 
select 1 
union all 
select 1 
相關問題