SQLServer中將XML值轉成表格內容
阿新 • • 發佈:2021-07-20
原始XML值
<usertasks> <usertask usertaskid="165204" userid="103536" username="丁**" suggection="本研究是根據《藥品不良反應報告和監測管理辦法》要求開展的一項上市後重點監測研究" datetime="9/7/2020 12:06:24 PM" type="cra"/> <usertask usertaskid="165205" type="departdirector" userid="100232" username="宋**" suggection="" resultstatus="APPROVE" issignoff="1" datetime="9/7/2020 4:19:52 PM"/> <usertask usertaskid="165254" type="pi" userid="100033" username="王**" suggection="" resultstatus="APPROVE" issignoff="1" datetime="9/9/2020 5:22:51 PM"/> <usertask usertaskid="165593" type="departdirector" userid="100072" username="吳**" suggection="" resultstatus="APPROVE" issignoff="1" datetime="9/11/2020 8:29:04 AM"/> <usertask usertaskid="165794" type="secretary" userid="100619" username="李**" suggection="1、請完善系統研究人員名單; 2、請上傳藥物批件。 3、請上傳藥品說明書; 4、請上傳保險保單。" resultstatus="REJECT" datetime="11/2/2020 2:50:02 PM"/> <usertask usertaskid="171681" userid="103536" username="丁**" suggection="本研究是根據《藥品不良反應報告和監測管理辦法》要求開展的一項上市後重點監測研究" datetime="11/9/2020 3:26:43 PM" type="crc"/> <usertask usertaskid="173216" type="secretary" userid="100619" username="李**" suggection="" resultstatus="APPROVE" datetime="11/11/2020 5:03:53 PM"/> <usertask usertaskid="173878" type="orgdirector" userid="100001" username="劉**" suggection="同意" resultstatus="APPROVE" issignoff="1" datetime="11/12/2020 2:58:29 PM"/> </usertasks>
SQL處理
DECLARE @usertask XML; SET @usertask = ' <usertasks> <usertask usertaskid="165204" userid="103536" username="丁**" suggection="本研究是根據《藥品不良反應報告和監測管理辦法》要求開展的一項上市後重點監測研究" datetime="9/7/2020 12:06:24 PM" type="cra"/> <usertask usertaskid="165205" type="departdirector" userid="100232" username="宋**" suggection="" resultstatus="APPROVE" issignoff="1" datetime="9/7/2020 4:19:52 PM"/> <usertask usertaskid="165254" type="pi" userid="100033" username="王**" suggection="" resultstatus="APPROVE" issignoff="1" datetime="9/9/2020 5:22:51 PM"/> <usertask usertaskid="165593" type="departdirector" userid="100072" username="吳**" suggection="" resultstatus="APPROVE" issignoff="1" datetime="9/11/2020 8:29:04 AM"/> <usertask usertaskid="165794" type="secretary" userid="100619" username="李**" suggection="1、請完善系統研究人員名單; 2、請上傳藥物批件。 3、請上傳藥品說明書; 4、請上傳保險保單。" resultstatus="REJECT" datetime="11/2/2020 2:50:02 PM"/> <usertask usertaskid="171681" userid="103536" username="丁**" suggection="本研究是根據《藥品不良反應報告和監測管理辦法》要求開展的一項上市後重點監測研究" datetime="11/9/2020 3:26:43 PM" type="crc"/> <usertask usertaskid="173216" type="secretary" userid="100619" username="李**" suggection="" resultstatus="APPROVE" datetime="11/11/2020 5:03:53 PM"/> <usertask usertaskid="173878" type="orgdirector" userid="100001" username="劉**" suggection="同意" resultstatus="APPROVE" issignoff="1" datetime="11/12/2020 2:58:29 PM"/> </usertasks>'; SELECT T.C.value('@usertaskid', 'int') AS usertaskid , T.C.value('@userid', 'int') AS userid , T.C.value('@username', 'VARCHAR(160)') AS username , T.C.value('@resultstatus', 'VARCHAR(160)') AS resultstatus , T.C.value('@suggection', 'VARCHAR(320)') AS [suggection] , T.C.value('@datetime', 'datetime') AS [createdate] FROM @usertask.nodes('/usertasks/usertask') AS T ( C );
通過SQL語句轉成表格內容
usertaskid userid username resultstatus suggection createdate 165204 103536 丁** NULL 本研究是根據《藥品不良反應報告和監測管理辦法》要求開展的一項上市後重點監測研究 2020-09-07 12:06:24.000 165205 100232 宋** APPROVE 2020-09-07 16:19:52.000 165254 100033 王** APPROVE 2020-09-09 17:22:51.000 165593 100072 吳** APPROVE 2020-09-11 08:29:04.000 165794 100619 李** REJECT 1、請完善系統研究人員名單; 2、請上傳藥物批件。 3、請上傳藥品說明書; 4、請上傳保險保單。 2020-11-02 14:50:02.000 171681 103536 丁** NULL 本研究是根據《藥品不良反應報告和監測管理辦法》要求開展的一項上市後重點監測研究 2020-11-09 15:26:43.000 173216 100619 李** APPROVE 2020-11-11 17:03:53.000 173878 100001 劉** APPROVE 同意 2020-11-12 14:58:29.000
實際使用
-- 查詢最近一次流程的任務處理概況
SELECT T.C.value('@usertaskid', 'int') AS usertaskid ,
T.C.value('@userid', 'int') AS userid ,
T.C.value('@username', 'VARCHAR(160)') AS username ,
T.C.value('@appresult', 'VARCHAR(160)') AS appresult ,
T.C.value('@suggection', 'VARCHAR(320)') AS [suggection] ,
T.C.value('@datetime', 'datetime') AS [createdate]
FROM ( SELECT TOP 1
CONVERT(XML, wis.Value) AS usertask
FROM WorkflowInstanceSettings AS wis
WHERE wis.Name = 'result'
AND WorkflowInstanceID = ( SELECT TOP ( 1 )
WorkflowInstanceID
FROM dbo.UserTasks
ORDER BY CreatedDate DESC
)
) AS k
CROSS APPLY k.usertask.nodes('/usertasks/usertask') AS T ( C )
ORDER BY createdate;