select 결과를 update 하고 싶습니다~ 1 1 3,267

by 상혀니 [SQL Query] select update join [2015.01.24 05:10:27]


처음으로 질문 올려봅니다. 앞으로도 잘 부탁드려요~^^a

제 숙제는 test_sh12 테이블과 test_pro 테이블을 조인하여

test_sh12 테이블의 tab_nm 컬럼과 col_nm 컬럼에 AA,BB라는 값들을 test_pro 의

tab_nm 컬럼과 col_nm 컬럼 값으로 update를 하고 싶은데요 한다고 계속 해봤는데

서브쿼리 자체만으로는 값들이 정상적으로 나오는데

update 문을 적용하면 그냥 null로 치환되고 맙니다.

어느 부분이 문제인지 알려주시면 감사하겠습니다~

완전 초보라 쿼리 자체가 이상하다면 따끔한 질책도 달게 받아들이겠습니다

부탁드릴게요~ 고수님들~ ㅜㅜ


update test_sh12 a
   set (tab_nm, col_nm)
     = (
      select b.tab_nm, b.col_nm
          from (
              select distinct t.dbname
                   , t.tab_id
                   , p.tab_nm
                   , t.col_id
                   , p.col_nm
                   , t.exl_no
                from test_sh12 t
                   , test_pro  p
               where 1=1
                 and t.tab_id = p.tab_id
                 and t.col_id = p.col_id
                 and case when rtrim(t.tab_nm) in ('AA','BB') then 1
                          when rtrim(t.col_nm) is null then 1
                          when rtrim(t.col_nm) in ('AA','BB') then 1
                     else 0
                     end = 1
               order by dbname
                      , tab_id 
              ) b
        where 1=1
          and b.dbname = a.dbname 
          and b.tab_id = a.tab_id  
          and b.tab_nm = a.tab_nm  
          and b.col_id = a.col_id  
          and b.col_nm = a.col_nm  
          and b.exl_no = a.exl_no  
      ) 
where 1=1
  and rtrim(a.tab_nm) in ('AA','BB')  
   or rtrim(a.col_nm) in ('AA','BB')
   or rtrim(a.col_nm) is null
;

 

by 마농 [2015.01.26 08:34:07]
UPDATE test_sh12 a
   SET (tab_nm, col_nm)
     = (SELECT p.tab_nm, p.col_nm
          FROM test_pro  p
         WHERE p.tab_id = a.tab_id
           AND p.col_id = a.col_id
        )
 WHERE RTRIM(a.tab_nm) IN ('AA','BB')
    OR RTRIM(a.col_nm) IN ('AA','BB')
    OR RTRIM(a.col_nm) IS NULL
;

 

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