如何使用“With”语句在Oracle中进行更新查询更短?

时间:2014-02-17 09:02:18

标签: sql oracle sql-update with-statement

我正在构建更新查询。

但由于无用的重复“With”声明,它似乎相当混乱。

你有什么想让它变短吗?

示例查询如下。

UPDATE TABLE1
   SET C1 = (
              WITH W_VIEW AS
              (
                SELECT *
                  FROM TABLE1
              )
              SELECT C_VALUE
                FROM W_VIEW
               WHERE ROWNUM = 1

            ) 

 WHERE C2 IN (

              WITH W_VIEW AS
              (
                SELECT *
                  FROM TABLE1
              )
              SELECT C2_VALUES
                FROM W_VIEW
               WHERE STH < 10

             );

我只想使用W_VIEW一次。

例如,

WITH W_VIEW AS
(
    SELECT *
      FROM TABLE1
)
UPDATE TABLE1
   SET C1 = (SELECT C_VALUE FROM W_VIEW WHERE ROWNUM = 1) 
 WHERE C2 IN (SELECT C2_VALUES FROM W_VIEW  WHERE STH < 10)

。 如果该查询正在运行,我会很高兴:)

1 个答案:

答案 0 :(得分:0)

也许这一个:

WITH w_view 
     AS (SELECT * 
         FROM   table1) 
UPDATE (SELECT * 
        FROM   table1 
        WHERE  c2 IN (SELECT c2_values 
                      FROM   w_view 
                      WHERE  sth < 10)) 
SET    c1 = (SELECT c_value 
             FROM   w_view 
             WHERE  rownum = 1)