如何调用存储过程并返回值?
嘿大家, 我有一个存储过程,我需要在另一个存储过程中调用它,但我希望第一个存储过程返回一个值(字段值)。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你说
@alphaVar
是varchar(10)
。在这种情况下,您需要使用如下输出参数。Return
只能用于存储过程中的整数类型。或者,您可以使用标量 UDF 而不是存储过程。
You say
@alphaVar
isvarchar(10)
. In that case you need to use an output parameter as below.Return
can only be used for integer types in stored procedures.Alternatively you could use a scalar UDF rather than a stored procedure.
你调用的语法是错误的。
请参阅参考中的EXECUTE。
You're calling syntax is wrong.
See EXECUTE in the reference.
试试这个:
Try this:
获取此代码的解决方法是在
Management Studio
本身中执行存储过程并复制SQL
代码A work around of getting this code is to execute the stored procedure in your
Management Studio
itself and copy theSQL
code