使用WITH进行多项操作


16

有没有一种方法可以使用该WITH语句执行多个操作?

就像是

WITH T AS
(
  SELECT * FROM Tbl
)
BEGIN
  OPEN P_OUTCURSOR FOR
    SELECT * FROM T;

  SELECT COUNT(*) INTO P_OUTCOUNT FROM T;
END;

我想选择一些数据及其数量...

Answers:


17

CTE之后只能有一份声明。但是,您可以基于上一个定义后续的CTE:

WITH t1 AS (
   SELECT a, b, c
   FROM table1
)
, t2 AS (
   SELECT b
   FROM t1
   WHERE a = 5
)
SELECT *
FROM t2;

假定您要计算行数并从同一结果集中填充ref游标,则执行以下操作之一可能更合适:

  • 创建一个视图
  • 在临时表中暂存临时结果

最后,如果查询足够简单,则只需为计数计数一次,然后为游标写入一次即可。在这种情况下,简单性和可读性胜过DRY原则。


By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.