我想写个储存过程  大概功能是:  有一个char参数,先判断表中有没和这个参数一样的记录,有的话什么都不做,没得话就插入我知道select count(*)能知道返回记录数
但怎么把数值给变量  然后判断是否大于0
 还有SELECT COUNT(*) 会返回结果  怎么让他别返回结果  我只是要用那个数值来判断是否已有记录

解决方案 »

  1.   

    直接select into 
    如果有了的话,会主键冲突,然后捕捉错误不就好了哇
      

  2.   

    declare num int default 0;
    select count(*) into num from tb ;
      

  3.   

    直接INSERT INGORE 就行了。 错了就不插入。
    If you use the IGNORE keyword, errors that occur while executing the INSERT statement are treated as warnings instead. For example, without IGNORE, a row that duplicates an existing UNIQUE index or PRIMARY KEY value in the table causes a duplicate-key error and the statement is aborted. With IGNORE, the row still is not inserted, but no error is issued. IGNORE has a similar effect on inserts into partitioned tables where no partition matching a given value is found. Without IGNORE, such INSERT statements are aborted with an error; however, when INSERT IGNORE is used, the insert operation fails silently for the row containing the unmatched value, but any rows that are matched are inserted. For an example, see Section 17.2.2, “LIST Partitioning”. Data conversions that would trigger errors abort the statement if IGNORE is not specified. With IGNORE, invalid values are adjusted to the closest values and inserted; warnings are produced but the statement does not abort. You can determine with the mysql_info() C API function how many rows were actually inserted into the table. 
      

  4.   

    insert ingore,满足楼主需求,
    如果插入一条数据,在数据库中已经有了,现在想做的是update操作,怎么搞?
      

  5.   

    insert ignore  楼上打错了ignore~~~,这种方法满足楼主需求
    如果原表中有,现在需要的是更新为现在的数据,没有就插入,该怎么做~~
      

  6.   

    只是这个需求的话,确实上面的各位提到的都可以,若是再更新的话 也可以用 INSERT.... ON UPDATE...子句SET iVar_total=0;
    SELECT COUTN(*) INTO iVar_total FROM tablename WHERE ...;
    #****************************************#
    MySQL技术及运维自动化网:www.mysqlops.com新浪微博账号:http://weibo.com/mysqlops
    #****************************************#