我有一积分表,字段有“用户名”和“积分”
我向其中插入积分,判断如果用户名已存在,就更新积分,如果不存在就插入新的记录
这个SQL语句我应该怎么写啊?

解决方案 »

  1.   

    if exists (select * from table where id=@id)
    update ...
    else
    insert...
      

  2.   

    if exists (select * from table where yonghuming=@yonghuming) 
    begin 
     //update
    end
    else 
    begin 
     //insert
    end
      

  3.   

    根据用户名查询积分表select count(*) from table where username = 用户名;
    对这个结果进行判断
      

  4.   

    create proc updateuser(
       @userid int,@integral int
    )as 
    declare @usercount int
    select @usercount=count(*) from tablename where userid=@uesrid
    if @usercount=0
    begin
       insert .......
    end
    else
    begin
       update......
    end
      

  5.   

    create proc updateuser( 
      @userid int,@integral int 
    )as 
    declare @usercount int 
    select @usercount=count(*) from tablename where userid=@uesrid 
    if @usercount=0 
    begin 
      insert ....... 
    end 
    else 
    begin 
      update...... 
    end
      

  6.   

    5楼的存储过程 基本上表达出意思了
    不过不知道你的积分是否是在存在的基础上面累加的  
    create proc updateuser(
      @userid int,@integral int
    )as
    declare @usercount int
    select @usercount=count(*) from tablename where userid=@uesrid
    if @usercount=0
    begin
      insert .......
    end
    else
    begin
      update tablename set jifen=jifen+integral where 条件 
    end