Sql server 从存储过程中获取返回值为-1

Sql server 从存储过程中获取返回值为-1,sql-server,Sql Server,我试图看看我的存储过程是否可以根据if条件返回-1一些值,被截断的存储过程如下所示 CREATE PROCEDURE [StateMachine].[UpdateSnapshots] (@SystemName [nvarchar](128)= 'test_system', @IntrestingEvents VARCHAR(128)='test_event', @StateMachine_JSON [nvarchar] (max) = 'test_json', @StateMachine_

我试图看看我的存储过程是否可以根据if条件返回-1一些值,被截断的存储过程如下所示

CREATE PROCEDURE [StateMachine].[UpdateSnapshots] 


(@SystemName [nvarchar](128)= 'test_system',
@IntrestingEvents VARCHAR(128)='test_event',
@StateMachine_JSON [nvarchar] (max) = 'test_json',
@StateMachine_Object [nvarchar] (max)='test_object',
@CrossbarRouter VARCHAR(128) = 'text_xbar1') AS

BEGIN

DECLARE

@SystemNameValue [nvarchar](128),
@CrossbarValue [nvarchar](128),
@IsCrossbarNotNull int,
@IsCrossbarNull int,
@CrossbarLock int

select @SystemNameValue=count(systemname) from [StateMachine].[Snapshots] with (nolock) where SystemName=@SystemName



IF @SystemNameValue=0
BEGIN 
INSERT INTO [StateMachine].[Snapshots] (SystemName, IntrestingEvents, StateMachine_JSON, StateMachine_Object, CrossbarRouter) VALUES (@SystemName, @IntrestingEvents, @StateMachine_JSON, @StateMachine_Object, @CrossbarRouter)
END

select @CrossbarValue = isnull(count(CrossbarRouter),0) from [StateMachine].[Snapshots] with (nolock) where (CrossbarRouter=@CrossbarRouter and SystemName = @SystemName) 
select @IsCrossbarNotNull = isnull(count(CrossbarRouter),0) from [StateMachine].[Snapshots] with (nolock) where CrossbarRouter is not null and SystemName = @SystemName
select @IsCrossbarNull = isnull(count(CrossbarRouter),0) from [StateMachine].[Snapshots] with (nolock) where CrossbarRouter is null and SystemName = @SystemName
select @CrossbarLock = isnull(count(CrossbarRouter),0) from [StateMachine].[Snapshots] with (nolock) where (CrossbarRouter != @CrossbarRouter and SystemName = @SystemName) 

IF @CrossbarLock = 1
    RETURN -1



IF (@CrossbarValue = 0 and @IsCrossbarNotNull = 0) OR @IsCrossbarNull = 0
BEGIN
UPDATE [StateMachine].[Snapshots] SET IntrestingEvents=@IntrestingEvents, StateMachine_JSON=@StateMachine_JSON, StateMachine_Object=@StateMachine_Object, CrossbarRouter = @CrossbarRouter WHERE SystemName=@SystemName;
END
IF @CrossbarValue = 1 
BEGIN
UPDATE [StateMachine].[Snapshots] SET IntrestingEvents=@IntrestingEvents, StateMachine_JSON=@StateMachine_JSON, StateMachine_Object=@StateMachine_Object WHERE SystemName=@SystemName and CrossbarRouter=@CrossbarRouter;
END
END

GO
GRANT EXECUTE ON [StateMachine].[UpdateSnapshots] TO sp_executor
如果我试图这样做,我会得到以下错误

具有返回值的RETURN语句不能在此上下文中使用


谢谢

我建议您的存储过程可能存在另一个与
RETURN
语句的使用不直接相关的问题。也许查看代码的其余部分会很有用?您确定它位于特定的
RETURN
语句中吗?你能显示整个存储过程吗?当然,我可以显示整个存储过程。谢谢你的编辑。这段代码适合我,我似乎无法重现你的错误