2016-09-20 72 views
0
Sub Run() 

    Call ConnectDB 

    Dim Cmd As ADODB.Command 
    Dim rcs As ADODB.Recordset 
    Dim SQL As String 
    Dim res() As String 

    Set Cmd = New ADODB.Command 
    Set Cmd.ActiveConnection = con 

    SQL = "select tl.id, al.price_crossing, al.price_exchange_fees, tl.charges_execution, tl.charges_mariana, tl.charges_exchange, tl.trade_date, un.value, tl.nb_crossing from mfb.trade_leg tl" & _ 
    "inner join mfb.trade t on t.id = tl.id_trade" & _ 
    "inner join mfb.instrument i on t.id_instrument = i.id" & _ 
    "inner join mfb.instrument_type it on it.id = i.id_instrument_type" & _ 
    "inner join mfb.options o on o.id_instrument = i.id" & _ 
    "inner join mfbref.mfb.underlying un on un.id = o.id_underlying" & _ 
    "inner join mfb.allocation_leg al on al.id_trade_leg = tl.id" & _ 
    "where tl.trade_date > '20160101' and t.state = 3" 

    Cmd.CommandText = SQL 
    Set rcs = Cmd.Execute() 

每當我執行此代碼時,出現調試錯誤,提示'tl'附近的語法不正確。我嘗試以不同的方式執行SQL,但錯誤不會改變如何在VBA Excel中執行SQL查詢

+0

也許你錯過了之前的空格內連接語句 – 2016-09-20 12:57:13

+1

你沒有線之間的空間。 – czuroski

+0

我不是關於spaes,是否需要在'tl'之前放置'AS'。 – Nebi

回答

2
SQL = "select tl.id, al.price_crossing, al.price_exchange_fees, tl.charges_execution, tl.charges_mariana, tl.charges_exchange, tl.trade_date, un.value, tl.nb_crossing from mfb.trade_leg AS tl " & _ 
"inner join mfb.trade t on t.id = tl.id_trade " & _ 
"inner join mfb.instrument i on t.id_instrument = i.id " & _ 
"inner join mfb.instrument_type it on it.id = i.id_instrument_type " & _ 
"inner join mfb.options o on o.id_instrument = i.id " & _ 
"inner join mfbref.mfb.underlying un on un.id = o.id_underlying " & _ 
"inner join mfb.allocation_leg al on al.id_trade_leg = tl.id " & _ 
"where tl.trade_date > '20160101' and t.state = 3 " 
+0

你很快就會大聲笑 –

+0

好,但你得到了「回答」標記 – Nebi

0

將別名分配給SQL對象時應使用「as」。

嘗試改變

 ...from mfb.trade_leg tl... 
    ...join mfb.trade t... 
    ...join mfb.options o ... 

 ...from mfb.trade_leg as tl... 
    ...join mfb.trade as t... 
    ...join mfb.options as o ... 
+0

1)這不是語法錯誤的根本原因。 2)它實際上是一個[範圍可變](https://books.google.co.uk/books?id=Jx5UCwAAQBAJ&pg=PT162&lpg=PT162&dq=database+range+variable&source=bl&ots=-DhuMdazmr&sig=cVB4Sn4N2_igTwbLVwG7PFdXo9U&hl=en&sa=X&ved=0ahUKEwi56KfZlZ7PAhUCkZQKHWexBBMQ6AEINzAE #v = onepage&q =數據庫%20range%20variable&F = FALSE)。 3)基於意見的例如我傾向於省略'AS',因爲它鼓勵錯誤的想法,即'table as x'錯誤地暗示'x'是一個別名。 – onedaywhen

1
SQL = "select tl.id, al.price_crossing, al.price_exchange_fees, tl.charges_execution, tl.charges_mariana, tl.charges_exchange, tl.trade_date, un.value, tl.nb_crossing from mfb.trade_leg tl " & _ 
"inner join mfb.trade t on t.id = tl.id_trade " & _ 
"inner join mfb.instrument i on t.id_instrument = i.id " & _ 
"inner join mfb.instrument_type it on it.id = i.id_instrument_type " & _ 
"inner join mfb.options o on o.id_instrument = i.id " & _ 
"inner join mfbref.mfb.underlying un on un.id = o.id_underlying " & _ 
"inner join mfb.allocation_leg al on al.id_trade_leg = tl.id " & _ 
"where tl.trade_date > '20160101' and t.state = 3;" 

你冷落噸的空間

+1

謝謝。這幫了我很多 –

+0

給了一個人一個大拇指!並且不要忘記標記某人的答案爲「回答」 –

+0

也不值得,因爲間距是嵌入式SQL的問題,它很好地開發模式。我個人會在每一行的末尾留下我的空間。建立一種行爲模式將有助於您在未來發現問題。 @PaulKuriakides –