我的光标循环有很奇怪的行为,这是我得到的;
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
现在,如果我运行这个,我将到达光标上的第二行,它将崩溃并状态;
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)
一些类似的东西。