如何调用存储过程并返回值?

发布于 2024-10-09 19:34:21 字数 628 浏览 0 评论 0原文

嘿大家, 我有一个存储过程,我需要在另一个存储过程中调用它,但我希望第一个存储过程返回一个值(字段值)。

CREATE PROCEDURE rnd_STR
(
    @Length int

)

@alphaVar varchar(10) OUTPUT
AS
SET @alphaVar = 'blah'

 #procedure body
END
GO

DECLARE @alphaVar varchar(10)

EXEC rnd_STR @alphaVar output

SELECT @alphaVar

错误

消息 102,级别 15,状态 1,过程 rnd_STR,第 6 行

“@alphaVar”附近的语法不正确。

消息 137,级别 15,状态 1,过程 rnd_STR,第 8 行

必须声明标量变量“@alphaVar”。

消息 2812,级别 16,状态 62,第 4 行

找不到存储过程“rnd_STR”。

(1 行受影响)

不起作用!

我该怎么称呼它?

顺便说一句,返回的@ID是一个字符串

Hey all,
I have a stored procedure and I need to call it within another stored procedure, but I want the first one to return a value (field value).

CREATE PROCEDURE rnd_STR
(
    @Length int

)

@alphaVar varchar(10) OUTPUT
AS
SET @alphaVar = 'blah'

 #procedure body
END
GO

DECLARE @alphaVar varchar(10)

EXEC rnd_STR @alphaVar output

SELECT @alphaVar

ERRORS

Msg 102, Level 15, State 1, Procedure rnd_STR, Line 6

Incorrect syntax near '@alphaVar'.

Msg 137, Level 15, State 1, Procedure rnd_STR, Line 8

Must declare the scalar variable "@alphaVar".

Msg 2812, Level 16, State 62, Line 4

Could not find stored procedure 'rnd_STR'.

(1 row(s) affected)

didn't work !!

How can I call it??

BTW, the returned @ID is a string

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

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

发布评论

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

评论(4

我们的影子 2024-10-16 19:34:21

你说@alphaVarvarchar(10)。在这种情况下,您需要使用如下输出参数。 Return 只能用于存储过程中的整数类型。

CREATE PROCEDURE rnd_STR
@Length int,
@alphaVar varchar(10) OUTPUT    
AS
BEGIN
SET @alphaVar = 'blah'
/* Rest of procedure body*/
END

GO

DECLARE @alphaVar varchar(10) 

EXEC rnd_STR 10, @alphaVar output

SELECT @alphaVar

或者,您可以使用标量 UDF 而不是存储过程。

You say @alphaVar is varchar(10). In that case you need to use an output parameter as below. Return can only be used for integer types in stored procedures.

CREATE PROCEDURE rnd_STR
@Length int,
@alphaVar varchar(10) OUTPUT    
AS
BEGIN
SET @alphaVar = 'blah'
/* Rest of procedure body*/
END

GO

DECLARE @alphaVar varchar(10) 

EXEC rnd_STR 10, @alphaVar output

SELECT @alphaVar

Alternatively you could use a scalar UDF rather than a stored procedure.

橘亓 2024-10-16 19:34:21

你调用的语法是错误的。

 DECLARE @newId int
 EXEC @newId = rnd_STR, @length = 10

请参阅参考中的EXECUTE

You're calling syntax is wrong.

 DECLARE @newId int
 EXEC @newId = rnd_STR, @length = 10

See EXECUTE in the reference.

岛歌少女 2024-10-16 19:34:21

试试这个:

EXEC @alphaVar = rnd_STR 10

Try this:

EXEC @alphaVar = rnd_STR 10
长伴 2024-10-16 19:34:21

获取此代码的解决方法是在 Management Studio 本身中执行存储过程并复制 SQL 代码

A work around of getting this code is to execute the stored procedure in your Management Studio itself and copy the SQL code

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