再搞不定我就死了,关于一个存储过程中的IF语句
CREATE PROCEDURE `AddTopics`($fid smallint, $author char(15), $subject char(80), $message mediumtext)
BEGIN
if $message is not null then
DECLARE $authorid mediumint;
SELECT uid INTO $authorid FROM my023bbs_members WHERE username = $author;
INSERT INTO my023bbs_threads (fid, subject, author, authorid, dateline, lastpost) VALUES ($fid, $subject, $author, $authorid, UNIX_TIMESTAMP(), UNIX_TIMESTAMP());
UPDATE my023bbs_forums SET threads = threads + 1 WHERE fid = $fid;
INSERT INTO my023bbs_posts (fid, tid, subject, author, authorid, message, dateline) VALUES ($fid, last_insert_id(), $subject, $author, $authorid, $message, UNIX_TIMESTAMP());
UPDATE my023bbs_forums SET posts = posts + 1 WHERE fid = $fid;
end if
END
加了这个:if $message is not null then的判断,程序就始终出错,即使我在里面再加一层begin, 或者end if 后面加分号,都会出错.
我不知道怎么判断这个变量是否为空或者为 ' '
请高手帮忙呀
------解决方案--------------------DELIMITER $$
CREATE PROCEDURE `AddTopics`($fid smallint, $author char(15), $subject char(80), $message mediumtext)
BEGIN
DECLARE $authorid mediumint;
if $message is not null and $message != ' ' then
SELECT uid INTO $authorid FROM my023bbs_members WHERE username = $author;
INSERT INTO my023bbs_threads (fid, subject, author, authorid, dateline, lastpost)
VALUES ($fid, $subject, $author, $authorid, UNIX_TIMESTAMP(), UNIX_TIMESTAMP());
UPDATE my023bbs_forums SET threads = threads + 1 WHERE fid = $fid;
INSERT INTO my023bbs_posts (fid, tid, subject, author, authorid, message, dateline)
VALUES ($fid, last_insert_id(), $subject, $author, $authorid, $message, UNIX_TIMESTAMP());
UPDATE my023bbs_forums SET posts = posts + 1 WHERE fid = $fid;
end if;
END$$
DELIMITER ;
------解决方案--------------------你可以看看这个或许会有帮助
这个命令与存储过程没什么关系吧。
其实就是告诉mysql解释器,该段命令是否已经结束了,mysql是否可以执行了。
默认情况下,delimiter是分号;。在命令行客户端中,如果有一行命令以分号结束,
那么回车后,mysql将会执行该命令。如输入下面的语句
mysql> select * from test_table;
然后回车,那么MySQL将立即执行该语句。
但有时候,不希望MySQL这么做。在为可能输入较多的语句,且语句中包含有分号。
如试图在命令行客户端中输入如下语句
mysql> CREATE FUNCTION `SHORTEN`(S VARCHAR(255), N INT)
mysql> RETURNS varchar(255)
mysql> BEGIN
mysql> IF ISNULL(S) THEN
mysql> RETURN ' ';
mysql> ELSEIF N <15 THEN
mysql> RETURN LEFT(S, N);
mysql> ELSE
mysql> IF CHAR_LENGTH(S) <=N THEN
mysql> RETURN S;
mysql> ELSE
mysql> RETURN CONCAT(LEFT(S, N-10), '... ', RIGHT(S, 5));
mysql> END IF;
mysql> END IF;
mysql> END;
默认情况下,不可能等到用户把这些语句全部输入完之后,再执行整段语句。
因为mysql一遇到分号,它就要自动执行。
即,在语句RETURN ' ';时,mysql解释器就要执行了。
这种情况下,就需要事先把delimiter换成其它符号,如//或$$。
mysql> delimiter //
mysql>