这是Informix更新的正确语法吗?
update table1
set table1.code = 100
from table1 a, table2 b, table3 c
where a.key = c.key
a.no = b.no
a.key = c.key
a.code = 10
b.tor = 'THE'
a.group = 4183
a.no in ('1111','1331','1345')
我得到通用的-201'发生语法错误'消息,但我看不出有什么问题。
答案 0 :(得分:6)
您的语法错误是table1.code
set table1.code = 100
将此更改为
set a.code = 100
完整代码
update table1
set a.code = 100
from table1 a, table2 b, table3 c
where a.key = c.key
and a.no = b.no
and a.key = c.key
and a.code = 10
and b.tor = 'THE'
and a.group = 4183
and a.no in ('1111','1331','1345')
答案 1 :(得分:6)
不幸的是,接受的答案会导致Informix Dynamic Server Version 11.50中出现语法错误。
这是避免语法错误的唯一方法:
update table1
set code = (
select 100
from table2 b, table3 c
where table1.key = c.key
and table1.no = b.no
and table1.key = c.key
and table1.code = 10
and b.tor = 'THE'
and table1.group = 4183
and table1.no in ('1111','1331','1345')
)
BTW,到get Informix version,运行以下SQL:
select first 1 dbinfo("version", "full") from systables;
答案 2 :(得分:3)
问题中的原始SQL是:
update table1
set table1.code = 100
from table1 a, table2 b, table3 c
where a.key = c.key
a.no = b.no
a.key = c.key
a.code = 10
b.tor = 'THE'
a.group = 4183
a.no in ('1111','1331','1345')
这无条件地缺少一系列AND关键字。接受的解决方案还使用table1
而不是别名a
来识别SET子句中的问题。那可能很重要;我无法测试它(见下面的讨论)。因此,假设完全接受了连接UPDATE,更正后的SQL应为:
UPDATE table1
SET a.code = 100
FROM table1 a, table2 b, table3 c
WHERE a.key = c.key
AND a.no = b.no
AND a.key = c.key
AND a.code = 10
AND b.tor = 'THE'
AND a.group = 4183
AND a.no IN ('1111','1331','1345')
这与(语法修正的)接受的答案相同。但是,我很想知道您正在使用哪个版本的Informix接受FROM语法(可能是XPS?)。我在Mac OS X 10.7.4上使用的是IDS 11.70.FC2(当前11.70.FC5版本背后的3个修复包),我无法使用FROM语法获得UPDATE。此外,IBM Informix 11.70 Information Center更新UPDATE的手册没有提到它。如果您使用ODBC或JDBC,我不确定它是否会有任何区别;它不应该,但我正在使用ESQL / C,它将SQL不变地发送到服务器。
我尝试的符号是(+
是提示符):
+ BEGIN;
+ CREATE TABLE a(a INTEGER NOT NULL, x CHAR(10) NOT NULL, y DATE NOT NULL);
+ INSERT INTO a(a, x, y) VALUES(1, 'obsoletely', '2012-04-01');
+ INSERT INTO a(a, x, y) VALUES(2, 'absolutely', '2012-06-01');
+ CREATE TABLE b(b INTEGER NOT NULL, p CHAR(10) NOT NULL, q DATE NOT NULL);
+ INSERT INTO b(b, p, q) VALUES(3, 'daemonic', '2012-07-01');
+ SELECT * FROM a;
1|obsoletely|2012-04-01
2|absolutely|2012-06-01
+ SELECT * FROM b;
3|daemonic|2012-07-01
+ SELECT *
FROM a, b
WHERE a.a < b.b
AND b.p MATCHES '*a*e*';
1|obsoletely|2012-04-01|3|daemonic|2012-07-01
2|absolutely|2012-06-01|3|daemonic|2012-07-01
+ UPDATE a
SET x = 'crumpet'
FROM a, b
WHERE a.a < b.b
AND b.p MATCHES '*a*e*';
SQL -201: A syntax error has occurred.
SQLSTATE: 42000 at <<temp>>:23
+ SELECT * FROM a;
1|obsoletely|2012-04-01
2|absolutely|2012-06-01
+ ROLLBACK;
答案 3 :(得分:0)
对于Informix SE 7.25 ......
另一个解决方案是将其分解为两个查询:
首先,获取所需记录的ROWID(在多个表上过滤):
SELECT a.ROWID
FROM table1 a, table2 b, table3 c
WHERE a.key = c.key
AND a.no = b.no
AND a.key = c.key
AND a.code = 10
AND b.tor = 'THE'
AND a.group = 4183
AND a.no IN ('1111','1331','1345')
将结果放入逗号分隔的字符串中。
然后,仅更新在第一个查询中找到ROWID的主表的那些记录:
UPDATE table1 a
SET a.code = 100
WHERE a.ROWID in ([comma separated ROWIDs found above])
答案 4 :(得分:0)
这取决于您使用的版本。如果您使用至少11.50,最佳解决方案将是:
MERGE INTO table1 as t1
USING table2 as t2
ON t1.ID = t2.ID
WHEN MATCHED THEN UPDATE set (t1.col1, t1.col2) = (t2.col1, t2.col2);
UPDATE - SET - FROM - 在大于11.50的版本中删除了语法。
如果您使用的是早期版本,则可以使用
UPDATE t SET a = t2.a FROM t, t2 WHERE t.b = t2.b;