使用相同的参数两次

时间:2013-07-11 15:27:37

标签: sql-server excel-vba adodb query-parameters vba

我有以下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代码,您会看到我的参数名为date1adate2adate1date2 - 但这些都是相同的日期!我确信在查询中有一种本地方式可以使用某种命名参数,因此只需要声明4个参数。

1 个答案:

答案 0 :(得分:0)

我确信有一种正确的方法可以做到这一点但最后我只是在DB上创建了一个UDF,它允许我只使用4个参数以及某些T-SQL命令和程序,否则这些命令和程序将无效。但如果有人知道合适的替代方案,请发布!