嵌入式SQL中的UNION子句

时间:2015-06-10 14:00:00

标签: sql ingres

我正在使用ingres 10S SQL,我正在尝试在嵌入式SQL C程序中编写以下SQL语句。它作为一个独立的SQL脚本工作正常,但编译eSQL程序会收到错误

  

%% Error in file localtask.sc, Line 498: E_EQ0244 Syntax error on 'union'.

INSERT INTO nr301_tab2 (authority_id)
SELECT a.authority_id
FROM nrremdets a, nrstatus_hierarchy z
WHERE a.authority_id = z.authority_id
UNION
SELECT a.authority_id
FROM nrsumsamts a, nrsumsdets b
WHERE a.authority_id = b.authority_id;

(第498行是UNION行)UNION条有什么问题?

1 个答案:

答案 0 :(得分:1)

只需稍微调整一下查询即可。试试这个:

INSERT INTO nr301_tab2 (authority_id)
SELECT authority_id
FROM
    (SELECT a.authority_id
     FROM nrremdets a, nrstatus_hierarchy z
     WHERE a.authority_id = z.authority_id
     UNION
     SELECT a.authority_id
     FROM nrsumsamts a, nrsumsdets b
     WHERE a.authority_id = b.authority_id) Result

我们的想法是结合结果集,这将创建一个在表中插入一次的新结果集。通过添加括号,我们确保发生这种情况。