运行时错误' 13':SQL代码中的类型不匹配

时间:2014-09-30 13:55:35

标签: sql vba

我有一个'运行时错误'13':为VBA中的部分SQL代码键入“mismatch”。

错误引用的代码是:

SQL = SQL & "   (select unique sv.gps_planshpdate from oes_delsegview sv, oes_opos op, oes_oposdelseg ds, oes_nrbom nb, ncf_comppart cp where cp.item_part_no = pd.part_no and nb.c_catnr = cp.catnr and nb.c_prodtyp = cp.prodtyp and nb.c_packtyp = cp.packtyp and nb.c_vernr = cp.vernr " & _
SQL = SQL & "       and sv.ordnr = ds.ordnr and sv.posnr = ds.posnr and sv.catnr = nb.p_catnr and sv.prodtyp = nb.p_prodtyp and sv.packtyp = nb.p_packtyp and op.ordnr = ds.ordnr and op.posnr = ds.posnr and op.catnr = nb.p_catnr and op.prodtyp = nb.p_prodtyp and op.packtyp = nb.p_packtyp " & _
SQL = SQL & "       and op.vernr = nb.p_vernr and nb.active = 'Y' and op.ol_typ in ('XX','CO') and sv.gps_planshpdate = " & _
SQL = SQL & "           (select max(sv1.gps_planshpdate) from oes_delsegview sv1, oes_opos op1, oes_oposdelseg ds1, oes_nrbom nb1, ncf_comppart cp1 where cp1.item_part_no = cp.item_part_no and nb1.c_catnr = cp1.catnr " & _
SQL = SQL & "               and nb1.c_prodtyp = cp1.prodtyp and nb1.c_packtyp = cp1.packtyp and nb1.c_vernr = cp1.vernr and sv1.ordnr = ds1.ordnr and sv1.posnr = ds1.posnr and sv1.catnr = nb1.p_catnr and sv1.prodtyp = nb1.p_prodtyp and sv1.packtyp = nb1.p_packtyp " & _
SQL = SQL & "               and op1.ordnr = ds1.ordnr and op1.posnr = ds1.posnr and op1.catnr = nb1.p_catnr and op1.prodtyp = nb1.p_prodtyp and op1.packtyp = nb1.p_packtyp and op1.vernr = nb1.p_vernr and nb1.active = 'Y' and sv1.ord_o_status = '9' and op1.ol_typ in ('XX','CO'))), " 'last_ship_date_manufacturing

任何想法是如何在没有“类型不匹配”错误的情况下运行此代码?

由于

1 个答案:

答案 0 :(得分:1)

当我评估你的表达式时,它会返回FALSE,这肯定是导致不匹配错误的原因,因为SQL需要一个字符串查询。

问题在于你原来的连接方法,它将两个错误的组合在一起。

如果你想这样做:

Sql = Sql & " some expression"
Sql = Sql & " some other expression"
'Etc.

请注意,在上面,我做了 NOT 继续这一行,就像你做的那样:

Sql = Sql & " some expression" & _
Sql = Sql & " some other expression" 

这会引发错误。如此可视化:

Sql = Sql & " A " & Sql = Sql & " B "

或者:

Sql = ((Sql & " A " & Sql) = (Sql & " B "))

错误引发,因为此表达式在逻辑上计算为False:此赋值语句的右侧是等价语句,并且因为等效语句中的两个表达式 NOT 相等,它只能返回FALSE

除了纠正行继续符的误用之外,我没有对您的查询表达式进行任何更改:

Dim SQL As String

SQL = SQL & "   (select unique sv.gps_planshpdate from oes_delsegview sv, " & _
            "oes_opos op, oes_oposdelseg ds, oes_nrbom nb, ncf_comppart cp " & _
            "where cp.item_part_no = pd.part_no and nb.c_catnr = cp.catnr and " & _
            "nb.c_prodtyp = cp.prodtyp and nb.c_packtyp = cp.packtyp and nb.c_vernr = cp.vernr " & _
            "       and sv.ordnr = ds.ordnr and sv.posnr = ds.posnr and sv.catnr = nb.p_catnr " & _
            "and sv.prodtyp = nb.p_prodtyp and sv.packtyp = nb.p_packtyp and op.ordnr = ds.ordnr " & _
            "and op.posnr = ds.posnr and op.catnr = nb.p_catnr and op.prodtyp = nb.p_prodtyp and " & _
            "op.packtyp = nb.p_packtyp " & _
            "       and op.vernr = nb.p_vernr and nb.active = 'Y' and op.ol_typ in ('XX','CO') and " & _
            "sv.gps_planshpdate = " & _
            "           (select max(sv1.gps_planshpdate) from oes_delsegview sv1, oes_opos op1, " & _
            "oes_oposdelseg ds1, oes_nrbom nb1, ncf_comppart cp1 where cp1.item_part_no = " & _
            "cp.item_part_no and nb1.c_catnr = cp1.catnr " & _
            "               and nb1.c_prodtyp = cp1.prodtyp and nb1.c_packtyp = cp1.packtyp and " & _
            "nb1.c_vernr = cp1.vernr and sv1.ordnr = ds1.ordnr and sv1.posnr = ds1.posnr and " & _
            "sv1.catnr = nb1.p_catnr and sv1.prodtyp = nb1.p_prodtyp and sv1.packtyp = nb1.p_packtyp " & _
            "               and op1.ordnr = ds1.ordnr and op1.posnr = ds1.posnr and op1.catnr = " & _
            "nb1.p_catnr and op1.prodtyp = nb1.p_prodtyp and op1.packtyp = nb1.p_packtyp and op1.vernr " & _
            "= nb1.p_vernr and nb1.active = 'Y' and sv1.ord_o_status = '9' and op1.ol_typ in ('XX','CO'))), " 'last_ship_date_manufacturing


Debug.Print SQL