我不知道为什么会这么慢
DECLARE kursor CURSOR FOR
SELECT mediatime, ShortDate, MediaDateID, TimeID
FROM DateTimeMCross
DECLARE @mediatime INT, @ShortDate DATE, @MediaDateID INT, @TimeID INT, @tmpDate DATE
SET @mediatime = 0;
OPEN kursor
FETCH NEXT FROM kursor INTO @mediatime, @ShortDate, @MediaDateID, @TimeID
WHILE @@FETCH_STATUS = 0
BEGIN
/*SET @tmpDate = (select DATEADD(day,-1,@shortDate))*/
IF @mediatime >= 2400
BEGIN
UPDATE DateTimeMCross
SET DateTimeMCross.newMediaDateID = 2/*(SELECT YEAR(@tmpDate) * 10000 + MONTH(@tmpDate) * 100 + DAY(@tmpDate))*/
WHERE DateTimeMCross.MediaDateID = @MediaDateID
AND DateTimeMCross.TimeID = @TimeID
END
ELSE IF @mediatime < 2400 AND @mediatime >= 0
BEGIN
UPDATE DateTimeMCross
SET DateTimeMCross.newMediaDateID = @MediaDateID
WHERE DateTimeMCross.MediaDateID = @MediaDateID
AND DateTimeMCross.TimeID = @TimeID
END
FETCH NEXT FROM kursor INTO @mediatime, @ShortDate, @MediaDateID, @TimeID
END
CLOSE kursor
DEALLOCATE kursor
我在使用光标的一个语句之前制作它很快,这真的很慢。
我在开始时认为它是由DATEADD()
和其他功能引起的,但没有。在评论之后它和以前一样慢
答案 0 :(得分:5)
这实际上根本不需要是光标。好像你想要一个简单的UPDATE
:
UPDATE DateTimeMCross
SET newMediaDateID = CASE
WHEN mediatime >= 2400 THEN 2
ELSE MediaDateID
END
;
答案 1 :(得分:1)
您只需要UPDATE d
SET d.newMediaDateID = CASE
WHEN mediatime>=2400 THEN 2
WHEN mediatime <2400 AND mediatime >=0 THEN MediaDateID
END
FROM DateTimeMCross d
,而不是光标:
{{1}}