derby中的sql错误 - 错误42X01:语法错误:遇到“WHERE”

时间:2017-03-15 13:26:12

标签: java sql oracle derby

我已经看到了有关此错误的几个问题,但每个解决方案都不同,因为它是所谓的"语法错误"。我在开发中使用Oracle,在开发中使用Derby(非常讨厌,但我能做些什么)。

当我运行我在Oracle上创建的某个SQL命令时,它似乎工作正常并按照它所做的那样(使用Oracle SQL Developer)。但是当我想在Derby中运行相同的命令时,我遇到了这个错误。

无论我做什么,我都会遇到这个错误。

  

警告| SQL错误:20000,SQLState:42X01

     

错误|语法错误:遇到" WHERE"第94行第6栏。

对于我的生活,我无法弄清楚出了什么问题。这是我的SQL命令。这有点长而复杂:

CREATE VIEW BDPBCDBView AS SELECT 
        BDP_INSTITUTION_NAME,
        BIC,
        BDP_COUNTRY_NAME,               
        BDP_ISO_COUNTRY_CODE,            
        BDP_CITY,                       
        BDP_NETWORK_CONNECTIVITY,      
        BDP_SERVICE_CODES,               
        BDP_ISTARGET,                        
        BCDB_NAME,                       
        BCDB_LAENDERKENNZEICHEN,  
        BCDB_AKTIVMERKMALBANK,          
        BCDB_AKTIVMERKMALLAND,     
        BCDB_AKTIVMERKMALBANKLAND, 
        BCDB_SWIFTKENNZEICHEN,    
        COUNTRYCODE,     
        ISBDP,    
        ISBCDB,    
        BCDB_ORT,
        s1.BICS_RMA 
       FROM
(SELECT 
        bdp.bic, 
        bdp.institution_name AS bdp_institution_name, 
        bdp.country_name AS bdp_country_name, 
        bdp.iso_country_code AS bdp_iso_country_code,
        bdp.city AS bdp_city, 
        bdp.network_connectivity AS bdp_network_connectivity, 
        bdp.service_codes as bdp_service_codes, 
        bdp.isTarget AS bdp_isTarget, 
        bcdb.name as bcdb_name, 
        bcdb.laenderKennzeichen as bcdb_laenderKennzeichen, 
        bcdb.aktivMerkmalBank AS bcdb_aktivMerkmalBank, 
        bcdb.aktivMerkmalLand AS bcdb_aktivMerkmalLand, 
        bcdb.aktivMerkmalBankLand AS bcdb_aktivMerkmalBankLand, 
        bcdb.swiftKennzeichen AS bcdb_swiftKennzeichen, 
        CASE 
            WHEN bcdb.laenderKennzeichen IS NOT NULL THEN bcdb.laenderKennzeichen
            ELSE bdp.iso_country_code
        END AS countryCode,
        CASE 
            WHEN bdp.bic IS NOT NULL THEN 1
            ELSE 0
        END AS isbdp,
        CASE 
            WHEN bcdb.bic IS NOT NULL THEN 1
            ELSE 0
        END AS isbcdb,
        bcdb.ort AS bcdb_ort 
        FROM BDP bdp LEFT JOIN BCDB bcdb ON bdp.bic = bcdb.bic WHERE bdp.bic IS NOT NULL 
    UNION ALL SELECT
        bcdb.bic, 
        bdp.institution_name AS bdp_institution_name, 
        bdp.country_name AS bdp_country_name, 
        bdp.iso_country_code AS bdp_iso_country_code, 
        bdp.city AS bdp_city, 
        bdp.network_connectivity AS bdp_network_connectivity, 
        bdp.service_codes as bdp_service_codes,
        bdp.isTarget AS bdp_isTarget, 
        bcdb.name as bcdb_name, 
        bcdb.laenderKennzeichen as bcdb_laenderKennzeichen, 
        bcdb.aktivMerkmalBank AS bcdb_aktivMerkmalBank, 
        bcdb.aktivMerkmalLand AS bcdb_aktivMerkmalLand, 
        bcdb.aktivMerkmalBankLand AS bcdb_aktivMerkmalBankLand, 
        bcdb.swiftKennzeichen AS bcdb_swiftKennzeichen, 
        CASE 
            WHEN bcdb.laenderKennzeichen IS NOT NULL THEN bcdb.laenderKennzeichen
            ELSE bdp.iso_country_code
        END AS countryCode,
        CASE 
            WHEN bdp.bic IS NOT NULL THEN 1
            ELSE 0
        END AS isbdp,
        CASE 
            WHEN bcdb.bic IS NOT NULL THEN 1
            ELSE 0
        END AS isbcdb,
        bcdb.ort AS bcdb_ort 
    FROM BDP bdp RIGHT JOIN BCDB bcdb ON bdp.bic = bcdb.bic WHERE bdp.bic IS NULL)  
    t1 LEFT JOIN    ( SELECT * FROM
    (
      SELECT s1.BIC_CRSPNDT AS BICS_RMA FROM      
    (SELECT
    rma.crspdt AS BIC_CRSPNDT,
    rma.issr AS BIC_ISSR    
    From RMA    
    WHERE ((RMA.tp= 'Issued' OR RMA.tp = 'Received') AND RMA.RMASTS='Enabled' AND RMA.SVCNM='swift.fin') )s1
    UNION
    SELECT s1.BIC_ISSR AS BIC FROM (SELECT
    rma.crspdt AS BIC_CRSPNDT, 
    rma.issr AS BIC_ISSR     
    FROM RMA 
     WHERE ((RMA.tp= 'Issued' OR RMA.tp = 'Received') AND RMA.RMASTS='Enabled' AND RMA.SVCNM='swift.fin') )s1 ) 
     WHERE BICS_RMA IS NOT NULL      
    ORDER BY BICS_RMA) s1 
    ON (s1.BICS_RMA = substr(t1.BIC, 1,8))

