请问如何在插入一条记录时防止ID号重复 ?
日期:2007-05-10 荐:
请问如何在插入一条记录时防止ID号重复 ?请问如何在插入一条记录时,防止ID号重复 ?它自己會出錯把id设为标识列把它設計成主鍵,它不會重復。或者create table tb(id int identity(1,1),item varchar(100)....)--==Create User :talantlee--==Create Date:2005/03/19--===modify by talntlee 2005/02/24--==Use:插入一個數據--==Exec Insert_Student 99,'','',2CREATE PROCEDURE Insert_Student@Sno varchar(50),@Sname nvarchar(50),@Ssex nvarchar(50),@Sage varchar(50)asif exists(select * from Student where sno=@sno)--update student set Sname=@sname,Ssex=@ssex,sage=@sage where sno=@sno--假如你想存在就更新得話,取消注釋print '已經存在id號'elseinsert into Student(sno,sname,ssex,sage) values(@Sno,@Sname,@Ssex,@Sage)GO參照declare @id intset @id='10'if exists(select 1 from tablename where id=@id) print 'this id is exists!'insert into table(id) values (@id)设置ID为自增长,添加时不要设置ID的值即可create table tb(id int identity(1,1)...)或把它設計成主鍵
标签: