數據庫表結構
《1》新增數據,并且按照"name" 字段查詢,如果重復返回“error”=-100 ,如果成功返回ID,如果失敗ID=0
USE [數據庫]GOSET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOALTER PRocedure [dbo].[存儲過程名稱]( @name varchar(50), @state int, @capbility int, @ip varchar(50), @port int)AS SET NOCOUNT ON; BEGIN declare @count int; declare @id int; --判斷此服務器是否已經注冊 select @count=COUNT(*) from serverlist where name =@name; if(@count>0)--此服務器已經注冊過 BEGIN select "ERROR" = -100; --此服務器已經注冊過 END else BEGIN insert into serverlist(name,[state],capbility,ip,port) values (@name,@state,@capbility,@ip,@port);select @@identity; select @id; END END
注意,代碼執行此存儲過程中,只查詢表中第一個字段即可,string id=表.rows[0][0].tostring().trim();
《2》 修改數據,依然查詢詞數據是否已經存在
USE [數據庫]GOSET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOALTER procedure [dbo].[存儲過程名稱]( @name varchar(50), @state int, @capbility int, @ip varchar(50), @port int, @id int)AS SET NOCOUNT ON; BEGIN declare @count int; declare @return int; select @count=COUNT(*) from serverlist where name =@name and ID<>@id;if(@count>0)BEGINselect "error"=-100; end beginselect @count=COUNT(*) from serverlist where id =@id if(@count>0) beginupdate serverlist set name=@name,[state]=@state,capbility=@capbility,ip=@ip,port=@port where ID=@id;set @return = 1; end else beginset @return = 0; endend select @return; END
新聞熱點
疑難解答