TSQL使用XML插入语句

时间:2014-05-29 21:33:53

标签: sql xml tsql

我有一个从我的工具接收一些xml的存储过程。

 BEGIN
            --Insert Plates
            INSERT INTO licensePlates (carColor, carModel, licensePlate, empID, dateAdded)
            SELECT ParamValues.x2.value('color[1]', 'VARCHAR(100)'),
                   ParamValues.x2.value('model[1]', 'VARCHAR(100)'),
                   ParamValues.x2.value('licensePlate[1]', 'VARCHAR(100)'),
                   @empID,
                   GETDATE()
            FROM   @xmlData.nodes('/vehicles/vehicle') AS ParamValues(x2);

            --Insert Carpool
            INSERT INTO licensePlatesCarpool (licensePlate, empID, dateUpdated)
            SELECT ParamValues.x2.value('licensePlate[1]', 'VARCHAR(60)'),
                   ParamValues.x2.value('empID[1]', 'VARCHAR(60)'),
                   GETDATE()
            FROM   @xmlData.nodes('/carPool/employee') AS ParamValues(x2);

        END

正在发送的XML是:

            <vehicles>
               <vehicle>
                  <licensePlate>fdg</licensePlate>
                  <model>fg</model>
                  <color>dfg</color>
                  <carPool>
                     <employee>
                        <licensePlate>fdg</licensePlate>
                        <empID>456</empID>
                     </employee>
                     <employee>
                        <licensePlate>fdg</licensePlate>
                        <empID>123</empID>
                     </employee>
                  </carPool>
               </vehicle>
            </vehicles>

由于某种原因,只有top insert语句实际上是在触发;另一个不插入任何数据,即使该表似乎设置正确。

enter image description here

任何人都可以看到我忽略的任何东西会导致第二次插入不起作用吗?

1 个答案:

答案 0 :(得分:0)

我想通了..在XML字符串中已经不够了:))

我改变了

 FROM   @xmlData.nodes('/carPool/employee') AS ParamValues(x2);

 FROM   @xmlData.nodes('vehicles/vehicle/carPool/employee') AS ParamValues(x2);