CREATE PROCEDURE PascalsTriangle2 @Levels INT AS BEGIN SET NOCOUNT ON DECLARE @o INT=2 DECLARE @ INT DECLARE @1 DECIMAL(38,0) DECLARE @2 DECIMAL(38,0) DECLARE @q NVARCHAR(2048) CREATE TABLE #o([1] DECIMAL(38,0)) INSERT #o VALUES(1) o:   EXEC('ALTER TABLE #o ADD['+@o+']DECIMAL(38,0)')   SELECT *INTO #r FROM #o ORDER BY 1 OFFSET(@o-2)ROWS FETCH NEXT 1 ROWS ONLY   SET @1 = 1   SET @2 = 1   SET @ = 2   i: SET @q = CONCAT('DECLARE @ TABLE (val DECIMAL(38,0));UPDATE #r SET [',@,']=ISNULL([',@,'], 0)+',@1,'OUTPUT deleted.[',@,']INTO @;SET @t=(SELECT *FROM @)') EXEC sp_executesql @q,N'@t DECIMAL(38,0) OUTPUT',@t=@2 OUTPUT      SET @1 = @2    SET @ += 1   IF @ <= @o GOTO i   INSERT #o   SELECT * FROM #r   SET @o += 1   DROP TABLE #r IF @o <= @Levels GOTO o SELECT * FROM #o DROP TABLE #o END