使用OpenXML的SQL插入导致空数据

时间:2019-01-29 16:16:35

标签: sql openxml

我试图将数据放入@table中,但是由于某些原因,我一直使用空值获取所有行。我得到空行,即使没有插入部分。一切似乎都应该起作用。我缺少什么明显的东西?

DECLARE       @AuthData_XML VARCHAR(max)

set @AuthData_XML = '
<AuthData>
<LOC>
   <Loc_ID>1</Loc_ID>
   <LocName>DD - Residential IP - Evaluation - Psychiatric</LocName>
   <Proccode></Proccode>
   <Proc_ID>7281</Proc_ID>
   <Selected>false</Selected>
   <DefaultUnits>0</DefaultUnits>
   <Func_Area>5319</Func_Area>
   <Serv_ID>0</Serv_ID>
   <Patient_ID>1222</Patient_ID>
</LOC>
<LOC>
       <Loc_ID>22</Loc_ID>
       <LocName>MH - Outpatient - Group Therapy</LocName>
       <Proccode>90853</Proccode>
       <Proc_ID>6697</Proc_ID>
       <Selected>false</Selected>
       <DefaultUnits>0</DefaultUnits>
       <Func_Area>0</Func_Area>
       <Serv_ID>0</Serv_ID>
       <Patient_ID>1222</Patient_ID>
</LOC>
<LOC>
       <Loc_ID>26</Loc_ID>
       <LocName>MH - Outpatient - Individual Therapy - 30min</LocName>
       <Proccode>90804</Proccode>
       <Proc_ID>7381</Proc_ID>
       <Selected>false</Selected>
       <DefaultUnits>0</DefaultUnits>
       <Func_Area>0</Func_Area>
       <Serv_ID>0</Serv_ID>
       <Patient_ID>1222</Patient_ID>
</LOC>
<LOC>
       <Loc_ID>94</Loc_ID>
       <LocName>SA - Inpatient - Detoxification</LocName>
       <Proccode>H0009</Proccode>
       <Proc_ID>7399</Proc_ID>
       <Selected>false</Selected>
       <DefaultUnits>0</DefaultUnits>
       <Func_Area>0</Func_Area>
       <Serv_ID>0</Serv_ID>
       <Patient_ID>1222</Patient_ID>
</LOC>
<LOC>
       <Loc_ID>47</Loc_ID>
       <LocName>MH - Medical-Surgical - Consult - Follow-up</LocName>
       <Proccode>99232</Proccode>
       <Proc_ID>7283</Proc_ID>
       <Selected>false</Selected>
       <DefaultUnits>0</DefaultUnits>
       <Func_Area>0</Func_Area>
       <Serv_ID>0</Serv_ID>
       <Patient_ID>1222</Patient_ID>
</LOC>
</AuthData>'

DECLARE @iDoc int
DECLARE @ServiceRequestData TABLE (Loc_ID int, LocName varchar(200), Proccode varchar(10), Proc_ID int, Selected int, DefaultUnits int, Func_Area int, Serv_ID int, Patient_ID int)

EXEC sp_xml_preparedocument @iDoc OUTPUT, @AuthData_XML
       INSERT INTO @ServiceRequestData
              SELECT        Loc_ID,
                     LocName,
                     Proccode,
                     Proc_ID,
                     Selected,
                     DefaultUnits,
                     Func_Area,
                     Serv_ID,
                     Patient_ID
              FROM Openxml(@iDoc, '/AuthData/LOC', 9) WITH (Loc_ID int, LocName varchar(200), Proccode varchar(10), Proc_ID int, Selected int, DefaultUnits int, Func_Area int, Serv_ID int, Patient_ID int)

结果,我的数据没有进入表中。到目前为止,我看到的所有示例都与我的示例非常接近。

1 个答案:

答案 0 :(得分:0)

最终从openxml更改为使用xquery。我将var类型更改为XML,并能够使用以下查询来获取预期的数据。

INSERT INTO @ServiceRequestData
SELECT  IdNode.value('(Loc_ID)[1]','int') AS Loc_ID,
    IdNode.value('(LocName)[1]','varchar(200)') AS LocName, 
    IdNode.value('(Proccode)[1]','varchar(10)') AS Proccode, 
    IdNode.value('(Proc_ID)[1]','int') AS Proc_ID, 
    IdNode.value('(Selected)[1]','bit') AS Selected, 
    IdNode.value('(DefaultUnits)[1]','int') AS DefaultUnits, 
    IdNode.value('(Func_Area)[1]','int') AS Func_Area, 
    IdNode.value('(Serv_ID)[1]','int') AS Serv_ID, 
    IdNode.value('(Patient_ID)[1]','int') AS Patient_ID
FROM @AuthData_XML.nodes('/AuthData/LOC') AS idTbl(IdNode)