SQL存储过程2008中的对象数组

时间:2014-05-23 18:48:10

标签: sql sql-server-2008 stored-procedures

我有一个java webservice,它在wsdl中以下列格式发送数据,并调用SQL存储过程在SQL数据库中插入记录

<EmpID>2001</EmpID>
<EmpLOCN>US</EmpLOCN>
<Salary>
<Salaryamt>2000</Salaryamt>
<Salarytype>EU</Salarytype>
<TAX>
<Tax1>20</Tax1>
<Tax2>2</Tax2>
<TAX>
</Salary>
<Salary>
<Salaryamt>4000</Salaryamt>
<Salarytype>EU</Salarytype>
<TAX>
<Tax1>10</Tax1>
<TAX>
</Salary>

从上面的输入我希望SQL读取它并插入3个具有相同员工ID的记录

我如何在SQL中读取此内容并插入记录

1 个答案:

答案 0 :(得分:0)

我没有在XML中看到第3行,但以下是如何在TSQL中读取该XML的内容:

    DECLARE @x XML

    SET @x = 
    '<EmpID>2001</EmpID>
    <EmpLOCN>US</EmpLOCN>
    <Salary>
        <Salaryamt>2000</Salaryamt>
        <Salarytype>EU</Salarytype>
        <TAX>
            <Tax1>20</Tax1>
            <Tax2>2</Tax2>
        </TAX>
    </Salary>
    <Salary>
        <Salaryamt>4000</Salaryamt>
        <Salarytype>EU</Salarytype>
        <TAX>
            <Tax1>10</Tax1>
        </TAX>
    </Salary>'


    SELECT
        Emp.Employee.value('(../EmpID)[1]', 'int') AS EmpID,
        Emp.Employee.value('(../EmpLOCN)[1]', 'varchar(50)') AS EmpLOCN,
        Emp.Employee.value('(Salaryamt)[1]', 'varchar(50)') AS SalaryAmt,
        Emp.Employee.value('(Salarytype)[1]', 'varchar(50)') AS SalaryType,
        Emp.Employee.value('(TAX/Tax1)[1]', 'varchar(50)') AS Tax1,
        Emp.Employee.value('(TAX/Tax2)[1]', 'varchar(50)') AS Tax2
    FROM
        @x.nodes('/Salary') AS Emp(Employee)