我们当前的设计是返回一个Http请求文本并通过LoadXML解析它。通过循环遍历节点并一次插入一个所需的数据来读取XML数据。由于我对此脚本进行了修改,因此我想使用批量插入修改它。我怎样才能直接执行像this这样的批量插入,而不是拥有XML文件,我只有一个XML对象。
Set http_request = CreateObject("MSXML2.XMLHTTP")
http_request.open "POST", url, vbfalse
http_request.setRequestHeader "AUTHORIZATION", "Basic " & AuthInfo
http_request.setRequestHeader "Content-type", "application/soap+xml"
http_request.setRequestHeader "Content-length", Len(parameters)
http_request.setRequestHeader "Connection", "close"
http_request.send parameters
httpText = http_request.responseText
Set xmlDoc = CreateObject("MSXML2.DOMDocument")
xmlDoc.async = False
xmlDoc.LoadXML httpText
Set objDict = CreateObject("Scripting.Dictionary")
set Nodes=xmlDoc.selectNodes("//getRecordsResponse/getRecordsResult")
For Each node in Nodes
*****Insert TO SQL*****
Next
答案 0 :(得分:1)
我怀疑您必须在服务器上有一个文件才能使用SQL Server的XML批量插入。但是,如果要在单个语句中插入所有数据,可能需要考虑使用INSERT INTO ... VALUES
:
INSERT INTO DestinationTable
VALUES (
('Row1_Value1', 'Row1_Value2'),
('Row2_Value1', 'Row2_Value2')
)
在For Each
循环中构建值列表。像这样:
'probably needs to check each node if it is also an element
Dim length = xmlDoc.childNodes.Length
Dim rows(length), i, node
For i = 0 To length
Set node = xmlDoc.childNodes(i)
rows(i) = Array(node.getAttribute("Value1"), node.getAttribute("Value2"))
rows(i) = "('" & Join(rows(i), "','") & "')"
'It might be simpler to just build the string directly, instead of Array / Join
Next
Dim sql
sql = "INSERT INTO DestinationTable VALUES (" & Join(rows, ",") & ")"