简体   繁体   English

VBA ADO 将数组参数传递给存储过程

[英]VBA ADO pass array parameter to stored procedure

I have an array with 3 column and 100K of rows in VBA MS Excel file.我在 VBA MS Excel 文件中有一个 3 列和 100K 行的数组。

I want to pass this array to SQL Server Stored Procedure and in the Stored Procedure to convert it to Tmp-table and do my "things.."我想将此数组传递给 SQL 服务器存储过程并在存储过程中将其转换为 Tmp-table 并执行我的“事情..”

How do I pass an array to Stored Procedure?如何将数组传递给存储过程?

This is an example of my VBA array:这是我的 VBA 阵列的示例:

Col1 | Col2 | Col3 
1      AAA     XXX
2      BBB     YYY
3      CCC     ZZZ

The VBA code: VBA 代码:

......

Set Cmd = CreateObject("ADODB.Command")

Cmd.CommandType = 4 
Cmd.NamedParameters = True
Cmd.ActiveConnection = DbConn
Cmd.CommandText = "My_SP_WithArrayParam"

''I dont know what parameters to use here:
Cmd.Parameters.Append Cmd.CreateParameter("@Arr", ??? , ?? , ??? , My_100K_Arr)

Cmd.Execute

And in the SQL side:而在 SQL 方面:

Create or alter proc My_SP_WithArrayParam(
                                            @Arr ???
                                            )
as 
begin
    create table #Tbl_From_Excel (
                                ID int
                                ,COL1 nvarchar(60)
                                ,COL2 nvarchar(60)
                                )

    ---Need to insert the array to the tmp table, and I dont know the syntax

end 

An array of objects can be passed as aa structured parameter value, such as a table-valued parameter, XML, or json (SQL 2016 and later).对象数组可以作为结构化参数值传递,例如表值参数 XML 或 json(SQL 2016 及更高版本)。 Although newer SQL Server drivers support table-valued parameters, ADODB (aka ADO classic) does not.虽然较新的 SQL 服务器驱动程序支持表值参数,但 ADODB(又名 ADO 经典)不支持。 I suggest a json string over XML since it has a smaller payload and is parsed efficiently by SQL Server.我建议在 XML 上使用 json 字符串,因为它的有效负载较小,并且可以由 SQL 服务器有效解析。

Below is VBA example code.下面是 VBA 示例代码。 The SQL call only took a few seconds but building the json string with traditional string concatenation took a couple of minutes on my workstation. SQL 调用只用了几秒钟,但在我的工作站上使用传统的字符串连接构建 json 字符串需要几分钟。 Performant string building in VBA requires much attention to detail but it seems there are techniques to improve it considerably if you want to go there. VBA 中的高性能字符串构建需要非常注意细节,但如果您想在那里进行 go,似乎有一些技术可以大大改善它

Stored procedure:存储过程:

CREATE OR ALTER PROC My_SP_WithArrayParam
    @Arr nvarchar(MAX)
AS
SET NOCOUNT ON;
    SELECT Col1, Col2, Col3
    INTO #Tbl_From_Excel 
FROM OPENJSON(@arr)
WITH (
     Col1 int
    ,Col2 varchar(100)
    ,Col3 varchar(100)
);
GO

ArrayEntry class: ArrayEntry class:

Option Explicit

Public Col1 As Long
Public Col2 As String
Public Col3 As String

Public Function getJson()
    Dim json As String
    ` note if double quotes may exist in values, they need to be escaped with a backslash
    ' TODO: string building optimization
    getJson = "{""Col1"":""" & CStr(Col1) & """,""Col2"":""" & Col2 & """,""Col3"":""" & Col3 & """}"
End Function

Sample usage code:示例使用代码:

' load 100000 array entries
Dim arrayEntries(100000) As ArrayEntry
Dim i As Long
For i = 0 To 99999
    Set entry = New ArrayEntry
    Set arrayEntries(i) = entry
    entry.Col1 = i
    entry.Col2 = "AAA"
    entry.Col3 = "XXX"
Next i

' build json from array (TODO: string building optimization)
Dim json As String
json = "["
For i = 0 To 99999
    Set entry = arrayEntries(i)
    If i > 0 Then json = json & ","
    json = json & entry.getJson()
Next
json = json & "]"

' call proc with json parameter
con.Open "Provider=MSOLEDBSQL;Data Source=.;Initial Catalog=YourDatabase;Integrated Security=SSPI;"
cmd.ActiveConnection = con
cmd.CommandText = "My_SP_WithArrayParam"
cmd.CommandType = adCmdStoredProc
cmd.Parameters.Append cmd.CreateParameter("@json", adVarChar, adParamInput, -1, json)
cmd.Execute
con.Close
Set cmd = Nothing
Set con = Nothing

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用ADO和C ++将null传递给存储过程输入参数 - How to pass null to stored procedure input parameter with ADO and C++ ADO命令参数未传递到存储过程或存储过程'忽略'参数 - ADO Command Parameter Not Passing to Stored Procedure or Stored Procedure 'Ignoring' Parameter 通过ADO检索存储过程输出参数 - Retrieving stored procedure output parameter via ADO 如何通过ADO VBA提供更新存储过程的十进制输入参数(可以为null) - How to feed an update stored procedure's decimal input parameter (could be null) by ADO VBA 如何将列的字符串数组作为参数传递给SQL Server存储过程 - How to pass string array of Columns as parameter to SQL Server Stored procedure 将变量作为存储过程的参数传递 - Pass variable as parameter on a Stored Procedure 将循环作为参数传递给存储过程 - pass a loop as parameter to a stored procedure VBA Excel ADO sp_addextendedproperty存储过程执行 - VBA Excel ADO sp_addextendedproperty stored procedure execution 使用函数返回值参数ADO.NET调用存储过程 - Call a stored procedure with a function returned value parameter ADO.NET 如何通过ADO.NET实体模型从C#将字符串数组传递到SQL Server中的存储过程中 - How to pass a string array into a stored procedure in SQL Server from c# via ADO.NET entity model
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM