表a中有个字段title
调用一个存储过程参数title='一二三',
向表中插入记录前先检察如果数据库中以经有title为'一二三'的记录则停止操作返回给调用程序提示有相同记录。如果没有相同的记录则插入新记录。这样的存储过程怎么写呀?

解决方案 »

  1.   

    create PROCEDURE p_setTitle
    @title varchar(200)
    as 
    if not exist(select * from table1 where title='一二三')
    begin
        insert into talbe1 (title)
        values (@title)
    end
    go
      

  2.   

    Sorry:
    create PROCEDURE p_setTitle
    @title varchar(200)
    as 
    if not exist(select * from table1 where title=@title)
    begin
        insert into talbe1 (title)
        values (@title)
    end
    go
      

  3.   

    使用函数不就行了!
    当有重复记录,返回false;
    插入记录成功,就返回true.
      

  4.   

    增加一个返回值不就可以判断添加结果了吗
    create PROCEDURE p_setTitle
    @title varchar(200)
    as 
    if not exist(select * from table1 where title=@title)
    begin
        insert into talbe1 (title) values (@title)
        return(0) --成功
    end
    else
    begin
        return(1) --数据重复
    end
    if @@error > 0
    begin
        return(2) --失败
    end
    go
      

  5.   

    if not exist(select * from table1 where title='一二三')
    begin
        insert into talbe1 (title)
        values (@title)
    end