我们的首席数据库程序员这周外出度假,我被困住了。
我有一个正在创建的视图,其中包括以下子查询:
select cast(cast(getdate() + i as date) as datetime) DATEVALUE
from NumbersTable(1,100,1)
我不知道这是做什么的,当我尝试在 Squirrel 中运行它时,我得到:
Error: Procedure or function NumbersTable has too many arguments specified.
SQLState: 37000
ErrorCode: 8144
下面是为 NumbersTable 创建的函数:
CREATE FUNCTION NumbersTable (
@fromNumber int,
@toNumber int,
@byStep int
) RETURNS TABLE
RETURN (
WITH CTE_NumbersTable AS (
SELECT @fromNumber AS i
UNION ALL
SELECT i + @byStep
FROM CTE_NumbersTable
WHERE
(i + @byStep) <= @toNumber
)
SELECT *
FROM CTE_NumbersTable
)
;
和
CREATE FUNCTION NumbersTable (
@fromNumber int,
@toNumber int
) RETURNS TABLE
RETURN (
WITH T_0_THRU_15 AS (
SELECT 0 j UNION ALL
SELECT 1 UNION ALL
SELECT 2 UNION ALL
SELECT 3 UNION ALL
SELECT 4 UNION ALL
SELECT 5 UNION ALL
SELECT 6 UNION ALL
SELECT 7 UNION ALL
SELECT 8 UNION ALL
SELECT 9 UNION ALL
SELECT 10 UNION ALL
SELECT 11 UNION ALL
SELECT 12 UNION ALL
SELECT 13 UNION ALL
SELECT 14 UNION ALL
SELECT 15
)
SELECT T1.j + (T2.j*16) + (T3.j*256) + @fromNumber i
FROM T_0_THRU_15 T1, T_0_THRU_15 T2, T_0_THRU_15 T3
WHERE T1.j + (T2.j*16) + (T3.j*256) + @fromNumber <= @toNumber
)
;
我正在针对 Microsoft SQL Server 2008 R2 运行它。奇怪的是,我相信我之前已经针对这个数据库运行过这段代码并且它有效,我现在根本不知道该怎么做。