mssql 질문드립니다. 0 1 585

by 미무미 [2021.05.04 18:04:42]


WITH a(num, f4) 
AS (
    select '1', '051100' union all
    select '2', '061100' union all
    select '3', '066100' union all
    select '4', '071100' union all
    select '5', '076100' union all
    select '6', '051200' union all
    select '7', '061200' union all
    select '8', '066200' union all
    select '9', '071200' union all
    select '10', '071300' union all
    select '11', '051300' union all
    select '12', '061300' union all
    select '13', '066300' union all
    select '14', '083800' union all
    select '15', '085700' union all
    select '16', '085800' union all
    select '17', '085900' union all
    select '18', '086000'

     ....

     ....
)
select * from a

update a set f4=024421 where f4=051100
update a set f4=081100 where f4=061100
update a set f4=081100 where f4=066100
update a set f4=081100 where f4=071100
update a set f4=081100 where f4=076100
update a set f4=081200 where f4=051200
update a set f4=081200 where f4=061200
update a set f4=081200 where f4=066200
update a set f4=081200 where f4=071200
update a set f4=081200 where f4=076200
update a set f4=081300 where f4=051300
update a set f4=081300 where f4=061300
update a set f4=081300 where f4=066300
update a set f4=081300 where f4=071300
update a set f4=081300 where f4=076300
update a set f4=081400 where f4=051400
update a set f4=081400 where f4=061400
update a set f4=081400 where f4=066400
update a set f4=081400 where f4=071400
update a set f4=081400 where f4=076400
update a set f4=081600 where f4=051600
update a set f4=081600 where f4=061600
update a set f4=081600 where f4=066600
update a set f4=081600 where f4=071600
update a set f4=081600 where f4=076600
update a set f4=081700 where f4=051700
update a set f4=081700 where f4=061700
update a set f4=081700 where f4=066700
update a set f4=081700 where f4=071700
update a set f4=081700 where f4=076700
update a set f4=081900 where f4=051900
update a set f4=081900 where f4=061900
update a set f4=081900 where f4=066900
update a set f4=081900 where f4=071900
update a set f4=081900 where f4=076900
update a set f4=082200 where f4=052200
update a set f4=082200 where f4=062200
update a set f4=082200 where f4=067200
update a set f4=082200 where f4=072200
update a set f4=082200 where f4=077200
update a set f4=082400 where f4=052400
update a set f4=082400 where f4=062300
update a set f4=082400 where f4=067300
update a set f4=082500 where f4=052500
update a set f4=082500 where f4=062500
update a set f4=082500 where f4=067500
update a set f4=082500 where f4=072500
update a set f4=082500 where f4=077500
update a set f4=082600 where f4=052600
update a set f4=082600 where f4=062600
update a set f4=082600 where f4=067600
update a set f4=082600 where f4=072600
update a set f4=082600 where f4=077600
update a set f4=082800 where f4=052800
update a set f4=082800 where f4=062800
update a set f4=082800 where f4=072800
update a set f4=082800 where f4=077800
update a set f4=082900 where f4=052900

 

이런 쿼리가 있을 때요..

f4의 값에 따라 case when으로 분기문으로 처리해봤자

소스가 너무 길어질 거 같은데
소스를 줄일 다른 방법이 있을까요??

by 마농 [2021.05.04 18:42:48]

변경전, 변경 후 값을 저장해둔 테이블은 없나요?
해당 테이블을 이용하여 조인 Update 가 가능합니다.
http://gurubee.net/article/79308

-- MSSQL --
UPDATE a
   SET a.f4 = b.f4_new
  FROM a
 INNER JOIN b
    ON a.f4 = b.f4_old
;

 

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