好的,所以我的 SQL Server 2005 数据库中有以下过程。我试图弄清楚为什么它没有给我任何结果。尝试访问它的用户是管理员,但我也尝试与另一个用户一起访问它,但我仍然没有得到任何结果。Bond 在对 Bond 表的基本查询中返回。我只是想弄清楚为什么这没有返回任何结果。任何帮助表示赞赏。
ALTER PROCEDURE dbo.GetBondAmounts
(
@Username varchar(20)
)
AS
DECLARE @Admin AS bit
SELECT @Admin = Admin FROM Users WHERE Username = @Username
if(@Admin = 1)
BEGIN
RETURN
SELECT Bond.ID, SUM(Charge.BondAmount) + SUM(ChargeWithoutPower.BondAmount) Amount,
SUM(Charge.BondPremium) + SUM(ChargeWithoutPower.BondPremium) + SUM(Forfeiture.AmountPaid)
+ SUM(Forfeiture.CostOfApprehension) - SUM(Payment.Amount) - SUM(BalanceForgiveness.AmountForgiven) Balance
FROM Bond LEFT OUTER JOIN Payment ON Bond.ID = Payment.Bond
LEFT OUTER JOIN BalanceForgiveness ON Bond.ID = BalanceForgiveness.BondID
LEFT OUTER JOIN Powers ON Bond.ID = Powers.Bond
LEFT OUTER JOIN Charge ON Powers.Surety = Charge.PowerSurety
AND Powers.PowerPrefix = Charge.PowerPrefix AND Powers.PowerNumber = Charge.PowerNumber
LEFT OUTER JOIN ChargeWithoutPower ON Bond.ID = ChargeWithoutPower.ID
LEFT OUTER JOIN Forfeiture ON Bond.ID = Forfeiture.Bond
LEFT OUTER JOIN BondFee ON Bond.ID = BondFee.Bond
GROUP BY Bond.ID
END
ELSE
BEGIN
RETURN
SELECT Bond.ID, SUM(Charge.BondAmount) + SUM(ChargeWithoutPower.BondAmount) Amount,
SUM(Charge.BondPremium) + SUM(ChargeWithoutPower.BondPremium) + SUM(Forfeiture.AmountPaid)
+ SUM(Forfeiture.CostOfApprehension) - SUM(Payment.Amount) - SUM(BalanceForgiveness.AmountForgiven) Balance
FROM Bond LEFT OUTER JOIN UserAgency ON Bond.Agency = UserAgency.Agency
LEFT OUTER JOIN Payment ON Bond.ID = Payment.Bond
LEFT OUTER JOIN BalanceForgiveness ON Bond.ID = BalanceForgiveness.BondID
LEFT OUTER JOIN Powers ON Bond.ID = Powers.Bond
LEFT OUTER JOIN Charge ON Powers.Surety = Charge.PowerSurety
AND Powers.PowerPrefix = Charge.PowerPrefix AND Powers.PowerNumber = Charge.PowerNumber
LEFT OUTER JOIN ChargeWithoutPower ON Bond.ID = ChargeWithoutPower.ID
LEFT OUTER JOIN Forfeiture ON Bond.ID = Forfeiture.Bond
LEFT OUTER JOIN BondFee ON Bond.ID = BondFee.Bond
WHERE UserAgency.Username = @Username
GROUP BY Bond.ID
END
我正在使用 C# 访问此过程,如下所示:
bondingData.Merge(getBondAmountsAdapter.GetData(currentUser.Username));
这是我添加到测试并查看是否返回任何结果的行:
MessageBox.Show("Bond Count A: " + bondingData.Bond.Count.ToString() + "\r\nBond Count B: " + bondingData.GetBondAmounts.Count.ToString());
当我运行它时,这是我收到的结果:
Bond Count A: 1
Bond Count B: 0
[编辑]
好的,这是我对我的问题的解决方案:
ALTER PROCEDURE dbo.GetBondAmounts
(
@Username varchar(20)
)
AS
SELECT Bond.ID, dbo.GetBondTotal(Bond.ID) Total, dbo.GetBondBalance(Bond.ID) Balance
FROM Bond LEFT OUTER JOIN
UserAgency ON Bond.Agency = UserAgency.Agency
WHERE UserAgency.Username = @Username
OR EXISTS (SELECT * FROM Users WHERE Username = @Username AND Admin = 1)
GROUP BY Bond.ID
这些是此处调用的 2 个存储函数。不确定它的效率如何,但不会有那么多付款或与单个债券相关的任何事情。当程序有很多记录时,它可能会变慢。
功能一:
ALTER FUNCTION dbo.GetBondTotal
(
@BondID bigint
)
RETURNS money
AS
BEGIN
DECLARE @PowersTotal AS money;
DECLARE @ChargesTotal AS money;
DECLARE @BondFeeTotal AS money;
DECLARE @ForfeitureCosts AS money;
SELECT @PowersTotal = SUM(BondPremium)
FROM Charge INNER JOIN Powers ON Charge.PowerSurety = Powers.Surety
AND Charge.PowerPrefix = Powers.PowerPrefix
AND Charge.PowerNumber = Powers.PowerNumber
WHERE Bond = @BondID
SELECT @ChargesTotal = SUM(BondPremium)
FROM ChargeWithoutPower
WHERE BondID = @BondID
SELECT @BondFeeTotal = SUM(Amount)
FROM BondFee WHERE Bond = @BondID
SELECT @ForfeitureCosts = SUM(CostOfApprehension) + SUM(AmountPaid)
FROM Forfeiture WHERE Bond = @BondID
RETURN @PowersTotal + @ChargesTotal + @BondFeeTotal + @ForfeitureCosts
END
功能二:
ALTER FUNCTION dbo.GetBondBalance
(
@BondID bigint
)
RETURNS MONEY
AS
BEGIN
DECLARE @PowersTotal AS money;
DECLARE @ChargesTotal AS money;
DECLARE @BondFeeTotal AS money;
DECLARE @ForfeitureCosts AS money;
DECLARE @PaymentTotal AS money;
DECLARE @AmountForgiven AS money;
SELECT @PowersTotal = SUM(BondPremium)
FROM Charge INNER JOIN Powers ON Charge.PowerSurety = Powers.Surety
AND Charge.PowerPrefix = Powers.PowerPrefix
AND Charge.PowerNumber = Powers.PowerNumber
WHERE Bond = @BondID
SELECT @ChargesTotal = SUM(BondPremium)
FROM ChargeWithoutPower
WHERE BondID = @BondID
SELECT @BondFeeTotal = SUM(Amount)
FROM BondFee WHERE Bond = @BondID
SELECT @ForfeitureCosts = SUM(CostOfApprehension) + SUM(AmountPaid)
FROM Forfeiture WHERE Bond = @BondID
SELECT @PaymentTotal = SUM(Amount)
FROM Payment WHERE Bond = @BondID
SELECT @AmountForgiven = SUM(AmountForgiven)
FROM BalanceForgiveness WHERE BondID = @BondID
RETURN @PowersTotal + @ChargesTotal + @BondFeeTotal + @ForfeitureCosts - @PaymentTotal - @AmountForgiven
END
我相信我会创建第三个函数来覆盖金额(Charge.Amount + ChargeWithoutPower.Amount)