CREATE PROCEDURE `AddTopics`($fid smallint, $author char(15) character set gbk, $subject char(80)  character set gbk, $message mediumtext  character set gbk)
BEGIN
//-----------------------------下面这个IF语句是新加的
  IF (select count(*) from my023bbs_threads where fid=$fid and subject=$subject)>0 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;//--------------这里是匹配新加的 IF
END加了那个IF  END IF 就出错,存储过程无法保存,太不可思议了

解决方案 »

  1.   

    不是太难用,是你不会用
    delimiter //
    CREATE PROCEDURE `AddTopics`($fid smallint, $author char(15) character set gbk, $subject char(80) character set gbk, $message mediumtext character set gbk)
    BEGIN
      declare $cnt int(11);
      DECLARE $authorid mediumint;
      select count(*) from my023bbs_threads where fid=$fid and subject=$subject into $cnt;
      IF $cnt>0 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 ;