I have a stored procedure with a lot of calculation (one example below). Despite it does what intended I would like to have an easier, neat formula, something with a loop. Can anybody help, please
SQL
SET @result1 = CONVERT(decimal(6,2),(@a1*200000)/@v1);
SET @result2 = CONVERT(decimal(6,2),((@a1+@a2)*200000)/(@v1+@v2));
SET @result3 = CONVERT(decimal(6,2),((@a1+@a2+@a3)*200000)/(@v1+@v2+@v3));
SET @result4 = CONVERT(decimal(6,2),((@a1+@a2+@a3+@a4)*200000)/(@v1+@v2+@v3+@v4));
SET @result5 = CONVERT(decimal(6,2),((@a1+@a2+@a3+@a4+@a5)*200000)/(@v1+@v2+@v3+@v4+@v5));
SET @result6 = CONVERT(decimal(6,2),((@a1+@a2+@a3+@a4+@a5+@a6)*200000)/(@v1+@v2+@v3+@v4+@v5+@v6));
SET @result7 = CONVERT(decimal(6,2),((@a1+@a2+@a3+@a4+@a5+@a6+@a7)*200000)/(@v1+@v2+@v3+@v4+@v5+@v6+@v7));
SET @result8 = CONVERT(decimal(6,2),((@a1+@a2+@a3+@a4+@a5+@a6+@a7+@a8)*200000)/(@v1+@v2+@v3+@v4+@v5+@v6+@v7+@v8));
SET @result9 = CONVERT(decimal(6,2),((@a1+@a2+@a3+@a4+@a5+@a6+@a7+@a8+@a9)*200000)/(@v1+@v2+@v3+@v4+@v5+@v6+@v7+@v8+@v9));
SET @result10 = CONVERT(decimal(6,2),((@a1+@a2+@a3+@a4+@a5+@a6+@a7+@a8+@a9+@a10)*200000)/(@v1+@v2+@v3+@v4+@v5+@v6+@v7+@v8+@v9+@v10));
SET @result11 = CONVERT(decimal(6,2),((@a1+@a2+@a3+@a4+@a5+@a6+@a7+@a8+@a9+@a10+@a11)*200000)/(@v1+@v2+@v3+@v4+@v5+@v6+@v7+@v8+@v9+@v10+@v11));
SET @result12 = CONVERT(decimal(6,2),((@a1+@a2+@a3+@a4+@a5+@a6+@a7+@a8+@a9+@a10+@a11+@a12)*200000)/(@v1+@v2+@v3+@v4+@v5+@v6+@v7+@v8+@v9+@v10+@v11+@v12));

Brahma Prakash ShuklaRank #633 · 3.8K reputationPosted Oct 24, 2024, 12:15 PM EDT
You can definitely refactor this repetitive calculation using a loop. SQL Server does not have native arrays, but you can handle this by iterating over the variables using a
WHILEloop. Here's how you can refactor the procedure using a loop:DECLARE @i INT = 1; DECLARE @sumA DECIMAL(12, 2) = 0; DECLARE @sumV DECIMAL(12, 2) = 0; DECLARE @currentResult DECIMAL(6, 2); WHILE @i <= 12 BEGIN -- Accumulate sums SET @sumA = @sumA + CASE @i WHEN 1 THEN @a1 WHEN 2 THEN @a2 WHEN 3 THEN @a3 WHEN 4 THEN @a4 WHEN 5 THEN @a5 WHEN 6 THEN @a6 WHEN 7 THEN @a7 WHEN 8 THEN @a8 WHEN 9 THEN @a9 WHEN 10 THEN @a10 WHEN 11 THEN @a11 WHEN 12 THEN @a12 END; SET @sumV = @sumV + CASE @i WHEN 1 THEN @v1 WHEN 2 THEN @v2 WHEN 3 THEN @v3 WHEN 4 THEN @v4 WHEN 5 THEN @v5 WHEN 6 THEN @v6 WHEN 7 THEN @v7 WHEN 8 THEN @v8 WHEN 9 THEN @v9 WHEN 10 THEN @v10 WHEN 11 THEN @v11 WHEN 12 THEN @v12 END; -- Perform calculation and store the result SET @currentResult = CONVERT(DECIMAL(6,2), (@sumA * 200000) / @sumV); -- Dynamically assign the result to variables (based on the index) -- You could also store this in a temp table if dynamic assignment isn't needed IF @i = 1 SET @result1 = @currentResult; IF @i = 2 SET @result2 = @currentResult; IF @i = 3 SET @result3 = @currentResult; IF @i = 4 SET @result4 = @currentResult; IF @i = 5 SET @result5 = @currentResult; IF @i = 6 SET @result6 = @currentResult; IF @i = 7 SET @result7 = @currentResult; IF @i = 8 SET @result8 = @currentResult; IF @i = 9 SET @result9 = @currentResult; IF @i = 10 SET @result10 = @currentResult; IF @i = 11 SET @result11 = @currentResult; IF @i = 12 SET @result12 = @currentResult; -- Increment the loop SET @i = @i + 1; ENDMarius VasileRank #785 · 2.9K reputationPosted Oct 24, 2024, 12:48 PM EDT
It works well and looks pretty good now. Thank you Brahma!