如何从xml中提取值到sql变量?

时间:2015-03-02 20:28:23

标签: sql sql-server xml xml-parsing sql-server-2012

如何从xml以下的某个变量中获取名称值?

<proc
    name="DB_NAME.dbo.up_SP"
    thread_allowed="0"
    operation_id="12"
    queueCreatedDate="2015-01-12T07:14:00.640"
    referenceStub="02216CCE-FA4A-4C27-B130-A835F514E22D"
    callback-sql="UPDATE dbo.SOME_TABLE SET is_running = 0, last_modified_by = 'up_AsyncMessage', last_modified_date = getdate() WHERE async_job_stub = '022000CE-F00-0000-B000-030000022D'"
   />

1 个答案:

答案 0 :(得分:0)

DECLARE @myxml XML


SET @myxml = '<proc name="DB_NAME.dbo.up_SP" thread_allowed="0" operation_id="12" queueCreatedDate="2015-01-12T07:14:00.640" referenceStub="02216CCE-FA4A-4C27-B130-A835F514E22D" callback-sql="UPDATE dbo.SOME_TABLE SET is_running = 0, last_modified_by = ''up_AsyncMessage'', last_modified_date = getdate() WHERE async_job_stub = ''022000CE-F00-0000-B000-030000022D''"/>'

SELECT xmlData.A.value('@name', 'VARCHAR(100)') AS DB_Name
FROM @myxml.nodes('proc') xmlData(a)