DELIMITER $$;DROP PROCEDURE IF EXISTS `emushu`.`sp_guests`$$CREATE PROCEDURE sp_test() BEGIN declare cnt int; select 1 from account where username='123' into cnt; if cnt != 0 then insert into account (username,passwd) values('123','123'); end if; END$$DELIMITER ;$$
执行: call sp_guests();
DELIMITER $$;DROP PROCEDURE IF EXISTS sp_test$$CREATE PROCEDURE sp_test() BEGIN declare cnt int; select 1 from account where username='123' into cnt; if cnt != 0 then insert into account (username,passwd) values('123','123'); end if; END$$DELIMITER ;$$
我这样的写法在ms sql 2000里行得通,到MYSQL里就报错了
[INTO] tbl_name [(col_name,...)]
SELECT ...
[ ON DUPLICATE KEY UPDATE col_name=expr, ... ]
DELIMITER $$;DROP PROCEDURE IF EXISTS `emushu`.`sp_guests`$$CREATE PROCEDURE sp_test()
BEGIN
declare cnt int;
select 1 from account where username='123' into cnt;
if cnt != 0 then
insert into account (username,passwd) values('123','123');
end if;
END$$DELIMITER ;$$
call sp_guests();
DELIMITER $$;DROP PROCEDURE IF EXISTS sp_test$$CREATE PROCEDURE sp_test()
BEGIN
declare cnt int;
select 1 from account where username='123' into cnt;
if cnt != 0 then
insert into account (username,passwd) values('123','123');
end if;
END$$DELIMITER ;$$