问题描述
大家好,我有一个存储过程,我需要在另一个存储过程中调用它,但我希望第一个返回一个值(字段值).
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
错误
消息 102,级别 15,状态 1,过程 rnd_STR,第 6 行
Msg 102, Level 15, State 1, Procedure rnd_STR, Line 6
'@alphaVar' 附近的语法不正确.
Incorrect syntax near '@alphaVar'.
消息 137,级别 15,状态 1,过程 rnd_STR,第 8 行
Msg 137, Level 15, State 1, Procedure rnd_STR, Line 8
必须声明标量变量@alphaVar".
Must declare the scalar variable "@alphaVar".
消息 2812,级别 16,状态 62,第 4 行
Msg 2812, Level 16, State 62, Line 4
找不到存储过程rnd_STR".
Could not find stored procedure 'rnd_STR'.
(1 行受影响)
没用!!
我怎么称呼它??
顺便说一句,返回的@ID 是一个字符串
推荐答案
你说 @alphaVar
是 varchar(10)
.在这种情况下,您需要使用如下输出参数.Return
只能用于存储过程中的整数类型.
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
或者,您可以使用标量 UDF 而不是存储过程.
Alternatively you could use a scalar UDF rather than a stored procedure.
这篇关于如何调用存储过程并返回值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!