我在sql server 2005中創建一個udf返回一個表,我在udf中使用這個表來計算客戶端的solde,但有時結果是正確的,有時是不正確的(錯誤值是在字段 「Solde」)在表達@ Solde = @Solde + @Debit - @Creditsql server 2005函數計算結果錯誤
這是函數的代碼:
CREATE FUNCTION HistoryClient
(
@IdClient int
)
RETURNS @Table_Var
TABLE (NAuto BigInt Identity(1,1),
Numero nvarchar(20),
Ligne nvarchar(50),
IdClient int,
Matricule nvarchar(30),
DateBL datetime,
Libelle nvarchar(50),
Qte decimal(18, 2),
PU money,
Debit money,
Credit money,
Solde money
)
AS
BEGIN
Declare @SoldeInitial money
DECLARE @Debit money
DECLARE @Credit money
DECLARE @Solde money
DECLARE @Solde1 money
DECLARE @Ligne nvarchar(50)
DECLARE History_Cursor CURSOR FOR
SELECT Ligne, Debit, Credit, Solde
FROM @Table_Var
FOR UPDATE OF Solde
Select @SoldeInitial = SoldeInitial
From Client
Where IdClient= @IdClient
INSERT INTO @table_Var (Numero, Ligne, IdClient, Matricule, DateBL, Libelle, Qte, PU, Debit, Credit, Solde)
Select Numero, Ligne, IdClient, Matricule, DateBL, Libelle, Qte, PU, Debit, Credit, 0
From vwHistoryAllClients
Where IdClient= @IdClient
Order By Ligne
OPEN History_Cursor
FETCH NEXT FROM History_Cursor Into @Ligne, @Debit, @Credit, @Solde1
SET @Solde = @SoldeInitial
WHILE @@FETCH_STATUS = 0
BEGIN
SET @Solde= @Solde + @Debit - @Credit
UPDATE @Table_Var
SET Solde = @Solde
WHERE CURRENT Of History_Cursor
FETCH NEXT FROM History_Cursor Into @Ligne, @Debit, @Credit, @Solde1
END
CLOSE History_Cursor
DEALLOCATE History_Cursor
RETURN
END
任何溶液
是否有錯誤或錯誤的值?你的數據是否包含NULL值? –
在n行之後solde值不正確,開始時solde值正確,我的數據不包含空值我使用isnull,但有些結果 – ghostdz