if exists(select * from 表 where 主键=@要查询的主键)
  print '已经存在'
else
  insert 表 values(....)

解决方案 »

  1.   

    create procedure sp_1 @pk varchar(30)
    as
    if not exists(select pk from t)
      insert into t(pk) values(@pk)
    go
      

  2.   

    create proc p_insert 
    @主键 int
    as
    begin tran
    if not exists(select * from 表 where 主键=@主键)
    insert 表 values(....)
    if @@error=0
    begin
    commit tran
    return 0
    end
    else
    begin
    rollback tran
    return 1
    end
    go--调用
    declare @re int
    exec @re=p_insert @主键=123
    if @re=1 
      print '插入失败'
    else
      print '插入成功'
      

  3.   

    我在jsp中要怎么调用返回值啊?