2016-01-25 48 views
-2

我嘗試用白色的大型SQL查詢是這樣的:嘗試使用大型SQL查詢

insert into R7810TEST 
    (select 1111111 , 111111 , 1111111 ,'ORG' ,R401.ZIHUI_BAAL_POLISA ,8990 ,'ID' ,R401.ZIHUI_M_RASHI , R401.M_POLISA ,R440.M_SOCHEN , 201507 ,'ORG' , R401.ZIHUI_BAAL_POLISA ,'RTP' , 'CUR' , '1', '*' ,0 ,10000.00 ,0.00 , 0.00 , 0.00, 0.00, 0.00 , 0.00,0.00, 0.00 , r430.hf_oved_tagmulim_45 ,r430.hf_mavid_tagmulim , r430.hf_mavid_pizuim_mukar , 0.00, r430.hf_mavid_chelef, r430.hf_oved_shonot, r430.hf_mavid_shonot , r430.hf_oved_tagmulim_47 ,'MNG', 0.00 , 0.00 ,0.00 ,0.00 , '*' , to_date ('20150701', 'yyyymmdd'), R401.ZIHUI_BAAL_POLISA, r400.sug_polisa, to_date ('20150701', 'yyyymmdd'),'CHI', '12' , 0, 0 ,'0', to_date ('19000101', 'yyyymmdd') 
from r400 R400 ,r401 R401 ,r430 r430 ,r440 r440 
where r400.m_polisa = r401.m_polisa 
and r401.m_polisa = r430.m_polisa 
and r430.m_polisa= r440.m_polisa 
and r401.tr_rishum in (select max(aa.tr_rishum) from r401 aa where aa.m_polisa = r401.m_polisa) 
and r430.tr_rishum in (select max(aa.tr_rishum) from r430 aa where aa.m_polisa = r430.m_polisa) 
and r440.tr_rishum in (select max(aa.tr_rishum) from r440 aa where aa.m_polisa = r440.m_polisa) 
and r401.status_rashi = 10 
--and r401.status_rashi in (30,35) 
----and r401.status_rashi in (90) 
--and r401.status_rashi = 20 
and r400.sug_polisa in ('1','3','5','7') 
and r400.sug_hishtatfut <>0 
and r400.sug_hazmada <>0 
and r400.m_polisa IN (XXXXX)); 

而且我從SQL例外,我通過Excel中使用Oracle和VBA,我認爲這太大字符串,所以它不工作,並且sql成爲查詢的一部分。我該如何在vba代碼中使用此查詢?

+1

你需要告訴我們你得到了什麼錯誤。這個查詢不是太大。 –

+0

刪除圓括號,'insert into R7810TEST select ...'。 – jarlh

+0

:描述:「ORA-00907:缺少右括號」:字符串:但是如果我在qurey上如此行動,所以不是所有的qurey在嚴格的內部,並且是sql認爲缺少右括號 – Tzahi

回答

0

我可以在這裏看到一些可能的東西。這很難不DDL告訴了R7810TEST,但我有2.5猜測

  1. 在SQL的最後聲明說r400.m_polisa IN (XXXXX)。這似乎沒有強類型。這是一個佔位符,還是這是真正的SQL?如果XXXXX是一個字符串,則需要引號。如果它應該是一個數字,那麼......你知道的。如果它是其中一個表格中的字段,那麼我認爲它沒關係,但如果是這樣的話,這是一個奇怪的構造。
  2. VBA不適合大型連接。你沒有列出你的VBA代碼,但是我可以很容易地看到它和你的VBA代碼一樣大。爲了解決這個問題,你可以將連接分成多個步驟。儘管你的SQL實際上似乎有平衡的圓括號(除了select語句周圍的括號是不必要的事實的例外),當你試圖用VBA字符串包裝它時,不會錯過小的東西。
  3. 對第2項的跟進 - 再次使用VBA連接,像缺少空間一樣簡單可能會影響最佳SQL。例如:

例如:

sql = "select one, two, three" & _ 
     "from foo" 

無意間變成了:

select one, two, threefrom foo 

樣品爲點#2:

