【发布时间】: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 KEY,Course_user_id 在 course_user 表中是 PRIMARY KEY。
【问题讨论】:
-
您可能想查看 MERGE 语句,如果该行已存在于表中,您可以选择如何操作
标签: sql oracle oracle11g not-exists