1. 0부터 9까지 10건 출력

WITH DUMY AS 
(
	SELECT 0 AS SEQ
	UNION ALL
	SELECT SEQ + 1 FROM DUMY
	WHERE SEQ + 1 < 10
)
SELECT *
FROM DUMY
GO

 

2. 오늘부터 10일후 까지 출력

WITH DUMY AS 
(
	SELECT GETDATE() AS DT
	UNION ALL
	SELECT DT + 1 FROM DUMY
	WHERE DT + 1 < GETDATE() + 10
)
SELECT DT
FROM DUMY
GO

 

3. DUAL 테이블 구현

CREATE FUNCTION dbo.DUAL (@ROW_COUNT INT = 1)
RETURNS TABLE 
AS
RETURN
(
    WITH DUMY AS 
    (
        SELECT 1 AS SEQ
	    UNION ALL
	    SELECT SEQ + 1 FROM DUMY
	    WHERE SEQ + 1 <= @ROW_COUNT
    )
    SELECT SEQ
    FROM DUMY
);
GO


SELECT GETDATE() 
FROM DBO.DUAL(365)
OPTION (MAXRECURSION 0) -- Recursion 무제한 옵션
;


-- STL에서 사용한 방법
----------------------------------------------------
DROP FUNCTION DBO.DUAL;
GO

CREATE FUNCTION DBO.DUAL (@ROW_COUNT INT = 1)
RETURNS @DUAL TABLE 
(   SEQ INT NOT NULL PRIMARY KEY
)
AS
BEGIN
    WITH DUMY AS 
    (
        SELECT 1 AS SEQ
        UNION ALL
        SELECT SEQ + 1 FROM DUMY
        WHERE SEQ + 1 <= @ROW_COUNT
    )
    INSERT INTO @DUAL
        SELECT SEQ
        FROM DUMY
        OPTION (MAXRECURSION 0)

    RETURN	
END
GO


SELECT GETDATE() 
FROM DBO.DUAL(365);