在informix中更新查询语句

时间:2013-02-18 05:47:57

标签: sql hibernate informix

我会在Infomix中得到关于我的sql语句的帮助。请在下面找到我的代码。我正在加入5个表并与主键链接。我需要的是,我必须根据export_flag='N'country='MA'将export_flag列更新为Y.

但是现在我遇到了像这样的语法错误

  

13:34:12 [UPDATE - 0 row(s),0.000 secs] [错误代码:-201,SQL   状态:42000]发生了语法错误。

我无法找到SQL中的问题。

    update a set a.export_flag ='Y'
    FROM sc_ob_temp_audit AS a
    JOIN sc_ob_allocation  AS b
    ON a.sc_ob_profile_code = b.sc_ob_profile_code 
    AND a.sc_orig_country= b.sc_orig_country
    OUTER JOIN sc_fac_group AS f 
    ON b.sc_orig_country = f.sc_orig_country 
    AND b.sc_orig_sf_group_code = f.sc_fac_group_code
    OUTER JOIN sc_ob_prod_group AS d, JOIN gbl_produce AS e 
    ON d.sc_prod_cont_code = e.gp_cnt_cd
    AND b.sc_orig_country= d.sc_orig_country
    AND b.sc_prod_cont_group_code = d.sc_prod_group_code
    WHERE a.sc_orig_country ='MY'
    AND a.export_flag='N';

1 个答案:

答案 0 :(得分:3)

Informix不支持使用JOIN的那种UPDATE - 请参阅IBM Informix 11.70 InfoCenter处的UPDATE语法。当它遇到无法识别的语法时,它会给出错误-201。


  

如果Informix不支持使用join进行更新,那么如何加入表?无论如何,我必须稍后将此查询转换为Hibernate。

我现在想进入休眠状态,请...你的意思是,你希望我解释你的查询并找出如何做到这一点?没有表的模式(主键,外键特别相关!)?你肯定要在Hibernate中修复它;我从来没有用过它。

这是您的查询,重新格式化我认为更好的可读性。

UPDATE a
   SET a.export_flag ='Y'
  FROM sc_ob_temp_audit AS a
  JOIN sc_ob_allocation  AS b
    ON a.sc_ob_profile_code = b.sc_ob_profile_code 
   AND a.sc_orig_country= b.sc_orig_country
  LEFT JOIN sc_fac_group AS f 
    ON b.sc_orig_country = f.sc_orig_country 
   AND b.sc_orig_sf_group_code = f.sc_fac_group_code
  LEFT JOIN sc_ob_prod_group AS d   -- ON clause missing (but there was a comma here)
  JOIN gbl_produce AS e 
    ON d.sc_prod_cont_code = e.gp_cnt_cd
   AND b.sc_orig_country= d.sc_orig_country             -- Part of missing ON clause?
   AND b.sc_prod_cont_group_code = d.sc_prod_group_code -- Part of missing ON clause?
 WHERE a.sc_orig_country ='MY'
   AND a.export_flag='N';

对于我未经训练的眼睛,这看起来像是一种非常复杂的写作方式:

UPDATE sc_ob_temp_audit
   SET export_flag = 'Y'
 WHERE sc_orig_country = 'MY'
   AND export_flag = 'N'

但是,大概所有连接都会做一些有用的事情,以某种方式限制更新的行集。所以,我们可以猜测它意味着:

UPDATE sc_ob_temp_audit
   SET export_flag = 'Y'
 WHERE sc_orig_country = 'MY'
   AND export_flag = 'N'
   AND sc_primary_key IN      -- Guessed at column; no schema for the DB; no keys!
       (SELECT a.sc_primary_key
          FROM sc_ob_temp_audit AS a
          JOIN sc_ob_allocation  AS b
            ON a.sc_ob_profile_code = b.sc_ob_profile_code 
           AND a.sc_orig_country    = b.sc_orig_country
          LEFT JOIN sc_fac_group AS f 
            ON b.sc_orig_country       = f.sc_orig_country 
           AND b.sc_orig_sf_group_code = f.sc_fac_group_code
          LEFT JOIN sc_ob_prod_group AS d
            ON b.sc_orig_country         = d.sc_orig_country
           AND b.sc_prod_cont_group_code = d.sc_prod_group_code
          JOIN gbl_produce AS e 
            ON d.sc_prod_cont_code = e.gp_cnt_cd
       )

如果实际上您在sc_ob_temp_audit表上没有单列主键,则必须使用与EXISTS相关的子查询:

UPDATE sc_ob_temp_audit
   SET export_flag = 'Y'
 WHERE sc_orig_country = 'MY'
   AND export_flag = 'N'
   AND EXISTS
       (SELECT *         -- It does not matter what you list here in an EXISTS sub-query
          FROM sc_ob_temp_audit AS a
          JOIN sc_ob_allocation  AS b
            ON a.sc_ob_profile_code = b.sc_ob_profile_code 
           AND a.sc_orig_country    = b.sc_orig_country
          LEFT JOIN sc_fac_group AS f 
            ON b.sc_orig_country       = f.sc_orig_country 
           AND b.sc_orig_sf_group_code = f.sc_fac_group_code
          LEFT JOIN sc_ob_prod_group AS d
            ON b.sc_orig_country         = d.sc_orig_country
           AND b.sc_prod_cont_group_code = d.sc_prod_group_code
          JOIN gbl_produce AS e 
            ON d.sc_prod_cont_code = e.gp_cnt_cd
         WHERE a.pk_column1 = sc_ob_temp_audit.pk_column1
           AND a.pk_column2 = sc_ob_temp_audit.pk_column2
           AND ...
       )

我认为这有机会工作;子查询的WHERE子句应使用a来标识子查询中的审计表,并使用全名来标识UPDATE语句中的审计表。但是考虑到晚上的时间,我有可能会错误思考。