如果该行不存在于同一个表中,如何在表中插入行

时间:2016-05-24 08:08:36

标签: sql oracle oracle11g not-exists

如果此行在同一个表中已经存在,如何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在注释表中为PRIMARY KEY,而Course_user_id在course_user表中为PRIMARY KEY

2 个答案:

答案 0 :(得分:0)

您可以尝试MERGE来完成此任务。希望下面片段有帮助。我没有测试过,因为我没有工作区,没有\ 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
  );

答案 1 :(得分:0)

此外,您可以尝试使用ORACLE HINTS的另一种方法来避免INDEX插入的重复,如下所示。但这适用于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
      ));