Oracle插入不同位置的属性

发布于 2025-01-10 19:31:04 字数 959 浏览 0 评论 0原文

请告诉我如果我们按照不同的属性位置顺序放置属性,Oracle insert 语句是否有效。我无法在开发环境中进行测试,因此在直接推动 PROD 中的更改之前需要专家意见。请帮忙。

我有以下表格:

tableA - col1, col2, col3, col4,col5

tableB - col1, col2, col4, col5

我需要从 tableB 中选择不同的值并通过向其添加序列号来插入到表A中。

由于不同序列号在插入语句中不能一起工作,因此我使用外部选择语句。

请让我知道以下 2 个选项中哪一个有效???如果两者都不起作用,那么也请提供您的建议。

选项 1 - 在最后的外部 select 语句中添加 nextval 并将 col3 保留为 insert 中的最后一个位置

insert into tableA ( col1, col2, col4, col5, col3 ) select col1, col2 , col4, col5, my_seq.nextval as col3 from ( select different col1, col2, col4, col5 from tableB );

选项2-在外部添加nextval选择语句以相同的顺序,并将 col3 保持在插入中的相同位置

insert into tableA ( col1, col2, col3, col4, col5 ) select col1, col2, my_seq.nextval as col3, col4 , col5 from (从 tableB 中选择不同的 col1, col2, col4, col5 );

提前致谢!!

Please let me know whether Oracle insert statement works if we place the attributes in a different sequence in terms of attribute positions. I am not able to test in dev environment so need expert opinion before I promote changes in PROD directly. Please help.

I am having the following tables:

tableA - col1, col2, col3, col4,col5

tableB - col1, col2, col4, col5

I need to pick the distinct values from tableB and insert into tableA by adding a sequence number to it.

Since distinct and sequence numbers don't work together in insert statements I am using an outer select statement.

Please let me know which of the following 2 options will work ??? If both don't work then please provide your suggestions as well.

option 1 - adding nextval in the outer select statement at the last and keeping col3 as the last position in insert

insert into tableA ( col1, col2, col4, col5, col3 ) select col1, col2, col4, col5, my_seq.nextval as col3 from ( select distinct col1, col2, col4, col5 from tableB );

option 2- adding nextval in the outer select statement in the same sequence and keeping col3 as also in the same position in insert

insert into tableA ( col1, col2, col3, col4, col5 ) select col1, col2, my_seq.nextval as col3, col4, col5 from ( select distinct col1, col2, col4, col5 from tableB );

thanking in advance!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

世界等同你 2025-01-17 19:31:04

两者都会起作用。只要指定列名称(在 insert into 中)并在后面的 select 中匹配它们,插入它们的顺序并不重要。

SQL> create sequence my_seq;

Sequence created.

SQL> create table tablea (deptno number, job varchar2(10), seq number);

Table created.

SQL> -- your first query
SQL> insert into tablea (deptno, job, seq)
  2    select deptno, job, my_seq.nextval
  3    from (select distinct deptno, job from emp);

9 rows created.

SQL> -- your second query
SQL> insert into tablea (seq, job, deptno)
  2    select my_seq.nextval, job, deptno
  3    from (select distinct deptno, job from emp);

9 rows created.

结果:

SQL> select * from tablea order by seq;

    DEPTNO JOB               SEQ
---------- ---------- ----------
        20 CLERK               1
        30 SALESMAN            2
        20 MANAGER             3
        30 CLERK               4
        10 PRESIDENT           5
        30 MANAGER             6
        10 CLERK               7
        10 MANAGER             8
        20 ANALYST             9
        20 CLERK              10
        30 SALESMAN           11
        20 MANAGER            12
        30 CLERK              13
        10 PRESIDENT          14
        30 MANAGER            15
        10 CLERK              16
        10 MANAGER            17
        20 ANALYST            18

18 rows selected.

SQL>

为什么没问题?因为 Scott 的 EMP 表中有 9 种不同的 [deptno, job] 组合。

SQL> select distinct deptno, job from emp;

    DEPTNO JOB
---------- ---------
        20 CLERK
        30 SALESMAN
        20 MANAGER
        30 CLERK
        10 PRESIDENT
        30 MANAGER
        10 CLERK
        10 MANAGER
        20 ANALYST

9 rows selected.

SQL>

Both will work. It doesn't matter in which order you insert them, as long as you specify column names (in insert into) and match them in select that follows.

SQL> create sequence my_seq;

Sequence created.

SQL> create table tablea (deptno number, job varchar2(10), seq number);

Table created.

SQL> -- your first query
SQL> insert into tablea (deptno, job, seq)
  2    select deptno, job, my_seq.nextval
  3    from (select distinct deptno, job from emp);

9 rows created.

SQL> -- your second query
SQL> insert into tablea (seq, job, deptno)
  2    select my_seq.nextval, job, deptno
  3    from (select distinct deptno, job from emp);

9 rows created.

Result:

SQL> select * from tablea order by seq;

    DEPTNO JOB               SEQ
---------- ---------- ----------
        20 CLERK               1
        30 SALESMAN            2
        20 MANAGER             3
        30 CLERK               4
        10 PRESIDENT           5
        30 MANAGER             6
        10 CLERK               7
        10 MANAGER             8
        20 ANALYST             9
        20 CLERK              10
        30 SALESMAN           11
        20 MANAGER            12
        30 CLERK              13
        10 PRESIDENT          14
        30 MANAGER            15
        10 CLERK              16
        10 MANAGER            17
        20 ANALYST            18

18 rows selected.

SQL>

Why is it OK? Because there are 9 distinct combinations of [deptno, job] in Scott's EMP table.

SQL> select distinct deptno, job from emp;

    DEPTNO JOB
---------- ---------
        20 CLERK
        30 SALESMAN
        20 MANAGER
        30 CLERK
        10 PRESIDENT
        30 MANAGER
        10 CLERK
        10 MANAGER
        20 ANALYST

9 rows selected.

SQL>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文