错误发生在第三行到最后一行。

我在Java中的代码读取是:

@PersistenceContext
EntityManager em;

@PostConstruct
public void createViewIfNeeded() {

    if (FidaProfile.isActive(FidaProfile.DEVELOPMENT)) {

        em.createNativeQuery("DROP TABLE BDPBCDBView").executeUpdate();

        String command_1 = loadDevelopmentViewScript("DEV-DB/init_dev_view.sql");//BDPBCDView sql script, this is made from 3 tables namely BCDB, BDP and RMA 

        em.createNativeQuery(command_1).executeUpdate();

    }
}

public void setEm(EntityManager em) {
    this.em = em;
}

private String loadDevelopmentViewScript(String addressOfSQLScript) {
    try {
        InputStream stream = BDPBCDPViewGenerator.class.getClassLoader().getResourceAsStream(addressOfSQLScript);

        ByteArrayOutputStream result = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int length;
        while ((length = stream.read(buffer)) != -1) {
            result.write(buffer, 0, length);
        }
        return result.toString("UTF-8");
    } catch (IOException e) {
        throw new FidaErrorCodeException(FidaErrorCode.UNEXPECTED_EXCEPTION,
                "Could NOT load Development-View-Script", e);
    }
}

1 个答案:

答案 0 :(得分:2)

顺便说一下,良好的格式化对于跟踪复杂查询中发生的事情至关重要!

由于我之前的回答是不正确的,那么如果你要更改s1子查询以便你是univoting而不是使用union呢?类似的东西:

SELECT DISTINCT CASE WHEN dummy.id = 1 THEN r.bic_crspndt
                     WHEN dummy.id = 2 THEN r.bic_issr
                END AS bics_rma
FROM   (SELECT rma.crspdt AS bic_crspndt,
               rma.issr   AS bic_issr
        FROM   rma
        WHERE  (rma.tp = 'Issued' OR rma.tp = 'Received')
        AND    rma.rmasts = 'Enabled'
        AND    rma.svcnm = 'swift.fin') r
       INNER JOIN (SELECT 1 ID FROM dual UNION ALL
                   SELECT 2 ID FROM dual) dummy ON (dummy.id = 1 AND r.crspdt IS NOT NULL)
                                                   OR (dummy.id = 2 AND r.issr IS NOT NULL);

也许如果你这样做,Derby数据库就可以应付它?

N.B。我通过条件交叉连接而不是Oracle 11g UNPIVOT函数使用了手动UNPIVOT,因为我对Derby一无所知并且可能不支持UNPIVOT。您在现场环境和开发环境之间被迫使用不同数据库平台的原因超出了我的意义。听起来疯狂,可能相当危险!我猜你已经尝试过标记过了!