重复的插入到表值中发生,跳过该特定插入而无需错误,然后进行下一个插入语句
我有一个名为table1的表,有2列(否,名称),
CREATE TABLE TABLE1( NO NUMBER(3) NOT NULL, NAME VARCHAR2(5) NOT NULL);
CREATE UNIQUE INDEX TX ON TABLE1(NO,NAME);
我试图插入值如下所述:
INSERT INTO TABLE1 VALUES(101, 'JOHN');
INSERT INTO TABLE1 VALUES(102, 'MORN');
INSERT INTO TABLE1 VALUES(103, 'TONY');
INSERT INTO TABLE1 VALUES(103, 'TONY');
INSERT INTO TABLE1 VALUES(103, 'RONY');
如上所述,我给了一个插入语句两次。实际上,上面的插入语句工作正常,但是当它到达第四个插入语句时,它是抛出的错误。我正在寻找插入值对值语句的插入,如果重复插入语句出现,它应该跳过该特定的插入语句而不显示错误并进行下一个插入语句。
我尝试使用以下语句,但它在Oracle SQL中不起作用
INSERT IGNORE INTO TABLE1 VALUES(101, 'JOHN');
INSERT IGNORE INTO TABLE1 VALUES(102, 'MORN');
INSERT IGNORE INTO TABLE1 VALUES(103, 'TONY');
INSERT IGNORE INTO TABLE1 VALUES(103, 'TONY');
INSERT IGNORE INTO TABLE1 VALUES(103, 'RONY');
I have a table named TABLE1 having 2 columns (NO, NAME)
CREATE TABLE TABLE1( NO NUMBER(3) NOT NULL, NAME VARCHAR2(5) NOT NULL);
CREATE UNIQUE INDEX TX ON TABLE1(NO,NAME);
I am trying to insert values as mentioned below:
INSERT INTO TABLE1 VALUES(101, 'JOHN');
INSERT INTO TABLE1 VALUES(102, 'MORN');
INSERT INTO TABLE1 VALUES(103, 'TONY');
INSERT INTO TABLE1 VALUES(103, 'TONY');
INSERT INTO TABLE1 VALUES(103, 'RONY');
As you see above, I have given one insert statement twice. Actually, above insert statements are working fine but when it comes 4th insert statement, it is throwing error which is expected. I am looking for INSERT INTO VALUES statement if duplicate insert statement comes it should skip that particular insert statement without showing error and go for next INSERT statement.
I tried using the below statements but its not working in Oracle SQL
INSERT IGNORE INTO TABLE1 VALUES(101, 'JOHN');
INSERT IGNORE INTO TABLE1 VALUES(102, 'MORN');
INSERT IGNORE INTO TABLE1 VALUES(103, 'TONY');
INSERT IGNORE INTO TABLE1 VALUES(103, 'TONY');
INSERT IGNORE INTO TABLE1 VALUES(103, 'RONY');
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用错误日志和重定向错误:
然后:
然后:
输出:
和:
输出:
db< “ rel =“ nofollow noreferrer”>此处
You can use an error log and redirect errors to that:
Then:
Then:
Outputs:
and:
Outputs:
db<>fiddle here
您可以使用
Merge
语句检查现有行,只插入新的行:然后:
输出:
db&lt;&gt;
You can use
MERGE
statements to check for existing rows and only insert new ones:Then:
Outputs:
db<>fiddle here
您可以将
插入
语句包装在PL/SQL匿名块中,并捕获dup_val_on_index
exception:然后:
输出:输出:
db&lt;
You can wrap your
INSERT
statements in a PL/SQL anonymous block and catch theDUP_VAL_ON_INDEX
exception:Then:
Outputs:
db<>fiddle here
您可以使用提示您的
插入
语句以跳过此类行。但它起作用比带有检查或错误登录表的普通插入量要慢得多。db&lt;&gt; fiddle
You may use
IGNORE_ROW_ON_DUPKEY_INDEX
hint in yourINSERT
statements to skip such rows. But it works much slower than plain insert with check or error logging table.db<>fiddle here