merge into 관련 문의 드립니다. 0 0 1,445

by 콩나무 [SQL Query] [2017.06.07 12:29:12]


update table1 a
set a.sal = (select sum(sal)
                   from table2 b
                   where a.tid=b.tid
                   and b.status='0'
                   and a.data=b.data),
      a.no (select max(no)
                   from table2 b
                   where a.tid=b.tid
                   and b.status='0'
                   and a.data=b.data)
where a.state_cd ='1'
 and exists ( select 1
                    from table2 b
                    where a.tid=b.tid
                    and b.status='0'
                    and a.data=b.data)

위 update 문장을 불 필요 하게 여러번 엑세스 해서

merge into table1 a

using (select sum(sal) sal,max(no) no ,tid ,data

from table2

where stats='0'

group by tid,data) b

on (a.tid=b.tid and a.data=b.data and a.state_cd='1')

when matched then

update set a.sal=b.sal , a.no=b.no

이문장으로 바꾸려고 하는데, 가능 한 건지 그리고 궁금한 점은 on절에 state_cd 라는 조건 추가 하는 것과

저문장 대신에 on 절에 a.sate_cd='1'조건을 빼고 when matched then update 절에 where a.state_cd='1' 값을 넣는 것과 차이점이 있나요?제가 넣은 테스트 값은 비슷한데, 다른 이유가 있는지 궁금 합니다.

 

 

 

 

 

 

 

댓글등록
SQL문을 포맷에 맞게(깔끔하게) 등록하려면 code() 버튼을 클릭하여 작성 하시면 됩니다.
로그인 사용자만 댓글을 작성 할 수 있습니다. 로그인, 회원가입