I have been working on creating a nested while loop in SQL, but having issues with the while loop. I think the main issue is with my outer loop. Any suggestions?
USE HW_DB;
IF OBJECT_ID('dbo.PythagoreanTriangles') IS NOT NULL
DROP TABLE dbo.PythagoreanTriangles;
GO
CREATE TABLE PythagoreanTriangles
(
Side1 INT NOT NULL,
Side2 INT NOT NULL,
Hypotenuse FLOAT NOT NULL
);
DECLARE @side1 AS INT = 1;
DECLARE @side2 AS INT = 1;
DECLARE @count AS INT = 1;
DECLARE @element AS INT = 0;
WHILE (@side1 = @count)
BEGIN
WHILE @side2 <= 10 BEGIN
INSERT INTO dbo.PythagoreanTriangles
VALUES (@side1, @side2, SQRT((@side2 * @side2) + (@side1 * @side1)));
UPDATE dbo.PythagoreanTriangles SET Hypotenuse = ROUND(Hypotenuse, 2)
SET @side2 = @side2 + 1;
SET @count = @count + 1;
SET @element = @element + 1;
IF @element = 10
BEGIN
SET @side1 = @side1 + 1;
SET @element = 0;
END;
END;
END;
Output should look like the following:
1 1 1.41
1 2 2.24
1 3 ...
1 4 ...
1 5
1 6
1 7
1 8
1 9
1 10
2 2
2 3
2 4
2 5
2 6
2 7
2 8
2 9
2 10
..........
........ etc
9 9
9 10
10 10