2013-07-11 93 views
0

我有以下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個參數。

+0

那麼你的問題是什麼? – 2013-07-11 15:46:10

+0

@mehow事實上,我必須聲明** 11 **參數,而不是4.在C#中,例如,我可以只使用它。4.它也很慢。 – Dan

+0

如果你不擔心注射,然後打破字符串,並插入4個參數,就好像你正在用變量替換字符串 – 2013-07-11 15:54:28

回答

0

我確定有一個正確的方法來做到這一點,但最終我只是在數據庫上做了一個UDF,它允許我只使用4個參數以及某些T-SQL命令和過程,否則不會工作。但如果有人知道一個合適的替代品,請發佈!

相關問題