在VBA中导出存储过程的参数

我试过search很多地方,不能find我要找的东西。

我想在vba中编写一个子例程,它会告诉我存储在SQL Server上的存储过程的参数。

我知道如何使用excel vba中的参数执行存储过程。 我写了一个存储过程,它存储过程名称并返回参数。 所以我可以使用这个。 但是我想也许有一个我不了解的更好的方法。 我发现VB的SQLCommandBuilder类将是完美的,但我需要它在VBA中。 这是在VBA中可用,我只是不知道在哪里激活它?

谢谢

**其他信息:在下面的有用的评论后,我正在接近我的目标实现。 我想能够通过任何存储过程到我的子程序,它将能够找出它需要多less个参数,他们会是什么

这是我的代码到目前为止

Private Sub execStoredProcedureWithParameters(strServer As String, strDatabase As String, strSchema As String, strUSPName As String) 'Declare variables Dim cmd As ADODB.Command Dim conn As ADODB.Connection Dim prm As ADODB.Parameter Dim rs As ADODB.Recordset Dim intParamCount As Integer 'Open database connection Set conn = New ADODB.Connection conn.ConnectionString = "Provider=sqloledb;Data Source=" + strServer + ";Initial Catalog=" + strDatabase + ";Integrated Security=SSPI;" conn.CommandTimeout = 0 'Here's where the connection is opened. conn.Open 'This can be very handy to help debug! 'Debug.Print conn.ConnectionString Set cmd = New ADODB.Command With cmd .CommandText = strSchema + "." + strUSPName .CommandType = adCmdStoredProc .ActiveConnection = conn .Parameters.Refresh For intParamCount = 0 To .Parameters.Count - 1 Debug.Print .Parameters(intParamCount).Name, .Parameters(intParamCount).Type, .Parameters(intParamCounti).Size, .Parameters(intParamCount).Attributes, .Parameters(intParamCount).NumericScale ' Set prm = cmd.CreateParameter(.Parameters(i).Name, adVarChar, adParamInput, 255) ' cmd.Parameters.Append prm ' cmd.Parameters(.Parameters(i).Name).Value = "DBName" Next End With Set rs = New ADODB.Recordset 'Execute the Stored Procedure Set rs = cmd.Execute 'Populate the sheet with the data from the recordset Sheet1.Range("RecordSet").CopyFromRecordset rs 'Cleanup rs.Close Set rs = Nothing conn.Close Set conn = Nothing End Sub 

关于参数。 有没有办法将DataTypeEnum从该值转换为常量。 所以这个types目前正在通过202作为第一个参数,我将根据这个表设置为adVarWChar

https://docs.microsoft.com/en-us/sql/ado/reference/ado-api/datatypeenum

你可以用ADODB做到这一点,添加一个对Microsoft ActiveX Data Objects的引用,然后你可以:

 With New ADODB.Command Set .ActiveConnection = myAdoDbConnection .CommandText = "[dbo].[usp_XXX]" .CommandType = adCmdStoredProc .Parameters.Refresh For i = 0 To .Parameters.Count - 1 Debug.Print .Parameters(i).Name, .Parameters(i).Type, .Parameters(i).Direction Next End With 

应该有一个必要的要求,因为它需要往返服务器。