这个查询在 Informix 中可行吗?
insert into emp(emp_id, sal, desg)
values (111, (select salary from emp_sal where emp_id=222), 'xxx');
表结构为:
emp:emp_id、姓名、sal、desg
emp_sal:emp_id,sal
大多数数据库(包括informix)都支持
insert into ... select
:
INSERT INTO emp(emp_id, sal, desg)
SELECT 111, salary, 'xxx'
FROM emp_sal
WHERE emp_id = 222;
⚠ 请注意,如果选择返回多于一行,则将插入多行。
只要子查询返回单行,所编写的语句就应该有效。
概念证明:
SQL[1871]: create temp table x(i integer, j integer, s char(10));
SQL[1872]: insert into x(i,j,s) values(1, (select atomic_number from elements where name = 'Carbon'), "Elephant");
SQL[1873]: select * from x;
1|6|Elephant
SQL[1874]:
我的测试数据库中有一个元素表,因此子选择对我有用。警告:我在 11.70.FC6 上测试,而不是 7.31。鉴于您似乎使用的是更旧版本的 Informix(7.31 是在 Y2K、IIRC 之前首次发布的,尽管 7.31.UDn 是 2000 年代中期(可能是 2005 年左右)的修复包,您的情况可能会有所不同。