SQL
posted by 준치 2011. 12. 1. 15:19

DB에 들어있는 nvarchar 값이 XML 구조로 들어있었다.. 그중에 특정 값을 변경해달라네..ㅠㅠ

프로그램으로 할수도 있지만 DB로 해보고 싶어졌다.. 잘 못하면서..ㅋㅋㅋ

그래서 또 겁나 검색.. 쫄지마 ㅆㅂ.. 정신으로...

우선 출처는 http://technet.microsoft.com/ko-kr/library/ms190675.aspx

여기가 짱인듯.. 예제를 그냥 복사해서 실행... 이해가 금방가네...ㅎㅎㅎ

DECLARE @myDoc xml
SET @myDoc = '<Root>
<Location LocationID="10" 
            LaborHours="1.1"
            MachineHours=".2" >Manufacturing steps are described here.
<step>Manufacturing step 1 at this work center</step>
<step>Manufacturing step 2 at this work center</step>
</Location>
</Root>'
SELECT @myDoc

-- update text in the first manufacturing step
SET @myDoc.modify('
  replace value of (/Root/Location/step[1]/text())[1]
  with     "new text describing the manu step"
')
SELECT @myDoc
-- update attribute value
SET @myDoc.modify('
  replace value of (/Root/Location/@LaborHours)[1]
  with     "100.0"
')
SELECT @myDoc
결과를 보면 이렇게 변경...파란 부분은 
/Root/Location/step 에 첫번째 node 값을 변경
<Root>

  <Location LocationID="10" LaborHours="1.1" MachineHours=".2">
  Manufacturing steps are described here.
<step>new text describing the manu step</step>
<step>Manufacturing step 2 at this work center</step></Location>
</Root>








두번째 결과
<Root>
  <Location LocationID="10" LaborHours="100.0" MachineHours=".2">
  Manufacturing steps are described here.
<step>new text describing the manu step</step>
<step>Manufacturing step 2 at this work center</step></Location>
</Root>
/Root/Location/@LaborHours 의 값이 변경


나중에 유용하게 쓸수 있을듯.. 오늘도 퐈이링..ㅎㅎ