我有以下 SQL 查询:
SELECT SUM(OpenInterest) *(SELECT DISTINCT Future
FROM MTM
WHERE Expiry = [dbo].fx_GetRelativeExpiry(@date, 1, @Code)
and TradeDate = @date
and Code = @Code
and type = @Type
and Class = 'Foreign Exchange Future') / 1000
FROM MTM
WHERE Expiry = [dbo].fx_GetRelativeExpiry(@date, @N, @Code)
and TradeDate = @date
and Code = @Code
and type = @Type
and Class = 'Foreign Exchange Future'
我想在 Excel 中用作函数。问题是我在上述查询中多次重用参数,我不知道如何在 excel 中执行此操作而不创建新的(基本上是冗余的)参数。这是我的 VBA 代码:
Function GetTotalOI(TradeDate As Date, Code As String, OptionType As String, N As Integer) As Variant
'Create and open the connection
Dim oConnection As Connection
Set oConnection = New Connection
oConnection.ConnectionString = strConnectionStringYieldX
oConnection.Open
'Create the command object
Dim oCommand As Command
Set oCommand = New Command
oCommand.CommandType = adCmdText
Dim SQLString As String
SQLString = "SELECT SUM(OpenInterest) * (SELECT DISTINCT Future" _
& " FROM MTM" _
& " WHERE Expiry = [dbo].fx_GetRelativeExpiry(?, 1, ?)" _
& " and TradeDate = ?" _
& " and Code = ?" _
& " and type = ?" _
& " and Class = 'Foreign Exchange Future') / 1000" _
& " FROM MTM" _
& " WHERE Expiry = [dbo].fx_GetRelativeExpiry(?, ?, ?)" _
& " and TradeDate = ?" _
& " and Code = ?" _
& " and type = ?" _
& " and Class = 'Foreign Exchange Future'"
oCommand.CommandText = SQLString
oCommand.ActiveConnection = oConnection
oCommand.Parameters.Append oCommand.CreateParameter("Date1a", adDBTimeStamp, adParamInput)
oCommand.Parameters.Append oCommand.CreateParameter("Code1a", adVarChar, adParamInput, 50)
oCommand.Parameters.Append oCommand.CreateParameter("Date2a", adDBTimeStamp, adParamInput)
oCommand.Parameters.Append oCommand.CreateParameter("Code2a", adVarChar, adParamInput, 50)
oCommand.Parameters.Append oCommand.CreateParameter("Typea", adVarChar, adParamInput, 1)
oCommand.Parameters.Append oCommand.CreateParameter("Date1", adDBTimeStamp, adParamInput)
oCommand.Parameters.Append oCommand.CreateParameter("N", adInteger, adParamInput)
oCommand.Parameters.Append oCommand.CreateParameter("Code1", adVarChar, adParamInput, 50)
oCommand.Parameters.Append oCommand.CreateParameter("Date2", adDBTimeStamp, adParamInput)
oCommand.Parameters.Append oCommand.CreateParameter("Code2", adVarChar, adParamInput, 50)
oCommand.Parameters.Append oCommand.CreateParameter("Type", adVarChar, adParamInput, 1)
oCommand.Parameters.Item("Date1a").Value = TradeDate
oCommand.Parameters.Item("Code1a").Value = Code
oCommand.Parameters.Item("Date2a").Value = TradeDate
oCommand.Parameters.Item("Code2a").Value = Code
oCommand.Parameters.Item("Typea").Value = OptionType
oCommand.Parameters.Item("Date1").Value = TradeDate
oCommand.Parameters.Item("Code1").Value = Code
oCommand.Parameters.Item("N").Value = N
oCommand.Parameters.Item("Date2").Value = TradeDate
oCommand.Parameters.Item("Code2").Value = Code
oCommand.Parameters.Item("Type").Value = OptionType
Dim result As New ADODB.Recordset
Set result = oCommand.Execute
Dim resultA As Variant
GetTotalOI = WorksheetFunction.Transpose(result.GetRows)
oConnection.Close
End Function
该代码有效,但它是一团糟。我只需要4个参数。知道怎么做吗?有没有办法按名称指定参数,而不是像?
在查询字符串中那样?
我的连接字符串如下所示:
Const strConnectionStringYieldX As String = "Provider=SQLNCLI10.1;Data Source=xxxx;Initial Catalog=xxxx;Uid=xxxx;Pwd=xxxx;"
编辑
为了澄清这个问题,在 ADO 中你必须指定参数?
而不是类似的东西@ParamName
,这意味着如果你两次使用相同的参数,你必须在代码中重新创建参数。这是丑陋和不愉快的。所以在这个查询中我真的只使用了 4 个参数,因为我重复了很多次,所以我必须唯一地命名并创建 11 个参数。因此,如果您阅读 vba 代码,您会看到我有名为date1a
,date2a
和- 但这些参数都是相同的日期!我确信有一种本地方式可以在查询中使用某种命名参数,因此只需声明 4 个参数。date1
date2