I am stuck on how to loop over a rowset and save in a variable.
Mind you, this may be pseudo-code because SQL is not my specialty.
@all_customers = select CustNum from [crrsql].[dbo].[Customer];
some loop(@all_customers as user)
//I need to find out what the Acct_balance field is and either subtract or add to bring all the balances to 0
@balance = select Acct_balance from [crrsql].[dbo].[Customer] where CustNum = user;
if @balance > 0
update [crrsql].[dbo].[Customer] set Acct_balance = 0;
INSERT INTO [crrsql].[dbo].[AR_Transactions] (cashier_ID, CustNum, Balance) VALUES (100199, user, @balance);
else
update [crrsql].[dbo].[Customer] set Acct_balance = 0;
INSERT INTO [crrsql].[dbo].[AR_Transactions] (cashier_ID, CustNum, Balance) VALUES (100199, user, "-" + @balance);
end
end loop
As you can see I am looping through the customers and within that loop I need to get the current balance and set it to zero, but first I need to find out if it's a positive or negative number to be able to figure out if the insert per user in the AR_Transactions table needs to be a positive or negative number. Could you help with the missing pieces?