本文转自:http://www.cnblogs.com/302soft/archive/2007/01/09/615667.html
例如在向数据库添加新数据时,需要检测是否有重复本例介绍如何把这个检测的过程放在存储过程中,并用程序调用检测的结果做出反应。存储过程如下:
CREATE PROCEDURE DInstitute_Insert
@InstituteNO nvarchar(6),@InstituteName nvarchar(40)
AS
declare @return int,@count int
if(ltrim(rtrim(@InstituteName))='' or ltrim(rtrim(@InstituteNO))='')
select @return=3--返回3表示提交的数据有空值
else
begin
select @count=count(1) from DInstitute where InstituteNO=@InstituteNO
if(@count>0)
select @return=1--返回1表示编号有重复
else
begin
insert into DInstitute (InstituteNO,InstituteName) values (@InstituteNO,@InstituteName)
if(@@error>0)
select @return=2--返回2表示数据操作错误
else
select @return=0--返回0表示数据操作成功
end
end
return @return
GO