MSDN关于存储过程默认返回值

发布于 2024-08-28 02:16:47 字数 234 浏览 5 评论 0原文

谁能准确指出 MSDN 所说的如果没有错误发生,每个用户存储过程默认返回 0 的地方吗?换句话说,

IF someStatement
BEGIN
  RETURN 1
END

如果 someStatement 为 false 并且没有发生错误,我能否确定下面给出的示例代码在作为存储过程时应该始终返回零?

我知道它实际上是这样工作的,但我没有找到微软对此的任何明确声明。

Could anyone point exactly where MSDN says thet every user stored procedure returns 0 by default if no error happens? In other words, could I be sure that example code given below when being a stored procedure

IF someStatement
BEGIN
  RETURN 1
END

should always return zero if someStatement is false and no error occurs?

I know that it actually works this way, but I failed to find any explicit statement about this from Microsoft.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

恏ㄋ傷疤忘ㄋ疼 2024-09-04 02:16:47

看起来从前返回值可能有某种含义(请参阅 SQL 2000 之前的 BOL 返回值文章)在 SQL Server 2000 之前。
我检查了一下是否可以找到这些专门用于 MS SQL 的原始返回代码的列表,并找到了 以下(不过我不知道它的真实性,而且它实际上没有列出这些值)。

因此,在阅读所有这些文章后,@return_status 看起来像是一个在执行存储过程时始终返回的参数(即使您不使用它)。根据 RETURN 在线图书文章返回代码不能为空。

与存储过程一起使用时,
RETURN 不能返回空值。如果
过程尝试返回 null
值(例如,使用 RETURN
@status 当 @status 为 NULL 时),a
生成警告消息并显示
返回值 0。

运行以下 T-SQL 肯定会显示这一点,

create Procedure Test
as
begin
DECLARE @RTN integer
Return @RTN
END
GO

Exec Test
GO

Drop Procedure Test
GO

The 'Test' procedure attempted to return a status of NULL, which is not allowed. A status of 0 will be returned instead.

最终会收到答案,因为 @return_status 不能为 null(未设置时为 0,或设置为 NULL)。 。

至于假定的 BOL SQL 7.0 的文章,我的猜测是 Sybase 的旧保留...

Kris

It looks like once upon a time the return value may have meant something (see reference of pre SQL 2000 BOL return value article) prior to SQL Server 2000.
I checked around to see if I could find a listing of these original return codes specifically for MS SQL, and found the following (I don't know its authenticity though, and it doesn't actually list the values).

So, after reading all of these articles it looks like @return_status is a parameter that is ALWAYS returned when a stored procedure is executed (even if you do not use it). According to the RETURN Books online article the return code CANNOT be null.

When used with a stored procedure,
RETURN cannot return a null value. If
a procedure tries to return a null
value (for example, using RETURN
@status when @status is NULL), a
warning message is generated and a
value of 0 is returned.

Running the following T-SQL definitely shows this,

create Procedure Test
as
begin
DECLARE @RTN integer
Return @RTN
END
GO

Exec Test
GO

Drop Procedure Test
GO

You'll receive

The 'Test' procedure attempted to return a status of NULL, which is not allowed. A status of 0 will be returned instead.

In the end it looks like the answer is because @return_status cannot be null (being 0 when not set, or set to NULL)...

As for the error codes mentioned in the supposed BOL article for SQL 7.0, my guess would be an old hold over from Sybase... Sybase 5.0 Manual

Kris

够钟 2024-09-04 02:16:47

在 MSDN 上返回

编辑:

链接显示

与存储过程一起使用时,
RETURN 不能返回空值。如果
过程尝试返回 null
值(例如,使用 RETURN
@status 当 @status 为 NULL 时),a
生成警告消息并显示
返回值 0。

有人可能会说 no RETURN = RETURN NULL = RETURN 0。但是不会发出警告,因为您尚未运行 RETURN NULL。预期为零,因为它是一个存储过程。

另外,存储过程执行允许

EXEC @rtn = uspMyProc @p1...

所以必须返回一些东西,因为我们总是期望一个值,而不是 NULL

我已经依赖返回零 12 年了,即使 MSDN 没有说。以及我们更多的人:-)

RETURN on MSDN

Edit:

The link says

When used with a stored procedure,
RETURN cannot return a null value. If
a procedure tries to return a null
value (for example, using RETURN
@status when @status is NULL), a
warning message is generated and a
value of 0 is returned.

One could say that no RETURN = RETURN NULL = RETURN 0. But no warning is issued because you have not run RETURN NULL. And zero is expected because it's a stored procedure.

Also, stored proc execution allows for

EXEC @rtn = uspMyProc @p1...

So something must be returned because we always expect a value, never NULL

I've relied on zero being returned for 12 years, even if MSDN doesn't say. As well as many more of us :-)

夜雨飘雪 2024-09-04 02:16:47

如果您要在客户端中切换一些逻辑,为什么不返回“0”并使其更明确?

if you are going to switch some logic on that in your client, why not return "0" and make it more explicit?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文