不用存储过程也只需要一个.INSERT INTO tablename (key, otherfields,...) values(keyvalue,othervalues,...)
WHERE NOT EXISTS(SELECT key FROM tablename WHERE key=keyvalue)

解决方案 »

  1.   

    to:xs8577
    插入之前进行的判断
    防止出现重复的情况to xuyugen
    存储过程是不是就是把判断和插入,放在一起,实际上存储过程的内部也是先进行一个查询的判断,然后再进行一个插入的操作?
      

  2.   


    给你看个存储过程,就明白了
    CREATE proc add_about
    (
      @Name varchar(50),
      @iReturn int output
    )
    asselect * from abouts where WebName=@Name
    if(@@rowcount>0)
    begin
     set @iReturn=0 --该值已存在,插入失败
    end
    else
    begin
    insert into abouts values(@Name)
    set @iReturn=1 --插入成功
    end
    GO
      

  3.   

    打开一次SQLCONNECTION也可以啊
    先执行判断SQL,再根据结果执行插入SQL
      

  4.   

     using (SqlConnection conn = new SqlConnection(SqlHelper.DBConnectionString
                {
                   string pd="select ID from tb where ID=3";
                   if(SqlHelper.ExecuteScalar(conn, CommandType.Text,pd)!=null)
                   {return -1;}
                    else
                   {
                    return SqlHelper.ExecuteNonQuery(conn, CommandType.Text, 插入SQL);
                    }
                }
    这样时要两此打开,但是这种方式还是可取的,
    当然你可以写成一条SQL语句,就是判断和插入SQL语句组合在一起,比如:
    if exists (select FormID from lcFormStyle where FormID =2)
     insert into lcFormStyle ...
    GO