【问题标题】:Insert statement with select for only one column in Informix在 Informix 中仅选择一列的插入语句
【发布时间】:2013-04-04 04:32:27
【问题描述】:

只需要知道这个查询在 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版本,子查询支持随着版本的改进
  • 我不确定是否要找到版本。我做了 isql -v,它返回“unixODBC 2.2.8 and dbaccess -V”7.31.UD6”
  • 如果你运行SELECT DBINFO('version','full') FROM informix.systables WHERE tabid = 1,那应该会给你服务器的完整版本字符串。如果没有,您的服务器已经很旧,应该在一段时间前退役。从表面上看,您似乎在使用 IDS 7.31.UD6,该版本已正式停止服务,并且已经使用了几年。

标签: sql informix


【解决方案1】:

我没有尝试使用 Informix,但大多数数据库都支持 insert into ... select

insert into emp(emp_id, sal, desg) 
select 111, salary, 'xxx' 
from emp_sal where emp_id = 222;

【讨论】:

    【解决方案2】:

    只要子查询返回单行,所写的语句就应该有效。

    概念证明:

    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 年左右),因此您的里程可能会有所不同。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多