我希望使用XML参数字符串通过一次调用填充SQL表。
构建XML的代码如下:
循环列表
' update string for reserved character (ampersand)
Dim descStr As String = allMarkets(0).result(j).event.name.Replace("&", "&&")
(NOTE : the ampersand reference text shown is not displaying correctly in this browser, where I put the correct string reference it is displaying incorrectly)
strBuffer.Append(" <ID>""" & allMarkets(0).result(j).event.id.ToString & """ </ID><DESC>""" & descStr & """</DESC>")
Next
我的存储过程:
ALTER PROC [dbo].[PriceDataXMLEVENTInsert1]
(@ValidXMLInput XML)
AS
BEGIN
INSERT INTO PRICEDATA (EventID, EventDesc)
SELECT
x.v.value('@ID', 'INT'),
x.v.value('@DESC', 'nvarchar(50)')
FROM
@ValidXMLInput.nodes('//ROOT/ROW') x(v)
传递给函数的XML字符串包含有效数据并且代码运行正常...但是当表PRICEDATA
中应该有几百条记录时 - 没有.....有什么问题以上......?
提前感谢您提供的任何帮助......
答案 0 :(得分:1)
回答你的原始问题:
有什么问题SELECT x.v.value('@ID','INT'), x.v.value('@DESC','nvarchar(50)')
FROM @ValidXMLInput.nodes('//ROOT/ROW') x(v)
您的示例数据未显示ROW
元素,也没有属性@ID
或@DESC
。这将不会返回......
DECLARE @x XML=
'<ROOT>
<ID>"27748379" </ID>
<DESC>"Garbarnia v KS Spartakus Daleszyce"</DESC>
<ID>"27748239" </ID>
<DESC>"H Peta8h Tikva Youth v H Rishon Lezion Youth"</DESC>
</ROOT>';
有几个缺陷:
<element><ID>gfjha</ID><DESC>sfasdf</DESC></element>
将它们绑定在一起(在一个节点内),或者 - 我的选择 - 使用<element ID="fasdf" DESC="fsdfdaf">
<element attr="value">
)中,则需要使用引号。但在元素中他们是令人不安的!<
&gt;与>
和&amp;使用&
(没有加倍&amp;&amp; ,如您的示例!)无论如何,这是一种从XML中获取数据表以便轻松插入的方法。但你真的应该改变你的结构!
WITH IDNodes AS
(
SELECT ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS Nr
,B.value('.','varchar(max)') AS ID
FROM @x.nodes('/ROOT/ID') AS A(B)
)
, DESCNodes AS
(
SELECT ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS Nr
,B.value('.','varchar(max)') AS [DESC]
FROM @x.nodes('/ROOT/DESC') AS A(B)
)
SELECT IDNodes.ID,DESCNodes.[DESC]
FROM IDNodes
INNER JOIN DESCNodes ON IDNodes.Nr=DESCNodes.Nr
结果(你应该让"
离开......)
ID DESC
"27748379" "Garbarnia v KS Spartakus Daleszyce"
"27748239" "H Peta8h Tikva Youth v H Rishon Lezion Youth"
DECLARE @ValidXMLInput XML=
'<ROOT>
<element ID="27748379" DESC="Garbarnia v KS Spartakus Daleszyce"/>
<element ID="27748239" DESC="H Peta8h Tikva Youth v H Rishon Lezion Youth"/>
</ROOT>';
SELECT A.B.value('@ID','int') AS ID
,A.B.value('@DESC','varchar(max)') AS [DESC]
FROM @ValidXMLInput.nodes('/ROOT/element') AS A(B)