SQL SERVER repeated insertion and distinction of MYSQL

problem:
If a record exists, is not inserted, if there is no insert
 
In SQL SERVER:
create table b(id int)
insert into b select 1
union all
select 2
union all
select 3
union all
select 4

if not exists(select id from b where id=5)
insert into b(id) values(5)
===
MYSQL is not a simple sentence can only be achieved with stored procedures
 
MYSQL:
create table b(id int);
insert into b(id) values(1),(2),(3),(4);
 
DELIMITER $$
DROP PROCEDURE IF EXISTS `test`.`sp_b`$$
CREATE PROCEDURE `test`.`sp_b`(in i_id int)
    BEGIN
    declare cnt int;
    select count(1) from b where id=i_id into cnt;
    if cnt = 0 then
       insert into b select i_id;
    end if;
    END$$
DELIMITER ;

This article comes from " God, let there or be square! " Blog, reproduced please contact the author!

Reproduced in: https: //my.oschina.net/u/585111/blog/219444

Guess you like

Origin blog.csdn.net/weixin_33816611/article/details/92008326