开发者

How to ease the merge into sql statement

SQL> SELECT * FROM dept;

DEPTNO DNAME          LOC
    10 ACCOUNTING     NEW YORK
    20 RESEARCH       DALLAS
    30 SALES          CHICAGO
    60 HELP DESK      PITTSBURGH
    40 OPERATIONS     BOSTON

SQL> SELECT * FROM dept_online;

DEPTNO DNAME          LOC
    40 OPERATIONS     BOSTON
    20 RESEARCH DEV   DALLAS
    50 ENGINEERING    WEXFORD


SQL> MERGE INTO dept d
 U开发者_StackOverflow社区SING (SELECT deptno, dname, loc
        FROM dept_online) o
 ON (d.deptno = o.deptno)
 WHEN MATCHED THEN
     UPDATE SET d.dname = o.dname, d.loc = o.loc
 WHEN NOT MATCHED THEN
     INSERT (d.deptno, d.dname, d.loc)
     VALUES (o.deptno, o.dname, o.loc);

See in the above information given, After WHEN NOT MATCHED THEN statement in the insert i have to mention every column, instead of typing every column can i replace it something else.


You can omit the column list INSERT but not after VALUES. The manual says:

If you omit the column list after the INSERT keyword, then the number of columns in the target table must match the number of values in the VALUES clause.

What the manual forgets to mention - and why I would recommend against omitting the column names - is that the order of the expressions after VALUES must match the column order in the table's DDL.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