【问题标题】:How to INSERT a row in a table if this row doesn't already exist in same table如果同一表中尚不存在该行,如何在表中插入一行
【发布时间】:2016-09-21 07:46:49
【问题描述】:

如果同一表中不存在该行,如何INSERT 表中的一行? 我想要这样的东西。

insert into note (note_id, user_book_id, course_user_id, book_edition_id, book_id, role_type_id, page_id, book_page_number, xcoord, ycoord,
                        width, height, share_across_courses, date_created, date_updated, created_by, updated_by, description, share_with_students,text)

select  note_s.nextval, i_user_book_id, i_course_user_id, book_edition_id, book_id, n.role_type_id, page_id, book_page_number, xcoord,          ycoord, width, height, share_across_courses, sysdate, sysdate, i_user_id, i_user_id, description, share_with_students,text

from  note n inner join course_user cu
on n.course_user_id = cu.course_user_id
where     cu.course_id = 23846
and where not exists (select  note_s.nextval, i_user_book_id, i_course_user_id, book_edition_id, book_id, n.role_type_id, page_id, book_page_number, xcoord, ycoord,
                    width, height, share_across_courses, sysdate, sysdate, i_user_id, i_user_id, description, share_with_students,text

from  note n inner join course_user cu
on n.course_user_id = cu.course_user_id
where     cu.course_id = 23846);

也就是说,如果记录已经存在于特定 course_user_id 的记录表中​​,则什么也不做。否则,如果该特定 course_user_id 没有条目,则插入该 course_user_id 的注释中。

但我的代码不起作用。

这里,note_id 在 note 表中是 PRIMARY KEYCourse_user_id 在 course_user 表中是 PRIMARY KEY

【问题讨论】:

标签: sql oracle oracle11g not-exists


【解决方案1】:

您可以尝试 MERGE 来完成此操作。希望下面的 sn-p 有帮助。我没有测试它,因为我现在没有工作空间\w。

MERGE INTO note nt USING
(SELECT note_s.nextval,
  i_user_book_id,
  i_course_user_id,
  book_edition_id,
  book_id,
  n.role_type_id,
  page_id,
  book_page_number,
  xcoord,
  ycoord,
  width,
  height,
  share_across_courses,
  sysdate,
  sysdate,
  i_user_id,
  i_user_id,
  description,
  share_with_students,
  text
FROM note n
INNER JOIN course_user cu
ON n.course_user_id = cu.course_user_id
WHERE cu.course_id  = 23846
AND NOT EXISTS
  (SELECT note_s.nextval,
    i_user_book_id,
    i_course_user_id,
    book_edition_id,
    book_id,
    n.role_type_id,
    page_id,
    book_page_number,
    xcoord,
    ycoord,
    width,
    height,
    share_across_courses,
    sysdate,
    sysdate,
    i_user_id,
    i_user_id,
    description,
    share_with_students,
    text
  FROM note n
  INNER JOIN course_user cu
  ON n.course_user_id     = cu.course_user_id
  ))A ON (a.course_user_id = nt.course_user_id) 
  WHEN MATCHED THEN
UPDATE SET nt.bookId = a.book_id /*-- dummy update*/
  WHEN NOT MATCHED THEN
INSERT
  (
    nt.note_id,
    nt.user_book_id,
    nt.course_user_id,
    nt.book_edition_id,
    nt.book_id,
    nt.role_type_id,
    nt.page_id,
    nt.book_page_number,
    nt.xcoord,
    nt.ycoord,
    nt.width,
    nt.height,
    nt.share_across_courses,
    nt.date_created,
    nt.date_updated,
    nt.created_by,
    nt.updated_by,
    nt.description,
    nt.share_with_students,
    nt.text
  )
  VALUES
  (
    a.note_id,
    a.user_book_id,
    a.course_user_id,
    a.book_edition_id,
    a.book_id,
    a.role_type_id,
    a.page_id,
    a.book_page_number,
    a.xcoord,
    a.ycoord,
    a.width,
    a.height,
    a.share_across_courses,
    a.date_created,
    a.date_updated,
    a.created_by,
    a.updated_by,
    a.description,
    a.share_with_students,
    a.text
  );

【讨论】:

  • 我使用了您的代码,但它在第 7 行给出了错误:ORA-00936:缺少表达式。请指导。
【解决方案2】:

此外,您可以尝试使用 ORACLE HINTS 的另一种方法来避免 INDEX 插入时出现 dup val,如下所示。但这适用于 11g oracle DB 及以上版本。希望这会有所帮助。

INSERT
  /*+ ignore_row_on_dupkey_index(note_id) */
INTO note
  (
    note_id,
    user_book_id,
    course_user_id,
    book_edition_id,
    book_id,
    role_type_id,
    page_id,
    book_page_number,
    xcoord,
    ycoord,
    width,
    height,
    share_across_courses,
    date_created,
    date_updated,
    created_by,
    updated_by,
    description,
    share_with_students,
    text
  )
  (SELECT note_s.nextval,
      i_user_book_id,
      i_course_user_id,
      book_edition_id,
      book_id,
      n.role_type_id,
      page_id,
      book_page_number,
      xcoord,
      ycoord,
      width,
      height,
      share_across_courses,
      sysdate,
      sysdate,
      i_user_id,
      i_user_id,
      description,
      share_with_students,
      text
    FROM note n
    INNER JOIN course_user cu
    ON n.course_user_id = cu.course_user_id
    WHERE cu.course_id  = 23846
    AND NOT EXISTS
      (SELECT note_s.nextval,
        i_user_book_id,
        i_course_user_id,
        book_edition_id,
        book_id,
        n.role_type_id,
        page_id,
        book_page_number,
        xcoord,
        ycoord,
        width,
        height,
        share_across_courses,
        sysdate,
        sysdate,
        i_user_id,
        i_user_id,
        description,
        share_with_students,
        text
      FROM note n
      INNER JOIN course_user cu
      ON n.course_user_id = cu.course_user_id
      WHERE cu.course_id  = 23846
      ));

【讨论】:

  • 我建议在重复问题中添加这个答案,这个问题将被关闭。
  • 它说.. ORA-00936: 缺少表达式
  • PACKAGE BODY EBOOK_VIEWER_PKG 错误:LINE/COL ERROR 2239/12 PL/SQL:SQL 语句被忽略 2283/16 PL/SQL:ORA-00936:缺少表达式 2383/6 PL/SQL:SQL 语句忽略 2427/16 PL/SQL:ORA-00936:缺少表达式
  • 你现在可以试试。我已经修改了一些语法错误\。让我知道这是否有帮助。此外,我也更新了 MERGE 答案,没有语法错误。
猜你喜欢
  • 2022-06-28
  • 1970-01-01
  • 1970-01-01
  • 2019-01-11
  • 2014-07-14
  • 1970-01-01
  • 2015-02-16
  • 2011-08-26
  • 1970-01-01
相关资源
最近更新 更多