2

我正在编写一个按查看日期删除旧行的程序:

;WITH pv AS (
    SELECT
        RN = ROW_NUMBER() OVER (ORDER BY viewed DESC)
    FROM
        previouslyViewed
    WHERE
        userId = @userId
)
DELETE FROM pv WHERE RN >= 10

这适用于 SQL Server,但不适用于 Oracle。

Oracle 不支持 WITH DELETE 组合。它也不支持 DELETE ORDER BY 组合(理论上可以与 rownum 一起使用以达到相同的结果)。我尝试使用 rownum 创建一个临时视图并从中删除,但出现 Oracle 错误 - 使用 rownum 时,您似乎无法从视图中删除。

有没有人有任何指示?

4

1 回答 1

4

你可以这样做:

DELETE FROM previouslyViewed WHERE pkcol IN
( SELECT pkcol FROM
  (
      SELECT pkcol, ROW_NUMBER() OVER (ORDER BY viewed DESC) RN
      FROM previouslyViewed
      WHERE userId = :userId
  )
  WHERE RN >= 10
);

(更改pkcol为表的主键列)

于 2011-07-27T11:08:20.527 回答