在SQL Server中从表中删除一行,并为每个条件设置消息



我已经设置了@messagevar,我想根据条件设置这些消息,但每当我执行此代码时,它只返回一条消息,我设置了

@Message = 'Please provide Topic Name and Topic ID' 

请帮助我-提前感谢

ALTER PROCEDURE DeleteTopicNameWebAPI
@InstituteID bigint = 0 ,
@SubjectID bigint = 0,
@TopicName nvarchar(200) = null,
@TopicID bigint = 0
AS
BEGIN
DECLARE @Message nvarchar(200)

IF EXISTS (SELECT 1 FROM TopicMaster 
WHERE TopicID = @TopicID AND TopicName = @TopicName AND InstituteID = @InstituteID)
BEGIN               
SET @Message = 'Topic name is present in a system'
END
ELSE
BEGIN
SET @Message='Topic Name is not present in a system with Topic ID :'+' '+ Convert(nvarchar (20),@TopicID) + ' And Topic Name :' +' '+@TopicName;
END

IF @TopicID = 0 AND @TopicName IS NULL
BEGIN           
DELETE FROM TopicMaster 
WHERE InstituteID = @InstituteID  
AND CategoryID = @SubjectID  
AND TopicName = @TopicName 
AND TopicID = @TopicID;
SET @Message ='Done'
END
ELSE
BEGIN
SET @Message = 'Please Provide Topic Name and Topic ID. '
END

SELECT @Message AS [Message]
END

您正在覆盖消息。您应该将其附加到:

declare @message nvarchar(max) = '';
set @message += 'Now hear this.';
set @message += 'I am hungry.';

如果成功的话,可能会打印出来,如果不成功,可能会扔掉。不要select它:

print (@message); // or
throw 50000, @message, 1;

最新更新