我有一个简单的问题,我想将当前行与前一行进行比较,如果它们是相同的,则输出RowID。 Alos当我打印@abc和@ abc2时为什么他们给我相同的结果。?
以下是我的代码:
DECLARE
@abc float,
@def datetime,
@abc2 float,
@def2 datetime
DECLARE downTime SCROLL CURSOR FOR
select D_ABC, D_DEF
OPEN downTime
FETCH First FROM downTime INTO @abc,@def
FETCH NEXT FROM downTime INTO @abc2,@def2
print @abc
print @abc2
WHILE (@@FETCH_STATUS=0)
BEGIN
// Out here I want to print the rowID's of those rows where @abc = @abc2
END
CLOSE downTime
答案 0 :(得分:2)
您不需要光标
像
这样的东西SELECT
*
FROM
myTable M
CROSS APPLY
(--get previous row
SELECT TOP 1 *
FROM myTable M2
WHERE M2.ID < M.ID
ORDER BY M2.ID
) foo
WHERE --compare
foo.col1 = M.col1 AND foo.col2 = M.col2 etc
稍微不那么精确(它会,你会得到误报)
...
WHERE
CHECKSUM(M.*) <> CHECKSUM(foo.*)