好的,我现在有了存储过程的代码。
ALTER PROC [dbo].[Readxml]
@xmlparam XML
AS
BEGIN
SET NOCOUNT ON
DECLARE @CustomerXml AS XML
SET @CustomerXml = @xmlparam
INSERT INTO Custtable.dbo.SPCustomer
(
CustomerId,
CustomerName
)
SELECT
Customer.attribute.value('CustomerId[1]', 'nvarchar(255)') AS CustomerId,
Customer.attribute.value('Name[1]', 'nvarchar(255)') AS CustomerName
FROM
@xmlparam.nodes('Customers/Customer') AS Customer(attribute)
END
我的XML看起来像这样(简化)。
<Customers>
<Customer CustomerId="94" Name="name1" />
<Customer CustomerId="95" Name="name2" />
<Customer CustomerId="96" Name="name3" />
</Customers>
现在使用我的代码我无法获取属性值,因为据我所知,我正在尝试将<Customer>
标记中的元素称为CustomerId
和Name
,不存在。
当从表中选择所有行时,在完成该过程后,我得到所有行但是具有NULL值。
我的问题是,如何从XML中获取属性?
提前致谢!
答案 0 :(得分:3)
你需要这个:
SELECT
Customer.attribute.value('@CustomerId', 'nvarchar(255)') AS CustomerId,
Customer.attribute.value('@Name', 'nvarchar(255)') AS CustomerName
FROM
@xmlparam.nodes('Customers/Customer') AS Customer(attribute)
要获取该属性,请使用前导@
答案 1 :(得分:1)
ALTER PROC [dbo].[Readxml]
@xmlparam XML
AS
BEGIN
SET NOCOUNT ON
DECLARE @CustomerXml AS XML
SET @CustomerXml = @xmlparam
INSERT INTO Custtable.dbo.SPCustomer
(
CustomerId,
CustomerName
)
SELECT Customer.attribute.value('@CustomerId', 'BIGINT') AS CustomerId,
Customer.attribute.value('@Name', 'nvarchar(255)') AS CustomerName
FROM @xmlparam.nodes('Customers/Customer') AS Customer(attribute)
END