mysql中 储存过程 if exists 该怎么写呀?
CREATE PROCEDURE test(in ip int(20),in guid varchar(32))
BEGIN
if exists ( SELECT * FROM a_usercheck WHERE guid = guid )
BEGIN
SELECT 'Hello Word!';
END
END##
在 phpmyadmin 中执行sql命令,返回错误:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'BEGIN SELECT 'Hello Word!'; END END##' at line 4
正确该怎么写?
请高手指点一下.
谢谢.
------解决方案--------------------delimiter $$
CREATE PROCEDURE test(in ip int(20),in guid varchar(32))
BEGIN
set @x = 0;
SELECT count(1) into @x FROM a_usercheck WHERE guid = guid;
if (@x > 0) then
SELECT 'Hello Word!';
end if;
END$$
------解决方案--------------------注意红色部分。 另外你的 SELECT * FROM a_usercheck WHERE guid = guid 后面的WHERE永远为真。不要让变量与字段重名。
delimiter ##
CREATE PROCEDURE test(in ip int(20),in guid varchar(32))
BEGIN
if exists ( SELECT * FROM a_usercheck WHERE guid = guid ) then
SELECT 'Hello Word!';
end if;
END##
SQL code
mysql> delimiter ##
mysql> CREATE PROCEDURE test(in ip int(20),in guid varchar(32))
-> BEGIN
-> if exists ( SELECT * FROM a_usercheck WHERE guid = guid ) then
-> SELECT 'Hello Word!';
-> end if;
-> END##
Query OK, 0 rows affected (0.08 sec)
mysql>