samedi 2 juillet 2016

Setting and getting in the same SELECT statement, or Complex simple math

My script generates update scripts for the purpose of testing. In this statement, what I need is something like SELECT 'Update script', @PreviousBalance = @PreviousBalance - @CorrectPrice FROM table WHERE column = @Value **Order of logic here I have the original balance of an account subtract the transaction amount (generate) insert transaction amount and new balance store that new balance as the old balance** but since 'A SELECT statement that assigns a value to a variable must not be combined with data-retrieval operations.' I need some otherway to do this in line. My task is to iterate over some initial value, and update a transactional log. So each time the @CorrectPrice must be subtracted from @PreviousBalance. Ideas? Actual query: SELECT 'UPDATE table SET Amount = ' + CAST(@CorrectPrice as nvarchar(50)) + ', SET CurrentValueUsed = ' + CAST((@PreviousBalance - @CorrectPrice) as nvarchar(50)) + ' WHERE UsageId = ' + CAST(UsageId as nvarchar(50)) as 'UpdateQueriesToRun' FROM table WHERE ContractId = @ContractID AND DateActive IN (SELECT DateActive FROM table WHERE ContractId = @ContractID) ORDER BY DateActive ASC

Aucun commentaire:

Enregistrer un commentaire