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

How to call a stored procedure and return a value?(如何调用存储过程并返回值?)
本文介绍了如何调用存储过程并返回值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

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 是一个字符串

推荐答案

你说 @alphaVarvarchar(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.

这篇关于如何调用存储过程并返回值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Number of working days between two dates(两个日期之间的工作日数)
How do I use dateadd to get the first day of last year?(如何使用 dateadd 获取去年的第一天?)
SQL- Count occurrences of a specific word within all stored procedures(SQL- 计算所有存储过程中特定单词的出现次数)
SQL query to make a column of numbers a string(使一列数字成为字符串的 SQL 查询)
T-SQL: Best way to replace NULL with most recent non-null value?(T-SQL:用最新的非空值替换 NULL 的最佳方法?)
Count days in date range with set of exclusions which may overlap(使用一组可能重叠的排除项计算日期范围内的天数)