2014-03-28 71 views
0

我有以下存儲過程,需要爲年度報表返回多種類型的數據。出於某種原因,我只得到結果中的第一列。如果我註釋掉NumOfBonds列,則TotalAmount列是唯一返回的列。我需要它返回所有這些行,但由於某些原因,我只得到一個當我執行的過程:SQL Server 2008存儲過程僅返回預期結果中的第一列

ALTER PROCEDURE dbo.AnnualReport 
    @Agency varchar(50), 
    @Subagency varchar(50), 
    @StartDate datetime, 
    @EndDate datetime 
AS 
    SELECT COUNT(*) AS NumOfBonds, SUM(ISNULL(Powers.BondAmount, 0)) + SUM(ISNULL(Charges.BondAmount, 0)) AS TotalAmount, 
    SUM(ISNULL(Powers.BondPremium, 0)) + SUM(ISNULL(Charges.BondPremium, 0)) AS TotalPremium, 
    SUM(ISNULL(Powers.BondPremium, 0)) + SUM(ISNULL(Charges.BondPremium, 0)) + SUM(ISNULL(Fees.Amount, 0)) 
    + SUM(ISNULL(ForfeitureExpense.Amount, 0)) - SUM(ISNULL(Payment.Amount, 0)) AS TotalBalance 
    FROM Bond 
    LEFT OUTER JOIN 
    (
     SELECT Powers.Bond, SUM(Charge.BondAmount) AS BondAmount, SUM(Charge.BondPremium) AS BondPremium 
     FROM Powers INNER JOIN Charge ON Powers.Surety = Charge.PowerSurety 
     AND Powers.PowerPrefix = Charge.PowerPrefix AND Powers.PowerNumber = Charge.PowerNumber 
     GROUP BY Powers.Bond 
    ) AS Powers ON Bond.ID = Powers.Bond 
    LEFT OUTER JOIN 
    (
     SELECT BondID, SUM(BondAmount) AS BondAmount, SUM(BondPremium) AS BondPremium 
     FROM ChargeWithoutPower 
     GROUP BY BondID 
    ) AS Charges ON Bond.ID = Charges.BondID 
    LEFT OUTER JOIN 
    (
     SELECT Bond, SUM(Amount) AS Amount 
     FROM BondFee 
     WHERE Date >= @StartDate AND Date <= @EndDate 
     GROUP BY Bond 
    ) AS Fees ON Bond.ID = Fees.Bond 
    LEFT OUTER JOIN 
    (
     SELECT Bond, SUM(Amount) AS Amount 
     FROM ForfeitureExpense 
     WHERE ExpenseDate >= @StartDate AND ExpenseDate <= @EndDate 
     GROUP BY Bond 
    ) AS ForfeitureExpense ON Bond.ID = ForfeitureExpense.Bond 
    LEFT OUTER JOIN 
    (
     SELECT Bond, SUM(Amount) AS Amount 
     FROM Payment 
     WHERE Date >= @StartDate AND Date <= @EndDate 
     GROUP BY Bond 
    ) AS Payment ON Bond.ID = Payment.Bond 
    WHERE Bond.ReleaseDate >= @StartDate AND Bond.ReleaseDate <= @EndDate 
    AND Bond.Agency = @Agency AND Bond.Subagency = @Subagency 
    RETURN 

的基本目的是給我的寫的年期國債的數量,總金額的債券,這些債券的總保費,以及這些債券在年底的餘額。我不知道爲什麼它只看到第一列,而不看其他部分。

+1

如果您嘗試自行運行SQL,而不是通過存儲過程,是否會獲得所有列? –

+0

不,當我輸入具有明確日期,代理和子代理的查詢時,我得到全部4列 – Grungondola

+0

很難看到明顯的問題但是,我想知道它是否連接到您使用的別名 - 例如,在您的第一個內聯表中,您正在從一個名爲Powers的表中進行選擇,但您也將內聯表別名爲Powers,在您的主要選擇中,您指的是來自Powers的列 - SQL Server如何知道您是否指向內聯表還是原始表? –

回答

0

好的,所以這裏的基本解決方案是單獨執行SQL Server 2008存儲過程並沒有在Visual Studio 2010 Ultimate中給出正確的結果,但是一旦我將它添加到我的數據集並從C#代碼,它解決了這個問題。我不確定這是爲什麼,但我責備Visual Studio 2010旗艦版。