1

重複の可能性:
SQL ストアド プロシージャのパラメータが破損していますか?

単純な SQL Server のスカラー値関数が 0 を返す状況に陥っています。何が間違っているのか誰か教えてください。

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go

ALTER FUNCTION [dbo].[OpeningBalance]
(
    @AccountCode AS varchar,
    @FromDate AS datetime
)
RETURNS numeric
AS
BEGIN
    DECLARE @Balance AS numeric

    SELECT     
      @Balance = SUM(Debit) - SUM(Credit)
    FROM 
    (
        SELECT     
            CASE 
               WHEN Debit - Credit > 0 THEN Debit - Credit 
               ELSE 0 
            END AS Debit, 
            CASE 
              WHEN Credit - Debit > 0 THEN Credit - Debit 
              ELSE 0 
            END AS Credit
        FROM
        (
             SELECT     
               SUM(VoucherDetail.Debit) AS Debit, 
               SUM(VoucherDetail.Credit) AS Credit
             FROM          VoucherMaster AS VoucherMaster 
             INNER JOIN VoucherDetail AS VoucherDetail 
                     ON VoucherMaster.ID = VoucherDetail.VoucherMasterID
             WHERE (VoucherDetail.AccountCode = @AccountCode) 
               AND (VoucherMaster.VoucherDate < @FromDate)
        ) AS Data
        UNION ALL
        SELECT     
          CASE 
            WHEN OpeningBalance > 0 THEN OpeningBalance 
            ELSE 0 
          END AS Debit, 
          CASE 
            WHEN OpeningBalance < 0 THEN OpeningBalance * - 1 
            ELSE 0 
          END AS Credit
        FROM ChartofAccounts
        WHERE (AccountCode = @AccountCode)
    ) AS OpeningData
    RETURN @Balance
END

ただし、このクエリをクエリ アナライザーで実行すると、必要な回答が得られます。

4

0 に答える 0