 <p>You need to define the table the function returns, and insert into it, rather than selecting.</p> <pre><code>CREATE FUNCTION dbo.get_numseq (@max_rows AS BIGINT,@min_rows AS BIGINT) RETURNS @t TABLE (n int) AS begin -- 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) insert @t SELECT n FROM Nums where n &gt;= @min_rows and n &lt;= @max_rows ORDER BY n return end </code></pre> <p>See <a href="http://technet.microsoft.com/en-us/library/ms191165(v=sql.105).aspx" rel="nofollow">http://technet.microsoft.com/en-us/library/ms191165(v=sql.105).aspx</a></p>