1

开放式XML:

DECLARE @idoc int
DECLARE @doc varchar(1000)
SET @doc =
'<ROOT>
        <Employee EmployeeID = "1"  EmpStatus = "Full Time"/>
        <Employee EmployeeID = "2"  EmpStatus ="Part Time" />
        </ROOT>'
    EXEC sp_xml_preparedocument @idoc OUTPUT, @doc
SELECT    *
FROM       OPENXML (@idoc, '/ROOT/Employee',1)
            WITH (EmployeeID  varchar(10),
                  EmpStatus varchar(20))

结果:

EmployeeID    EmpStatus
    1         Full Time
    2         Part Time

表查询:

    SELECT hr.EmployeeID, hr.Title, c.FirstName,c.LastName 
    FROM HumanResources.Employee hr WITH (NOLOCK)
    INNER JOIN ContactInfo c WITH (NOLOCK)
    ON hr.ContactID = c.ContactID
    Where hr. EmployeeID IN ( 1, 2)

结果:

EmployeeID  Title         FirstName  LastName
    1   Engineering   Mike       Brown
    2   Programmer    Yves       Anthony

如何将 OpenXML 数据加入到我的内部连接查询中EmployeeID

4

2 回答 2

1

您是否坚持使用 OpenXML?它是旧的,它是遗留的——使用本机 XQuery 函数通常要容易得多

尝试这样的事情:

DECLARE @Employees TABLE (EmployeeID INT, Title VARCHAR(20), FirstName VARCHAR(20),LastName VARCHAR(20))

INSERT INTO @Employees VALUES(1, 'Engineering', 'Mike', 'Brown')
INSERT INTO @Employees VALUES(2, 'Programmer', 'Yves', 'Anthony')

DECLARE @doc XML 
SET @doc = '<ROOT>
              <Employee EmployeeID = "1"  EmpStatus = "Full Time"/>
              <Employee EmployeeID = "2"  EmpStatus ="Part Time" />
            </ROOT>'

;WITH XmlCTE AS
(
    SELECT
        EmpID = Empl.value('@EmployeeID', 'int'),
        EmpStatus = Empl.value('@EmpStatus', 'varchar(10)')
    FROM @doc.nodes('/ROOT/Employee') AS Tbl(Empl)
)
SELECT 
    e.*, x.EmpStatus 
FROM 
    @Employees e
INNER JOIN  
    xmlcte x ON e.EmployeeID = x.EmpID

这给了我一个输出:

在此处输入图像描述

于 2012-10-10T19:49:41.820 回答
0

尽管另一个答案暗示了一定程度的弃用,openxml因为我找不到任何证据,也没有费心去解决如何去做......你可以同时使用openxmljoin

在你的情况下,它会是这样的:

select
    x.*,
    hr.EmployeeID, hr.Title, c.FirstName, c.LastName 
from
    openxml (@IDoc, '/ROOT/Employee',1)
    with (
        EmployeeID  varchar(10),
        EmpStatus   varchar(20)
    ) as x
    inner join HumanResources.Employee hr with (nolock) on
        x.EmployeeID = hr.EmployeeID
    inner join Contactinfo c with (nolock)
        on hr.ContactID = c.ContactID;

h/t:http ://www.informit.com/articles/article.aspx?p=26499&seqNum=3

于 2016-11-01T11:02:44.297 回答