I create a udf in sql server 2005 return a table, i use this table in the udf for calculate the solde for client, but sometimes the result is correct and sometimes is not correct (the error value is in the Field "Solde" ) in the expression @Solde= @Solde + @Debit - @Credit
This is the code of the function :
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
any solution