2011-11-15 60 views
0

我設計了以下視圖並試圖將其用作存儲過程;但是,我的存儲過程不會返回與我的視圖相同的行。我的存儲過程不會返回行,但是我的視圖確實是

我的觀點返回46行:

SELECT 
    NDC_Text, Trade_Name, Vendor_Code, SUM(Quantity_Received) AS QTY, 
    SUM(CASE WHEN Cost_Center = 7070 THEN Cost_Center_Dollars ELSE 0 END) AS [7070], 
    SUM(CASE WHEN Cost_Center = 7071 THEN Cost_Center_Dollars ELSE 0 END) AS [7071], 
    SUM(CASE WHEN Cost_Center = 7700 THEN Cost_Center_Dollars ELSE 0 END) AS [7700], 
    SUM(CASE WHEN Cost_Center = 7701 THEN Cost_Center_Dollars ELSE 0 END) AS [7701], 
    SUM(CASE WHEN Cost_Center = 7702 THEN Cost_Center_Dollars ELSE 0 END) AS [7702], 
    SUM(CASE WHEN Cost_Center = 7703 THEN Cost_Center_Dollars ELSE 0 END) AS [7703], 
    SUM(CASE WHEN Cost_Center = 7703 THEN Cost_Center_Dollars ELSE 0 END) AS [7705], 
    SUM(Cost_Center_Dollars) AS Total, Program_Type 
FROM dbo.VDP_Master_Drug_Receipts_Log 
WHERE Date_Entered BETWEEN '10/1/2011' AND '11/1/2011' 
    AND (Program_Type = 'VDP-BULK') 
GROUP BY NDC_Text, Trade_Name, Vendor_Code, Program_Type 
ORDER BY Trade_Name 

Results

我的存儲過程

GO ALTER PROCEDURE [dbo].[ShowProductByCategory](@StartDate DateTime, @EndDate DateTime, @Type Varchar(15)) 
AS 
Return 
SELECT NDC_Text, Trade_Name, Vendor_Code, SUM(Quantity_Received) AS QTY, 
    SUM(CASE WHEN Cost_Center = 7070 THEN Cost_Center_Dollars ELSE 0 END) AS [7070], 
    SUM(CASE WHEN Cost_Center = 7071 THEN Cost_Center_Dollars ELSE 0 END) AS [7071], 
    SUM(CASE WHEN Cost_Center = 7700 THEN Cost_Center_Dollars ELSE 0 END) AS [7700], 
    SUM(CASE WHEN Cost_Center = 7701 THEN Cost_Center_Dollars ELSE 0 END) AS [7701], 
    SUM(CASE WHEN Cost_Center = 7702 THEN Cost_Center_Dollars ELSE 0 END) AS [7702], 
    SUM(CASE WHEN Cost_Center = 7703 THEN Cost_Center_Dollars ELSE 0 END) AS [7703], 
    SUM(CASE WHEN Cost_Center = 7703 THEN Cost_Center_Dollars ELSE 0 END) AS [7705], 
    SUM(Cost_Center_Dollars) AS Total, Program_Type 
FROM dbo.VDP_Master_Drug_Receipts_Log 
WHERE Date_Entered BETWEEN @StartDate AND @StartDate 
    AND (Program_Type = 'Type') 
GROUP BY NDC_Text, Trade_Name, Vendor_Code, Program_Type 
ORDER BY Trade_Name 

回答

1

你的存儲過程是選擇一個數據集 - 所以你不需要(且不能有)在那裏有一個RETURN條款!

試試這個:

ALTER PROCEDURE [dbo].[ShowProductByCategory] 
    (@StartDate DateTime, @EndDate DateTime, @Type Varchar(15)) 
AS 
    -- Return NO RETURN HERE !! 
    SELECT 
     NDC_Text, Trade_Name, Vendor_Code, SUM(Quantity_Received) AS QTY, 
     SUM(CASE WHEN Cost_Center = 7070 THEN Cost_Center_Dollars ELSE 0 END) AS [7070], 
     SUM(CASE WHEN Cost_Center = 7071 THEN Cost_Center_Dollars ELSE 0 END) AS [7071], 
     SUM(CASE WHEN Cost_Center = 7700 THEN Cost_Center_Dollars ELSE 0 END) AS [7700], 
     SUM(CASE WHEN Cost_Center = 7701 THEN Cost_Center_Dollars ELSE 0 END) AS [7701], 
     SUM(CASE WHEN Cost_Center = 7702 THEN Cost_Center_Dollars ELSE 0 END) AS [7702], 
     SUM(CASE WHEN Cost_Center = 7703 THEN Cost_Center_Dollars ELSE 0 END) AS [7703], 
     SUM(CASE WHEN Cost_Center = 7703 THEN Cost_Center_Dollars ELSE 0 END) AS [7705], 
     SUM(Cost_Center_Dollars) AS Total, Program_Type 
    FROM dbo.VDP_Master_Drug_Receipts_Log 
    WHERE Date_Entered BETWEEN @StartDate AND @EndDate 
    AND (Program_Type = 'Type') 
    GROUP BY 
     NDC_Text, Trade_Name, Vendor_Code, Program_Type 
    ORDER BY 
     Trade_Name 
1

你的存儲過程的WHERE子句

WHERE Date_Entered BETWEEN @StartDate AND @StartDate 

那應該是@EndDate

還有一個不同的Progam_Type,這會導致不同的結果。

0

的問題是在WHERE子句

WHERE Date_Entered BETWEEN @StartDate AND @StartDate 
    AND (Program_Type = 'Type') 

我想那一定是

WHERE Date_Entered BETWEEN @StartDate AND @EndDate 
    AND (Program_Type = @Type) 
+0

感謝您的幫助。我已將存儲過程中的代碼更改爲「WHERE Date_Entered BETWEEN」@StartDate「AND」@EndDate「AND(Program_Type =」@Type「) GROUP BY NDC_Text,Trade_Name,Vendor_Code,Program_Type ORDER BY Trade_Name」仍然返回值爲0作爲SQL SSMS中的INT字段。它可以是商店程序中的「返回」。 –

+0

@JavonFisher這個評論可能是更新你的問題的好東西,所以它會得到更多的曝光(並且查詢更改會更容易閱讀) –

+0

@JavonFisher當然,我沒有注意到RETURN。由於marc_s說你需要刪除它。 –

相關問題