您好我有一个包含四列的表格。我需要避免插入重复的服务名称 对于特定的ServerId。但我不想检查说明。两个或三个服务,特别是具有相同描述名称的ServerId。
表格: -
Id int,
ServerId int,
ServiceName varchar(200),
Description varchar(400)
步骤: -
CREATE procedure sp_UpdateServices
(
@id int
,@ServerId int
,@ServiceName varchar(500)
,@Description varchar(400)
,@ReturnMessage nvarchar(500)= null output
)
as
BEGIN
BEGIN TRY
If Exists(SELECT servicename from tbl_servicename where
servicename=@ServiceName AND
serverid=@ServerId )
RAISERROR('Service Name Already Exists',16,1)
BEGIN transaction
Update tbl_servicename SET servicename=@ServiceName , description=@Description
where serverid=@ServerId and id=@id
COMMIT Transaction
END TRY
BEGIN CATCH
if(@@trancount>0)
rollback tran
SELECT @ReturnMessage=ERROR_MESSAGE()
select @ReturnMessage as ReturnMessage
END CATCH
End
输出: -
id serverid servicename description
379 218 test1 test1
385 218 test2 test1
问题: -
如果我尝试为服务名称test1和test1保存描述test1,则会出现错误。