尝试使用大型SQL查询

时间:2016-01-25 15:37:38

标签: sql oracle excel-vba vba excel

我尝试使用像这样的白色大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中得到一个例外,我使用Oracle和vba通过Excel,我认为这个字符串太大,所以它不起作用,而sql得到了查询的一部分。如何在vba代码中使用此查询?

2 个答案:

答案 0 :(得分:0)

您没有说出错误是什么,但看起来您的插入...选择格式错误。作为一种良好实践,您应该在select子句之前指定要插入的列,并删除select周围的括号。像这样:

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

答案 1 :(得分:0)

我可以在这里看到一些可能发生的事情。没有R7810TEST的DDL很难说,但我有2.5猜测

  1. SQL中的最后一个语句是r400.m_polisa IN (XXXXX)。这似乎不是强类型的。这是占位符,还是真正的SQL?如果XXXXX是一个字符串,则需要引号。如果它应该是一个数字,那么......你知道。如果它是其中一个表中的一个字段,那么我认为它没关系,但如果是这样的话,这是一个奇怪的构造。
  2. VBA在大型连接中表现不佳。你没有列出你的VBA代码,但我可以很容易地看到它像你一样大的字符串。要解决这个问题,您可以将串联分解为多个步骤。虽然你的SQL实际上似乎有平衡的括号(除了select语句周围的括号是不必要的),当你试图将它包装在VBA字符串中时,不容错过一些小的东西。
  3. 关于第2项的后续行动 - 再次使用VBA连接,像缺少空间这样简单的东西可能会破坏您最好的SQL。例如:
  4. 例如:

    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。它可以提供一个关于错误的提示。