试图创建一个函数,但我不知道它有什么问题。它在关键字“WITH”附近给出错误语法错误。
CREATE FUNCTION dbo.get_numseq (@max_rows AS BIGINT,@min_rows AS BIGINT) RETURNS TABLE
AS
-- returns up to 4,294,967,296 records
WITH
lv0 AS (SELECT 0 AS g UNION ALL SELECT 0),
lv1 AS (SELECT 0 AS g FROM lv0 AS a CROSS JOIN lv0 AS b),
lv2 AS (SELECT 0 AS g FROM lv1 AS a CROSS JOIN lv1 AS b),
lv3 AS (SELECT 0 AS g FROM lv2 AS a CROSS JOIN lv2 AS b),
lv4 AS (SELECT 0 AS g FROM lv3 AS a CROSS JOIN lv3 AS b),
lv5 AS (SELECT 0 AS g FROM lv4 AS a CROSS JOIN lv4 AS b),
Nums AS (SELECT ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS n FROM lv5)
SELECT n FROM Nums where n >= @min_rows and n <= @max_rows ORDER BY n
GO