cmd.CommandText = _ 
    "insert into R7810TEST" & _ 
    "select" & _ 
    " 1111111 , 111111 , 1111111 ,'ORG' ,R401.ZIHUI_BAAL_POLISA ,8990 ,'ID' ," & _ 
    " R401.ZIHUI_M_RASHI , R401.M_POLISA ,R440.M_SOCHEN , 201507 ,'ORG' ," & _ 
    " R401.ZIHUI_BAAL_POLISA ,'RTP' , 'CUR' , '1', '*' ,0 ,10000.00 ," & _ 
    " 0.00 , 0.00 , 0.00, 0.00, 0.00 , 0.00,0.00, 0.00 ," & _ 
    " r430.hf_oved_tagmulim_45 ,r430.hf_mavid_tagmulim ," & _ 
    " r430.hf_mavid_pizuim_mukar , 0.00, r430.hf_mavid_chelef," & _ 
    " r430.hf_oved_shonot, r430.hf_mavid_shonot , r430.hf_oved_tagmulim_47 ," & _ 
    " 'MNG', 0.00 , 0.00 ,0.00 ,0.00 , '*' , to_date ('20150701', 'yyyymmdd')," & _ 
    " R401.ZIHUI_BAAL_POLISA, r400.sug_polisa, to_date ('20150701', 'yyyymmdd')," & _ 
    " 'CHI', '12' , 0, 0 ,'0', to_date ('19000101', 'yyyymmdd') " 

cmd.CommandText = cmd.CommandText & _ 
    "from r400 R400 ,r401 R401 ,r430 r430 ,r440 r440 " & _ 
    "where r400.m_polisa = r401.m_polisa " & _ 
    "and r401.m_polisa = r430.m_polisa " & _ 
    "and r430.m_polisa= r440.m_polisa " & _ 
    "and r401.tr_rishum in (select max(aa.tr_rishum) " & _ 
     "from r401 aa where aa.m_polisa = r401.m_polisa) " & _ 
    "and r430.tr_rishum in (select max(aa.tr_rishum) " & _ 
     "from r430 aa where aa.m_polisa = r430.m_polisa) " & _ 
    "and r440.tr_rishum in (select max(aa.tr_rishum) " & _ 
     "from r440 aa where aa.m_polisa = r440.m_polisa) " & _ 
    "and r401.status_rashi = 10 " & _ 
    "and r400.sug_polisa in ('1','3','5','7') " & _ 
    "and r400.sug_hishtatfut <>0 " & _ 
    "and r400.sug_hazmada <>0 " & _ 
    "and r400.m_polisa IN ('XXXXX')" 

有疑問時,自由使用的SQL中的空白。

分手 - 如果這沒有幫助,請張貼您的表的DDL。它可能會提供有關錯誤的提示。

0

你不會說錯誤是什麼,但它看起來像你的插入...選擇格式不正確。作爲一種良好的做法,您應該在您的select子句前面指定要插入的列,並刪除select處的括號。就像這樣:

insert into R7810TEST 
(column1, column2, ...etc) 
select 1111111 , 111111 , 1111111 ,'ORG' ...etc 
+0

ii在PL/SQL中運行這個查詢就像runnig優秀的時候我把它放在vba中我不工作,所以qurey很好 – Tzahi

+0

「ORA-00936:missing expression」這是我得到的錯誤,這是插入到字符串virble後的字符串:「插入到R7810TEST (請選擇7349926.89865094,580081.084408164,6215666.96627682,'ORG',R401。ZIHUI_BAAL_POLISA,8990,'ID', R401.ZIHUI_M_RASHI,775932,R440.M_SOCHEN,1,'ORG',R401.ZIHUI_BAAL_POLISA,'RTP', ,'1','*', 8,1,1, 1,1, 1,1, 1,1,1,並非所有的字符串都進入字符串VAR – Tzahi

+1

「R401.ZIHUI_BAAL_POLISA,'RTP','1',」會給你一個錯誤,因爲那裏這兩個逗號之間沒有任何內容。它看起來像你用來構造這個查詢的代碼有一個錯誤。 –