Oracle: Using CTE with update clause

后端 未结 3 1354
梦谈多话
梦谈多话 2021-01-22 00:21

Can I make an update using common table expression in oracle database?

I am getting error ORA-00928: missing SELECT keyword when I am trying this:

相关标签:
3条回答
  • 2021-01-22 00:56

    Since average salary just a scalar value you can do

    update instructor
       set salary = case
           when salary <= (select avg(t.salary) from instructor t) then salary * 1.05 
           else salary * 1.03                                     
       end
    

    In that case Oracle first compute the average (say 1234.4567) and then perform the update.

    0 讨论(0)
  • 2021-01-22 01:09

    Basing on another answer on correlated update of key-preserved view, here is another possible option to use CTE with update in Oracle SQL avoiding duplication of where clause:

    update (
        with cte as (select avg(salary) average_salary from instructor)
        select id, salary, cte.average_salary from instructor cross join cte
        where <some_condition>
    )
    set salary = case
        when salary <= average_salary/2 then salary * 1.1 
        when salary <= average_salary then salary * 1.05 
        else salary * 1.03                                     
    end
    

    In case of self-join this can be simplified to CTE-less version:

    update (
        select id, salary, (select avg(salary) from instructor) average_salary 
        from instructor
        where <some_condition>
    )
    set salary = case
        when salary <= average_salary/2 then salary * 1.1 
        when salary <= average_salary then salary * 1.05 
        else salary * 1.03                                     
    end
    
    0 讨论(0)
  • 2021-01-22 01:11

    Can I do something like this in oracle database?

    Well, it is not about whether you could do it or not. It is about whether you need to do it or not. In your query I don't see any filter criteria. You want to update all the rows? I don't see a need of CTE in your case.

    When do you need a CTE, i.e. a with clause as a sub-query factoring method whenever you have a scenario where the sub-query is executed multiple times. You use a WITH clause to make sure the subquery is executed once, and the resultset is stored as a temp table.

    Yes, you could use WITH clause for an UPDATE statement.

    For example,

    UPDATE TABLE t
    SET t.column1, t.column2 = (SELECT column1, column2 FROM 
                                           (
                                            WITH cte AS(
                                       SELECT ... FROM another_table
                                                     )
                                             SELECT * FROM cte
                                            )
    

    You could use a MERGE statement USING the WITH clause.

    For example,

    SQL> MERGE INTO emp e USING
      2  (WITH average AS
      3    (SELECT deptno, AVG(sal) avg_sal FROM emp group by deptno)
      4  SELECT * FROM average
      5  ) u
      6  ON (e.deptno = u.deptno)
      7  WHEN MATCHED THEN
      8  UPDATE SET e.sal      =
      9    CASE
     10      WHEN e.sal <= u.avg_sal
     11      THEN e.sal * 1.05
     12      ELSE e.sal * 1.03
     13    END
     14  /
    
    14 rows merged.
    
    SQL>
    
    0 讨论(0)
提交回复
热议问题