메뉴 건너뛰기

tnt_db

Oracle select -> update 구문 샘플

구퍼 2010.01.21 12:17 조회 수 : 5945

update
UPDATE tmb_act_plan a
   SET (a.total_gr, a.gr_1, a.gr_2, a.gr_3,  a.gr_4, a.gr_5, a.gr_6, a.gr_7, a.gr_8, a.gr_9, a.gr_10, a.gr_11, a.gr_12) =
          (SELECT b.total_gr, b.gr_1, b.gr_2, b.gr_3, b.gr_4, b.gr_5, b.gr_6, b.gr_7, b.gr_8, b.gr_9, b.gr_10, b.gr_11, b.gr_12
             FROM (SELECT   b.use_yy, b.charge_id, b.task_cd, b.task_act_cd,
                            b.gr_gbn, SUM (b.total_gr) total_gr,
                            SUM (b.gr_1) gr_1, SUM (b.gr_2) gr_2,
                            SUM (b.gr_3) gr_3, SUM (b.gr_4) gr_4,
                            SUM (b.gr_5) gr_5, SUM (b.gr_6) gr_6,
                            SUM (b.gr_7) gr_7, SUM (b.gr_8) gr_8,
                            SUM (b.gr_9) gr_9, SUM (b.gr_10) gr_10,
                            SUM (b.gr_11) gr_11, SUM (b.gr_12) gr_12
                       FROM tsi_gr b
                      WHERE b.use_yy = '2009'
                        AND b.charge_id = '222200331'
                        AND b.task_cd = '574'
                   GROUP BY use_yy, charge_id, task_cd, task_act_cd, gr_gbn) b
            WHERE a.par_yy = b.use_yy
              AND a.charge_id = b.charge_id
              AND a.kpi_cd = b.task_cd
              AND a.task_act_cd = b.task_act_cd
              AND a.gr_gbn = b.gr_gbn)
 WHERE (a.par_yy, a.charge_id, a.kpi_cd, a.task_act_cd, a.gr_gbn) IN (
          SELECT   b.use_yy, b.charge_id, b.task_cd, b.task_act_cd, b.gr_gbn
              FROM tsi_gr b
             WHERE b.use_yy = '2009'
               AND b.charge_id = '222200331'
               AND b.task_cd = '574'
          GROUP BY use_yy, charge_id, task_cd, task_act_cd, gr_gbn)

1. update문의 괄호안에 있는 where는 update대상이 되는 키를 조건에 넣는 부분이 됨
2. 마지막 WHERE절은 update대상이 되는 테이블과 업데이트 하는 테이블의 각  키가 연결될 수 있는 값을 정의함
위로