SET DATEFIRST CURSOR WHILE LOOP 错误 - 设置选项已更改
SET DATEFIRST CURSOR WHILE LOOP Error - set options have changed
我的光标循环出现了非常奇怪的行为,这就是我得到的结果;
DECLARE @StartDate AS DATE
DECLARE @ID INT
DECLARE CursorTest CURSOR FOR
SELECT ID FROM tblSomething
OPEN Schedule
FETCH NEXT FROM CursorTest INTO @ID
WHILE @@FETCH_STATUS = 0
BEGIN
SELECT @StartDate = StartDate FROM tblAnother WHERE ID = @ID
SELECT @StartDate --12/06/2018
-- NOW WE MOD IT
SET DATEFIRST 6 -- WE START ON SATURDAY 1
SET @StartDate = DATEPART(dw,@StartDate)
SELECT @StartDate -- ANSWER IS 4
FETCH NEXT FROM CursorTest INTO @ID
END
CLOSE CursorTest
DEALLOCATE CursorTest
现在,如果我 运行 到达 CURSOR 的第二行,它将崩溃并显示;
Could not complete cursor operation because the set options have changed since the cursor was declared.
现在如果我注释掉;
--SET DATEFIRST 6
错误消失了,所以我认为 SET DATEFIRST 正在修改导致错误的数据库。
是否可以使用类似的东西解决这个问题;
SET @StartDate = DATEPART(dw,@StartDate,DATEFIRST 6)
类似的东西。
你可以移动 SET
:
DECLARE @StartDate AS DATE
DECLARE @ID INT
DECLARE CursorTest CURSOR FOR
SELECT ID FROM tblSomething
SET DATEFIRST 6 -- WE START ON SATURDAY 1
OPEN Schedule
FETCH NEXT FROM CursorTest INTO @ID
WHILE @@FETCH_STATUS = 0
BEGIN
SELECT @StartDate = StartDate FROM tblAnother WHERE ID = @ID
SELECT @StartDate --12/06/2018
-- NOW WE MOD IT
SET @StartDate = DATEPART(dw,@StartDate)
SELECT @StartDate -- ANSWER IS 4
FETCH NEXT FROM CursorTest INTO @ID
END
CLOSE CursorTest
DEALLOCATE CursorTest
我修好了;
SET DATEFIRST 6 -- outside CURSOR LOOP;
WHILE @@FETCH_STATUS = 0
BEGIN
SET @StartWeek = DATEPART(wk,@StartDate) -- CORRECT BY DATEFIRST 6
SET @StartDay = (DATEPART(dw,@StartDate) + @@DATEFIRST - 1 - 1) % 7 + 1 -- MONDAY 1
END
我的光标循环出现了非常奇怪的行为,这就是我得到的结果;
DECLARE @StartDate AS DATE
DECLARE @ID INT
DECLARE CursorTest CURSOR FOR
SELECT ID FROM tblSomething
OPEN Schedule
FETCH NEXT FROM CursorTest INTO @ID
WHILE @@FETCH_STATUS = 0
BEGIN
SELECT @StartDate = StartDate FROM tblAnother WHERE ID = @ID
SELECT @StartDate --12/06/2018
-- NOW WE MOD IT
SET DATEFIRST 6 -- WE START ON SATURDAY 1
SET @StartDate = DATEPART(dw,@StartDate)
SELECT @StartDate -- ANSWER IS 4
FETCH NEXT FROM CursorTest INTO @ID
END
CLOSE CursorTest
DEALLOCATE CursorTest
现在,如果我 运行 到达 CURSOR 的第二行,它将崩溃并显示;
Could not complete cursor operation because the set options have changed since the cursor was declared.
现在如果我注释掉;
--SET DATEFIRST 6
错误消失了,所以我认为 SET DATEFIRST 正在修改导致错误的数据库。
是否可以使用类似的东西解决这个问题;
SET @StartDate = DATEPART(dw,@StartDate,DATEFIRST 6)
类似的东西。
你可以移动 SET
:
DECLARE @StartDate AS DATE
DECLARE @ID INT
DECLARE CursorTest CURSOR FOR
SELECT ID FROM tblSomething
SET DATEFIRST 6 -- WE START ON SATURDAY 1
OPEN Schedule
FETCH NEXT FROM CursorTest INTO @ID
WHILE @@FETCH_STATUS = 0
BEGIN
SELECT @StartDate = StartDate FROM tblAnother WHERE ID = @ID
SELECT @StartDate --12/06/2018
-- NOW WE MOD IT
SET @StartDate = DATEPART(dw,@StartDate)
SELECT @StartDate -- ANSWER IS 4
FETCH NEXT FROM CursorTest INTO @ID
END
CLOSE CursorTest
DEALLOCATE CursorTest
我修好了;
SET DATEFIRST 6 -- outside CURSOR LOOP;
WHILE @@FETCH_STATUS = 0
BEGIN
SET @StartWeek = DATEPART(wk,@StartDate) -- CORRECT BY DATEFIRST 6
SET @StartDay = (DATEPART(dw,@StartDate) + @@DATEFIRST - 1 - 1) % 7 + 1 -- MONDAY 1
END