use Db_sns;
DROP PROCEDURE IF EXISTS`pet_stat`;
CREATE PROCEDURE `pet_stat` (uid int)
BEGIN
DECLARE num int default 0;
select `count` into num from `k7_pet_stat` where `uid`=uid;
if num>0 then
    update `k7_pet_stat` set `count`=`count`+1 where `uid`=uid;
else
    insert into `k7_pet_stat`(`uid`, `count`) values(uid, 1);
end if ;
END代码如上,我的目的是在k7_pet_stat中先判断传入的uid是否存在,存在即把对应的count+1,不存在则插入一个新记录,count为1。
现在的情况很怪异,表为空的时候,第一次入记录是正确的,但是此后,不管uid为何值,都会把之前那个记录对应的count+1,表中始终保持一条记录存在求高手指教,谢谢~

解决方案 »

  1.   

    where `uid`=uid;
    这儿你使用了相同的变量名和字段名,这样,MYSQL会全部以为是字段名。把变量名改一下即可。DROP PROCEDURE IF EXISTS`pet_stat`;
    CREATE PROCEDURE `pet_stat` (v_uid int)
    BEGIN
    DECLARE num int default 0;
    select `count` into num from `k7_pet_stat` where `uid`=v_uid;
    if num>0 then
       update `k7_pet_stat` set `count`=`count`+1 where `uid`=v_uid;
    else
       insert into `k7_pet_stat`(`uid`, `count`) values(v_uid, 1);
    end if ;
    END
      

  2.   

    mysql 存储过程的怪异问题[ :50分]
    xinglu1983 
      '截至2011-11-07 16:07:36  正常结帖:0  当您的问题得到解答后请及时结贴.
    http://topic.csdn.net/u/20090501/15/7548d251-aec2-4975-a9bf-ca09a5551ba5.html
    http://topic.csdn.net/u/20100428/09/BC9E0908-F250-42A6-8765-B50A82FE186A.html
    http://topic.csdn.net/u/20100626/09/f35a4763-4b59-49c3-8061-d48fdbc29561.html8、如何给分和结贴?
    http://community.csdn.net/Help/HelpCenter.htm#结帖
      

  3.   


    多谢,没想到是这个原因,这点感觉mysql挺不人性的,sqlserver存储过程变量前面有个@符号就可以很轻易的区分开来了。