Hi,
If you need to loop through records, why not use a
cursor? And if you want to compare with today's date (without time), you need to get rid of the time part from
CURRENT_TIMESTAMP.
If you're looping through records and selecting
Mdate
and
Inv_ID
from that row, then why do you need
@mdt date
and
@invid numeric(18,0)
as
input parameters?
Example (according to your given code; simplified):
CREATE PROCEDURE sample (@amnt NUMERIC(18, 0),
@per NUMERIC(18, 0),
@acnum NUMERIC(18, 0))
AS
BEGIN
DECLARE @invid NUMERIC(18, 0);
DECLARE MyCursor CURSOR FAST_FORWARD FOR
SELECT Inv_ID FROM OnlineBankingServer.dbo.Fixed_Inv WHERE CONVERT (DATE, Mdate) = CONVERT (DATE, CURRENT_TIMESTAMP)
OPEN MyCursor
FETCH NEXT FROM MyCursor INTO @invid
WHILE @@FETCH_STATUS = 0
BEGIN
UPDATE OnlineBankingServer.dbo.Cust_Details SET accnt_bal = accnt_bal + ( ( @amnt * @per ) / 100 ) WHERE account_num = @acnum
DELETE FROM OnlineBankingServer.dbo.Fixed_Inv WHERE Inv_ID = @invid
INSERT INTO OnlineBankingServer.dbo.Transactions VALUES (@acnum, 1234, 'invd', @amnt, CURRENT_TIMESTAMP)
FETCH NEXT FROM MyCursor INTO @invid
END
CLOSE MyCursor
DEALLOCATE MyCursor
END
GO