该文章来自:http://www.cleardata.biz/articles/storedproc.aspx
By Bill Graziano (3/20/2002)
This article discusses three common ways to return data from stored procedures: OUTPUTing variables, temp tables and the RETURN statement. This article was originally published on SQLTeam.com. All the examples in this article use the pubs database and MS SQL Server 7. Everything in this article also works in SQL Server 2000. SQL Server does give you some additional options which I'll mention in the article.
Using OUTPUT variables
The first approach uses OUTPUT variables. This will allow us to return values (but not record sets in SQL7) back to the calling procedure. First I'll need a stored procedure:
CREATE PROCEDURE GetStateCount ( @State char(2), @StateCount int OUTPUT )
AS
SELECT @StateCount = Count(*)
FROM authors
WHERE State = @State
go
The OUTPUT statement indicates that @StateCount
can be used to return a value back to the calling procedure. Whatever value we put into @StateCount
will be passed back to the calling program. All this stored procedure does is count the number of authors from a given state.
The SQL to call this looks like this:
DECLARE @TheCount int
EXEC GetStateCount
@State = 'UT',
@StateCount = @TheCount OUTPUT
Select TheCount = @TheCount
We use the EXEC
command to call a stored procedure. I always specify the name of the arguements as I pass them. Notice that I also had to include the OUTPUT
clause in the EXEC statement. After the EXEC statement runs, @TheCount should contain the value from @StateCount inside the stored procedure. The output should show that there are two authors from Utah and it looks like this:
TheCount
-----------
2
(1 row(s) affected)
Record Sets
Let's suppose you wanted the stored procedure to return a list of all the authors in a given state. In SQL Server 2000 you can experiment with the TABLE datatype. In SQL Server 7 we'll stick with temp tables. The basic approach is to build a temp table, call a stored procedure to populate it, and then do the processing. My script on the outside looks like this:
CREATE TABLE #Authors (au_id char(11))
INSERT #Authors
EXEC GetStateAuthors @State='UT'
SELECT *
FROM #Authors
DROP TABLE #Authors
This is a variation on using SELECT to INSERT records into a table. The GetStateAuthors
procedure looks like this:
CREATE PROCEDURE GetStateAuthors ( @State char(2) )
AS
SELECT au_id
FROM authors
WHERE State = @State
go
Any procedure that returns a record set can be handled like this. I use this quite a bit when dealing with packaged applications. In many cases they use stored procedures to return record sets to screens. I call their procedures, capture the output in my scripts and handle it there. Just make sure your temporary table and the SELECT statement match.
Using Return
The last way to get data back from a stored procedure is also the most limiting. It only returns a single numeric value. This is most commonly used to return a status result or error code from a procedure. Consider this procedure:
CREATE PROC TestReturn (@InValue int)
AS
Return @Invalue
go
All it does is use the RETURN statement to send back the value that was passed in. Note that executing a RETURN statement causes a stored procedure to stop executing and return control back to the calling program. This is often used to test for error conditions and stop processing if one is found. The following script calls the TestReturn
stored procedure:
Declare @ReturnValue int
EXEC @ReturnValue = TestReturn 3
Select ReturnValue=@ReturnValue
and the output looks like this:
ReturnValue
-----------
3
(1 row(s) affected)
Whatever number is returned using the RETURN statement is put into the variable @ReturnValue.
That's the three best way I know of to get data from a stored procedure back to a calling stored procedure. Enjoy!
分享到:
相关推荐
1. **明确横切关注点**:在使用@AfterReturning 之前,确保你已经识别出了需要在目标方法执行后处理的横切关注点,如日志记录、性能监控或事务管理。 2. **合理定义切入点**:使用切入点表达式精确地定义哪些方法...
《深入理解FindBugs:可能通过返回暴露内部表示——“NULL”问题分析》 在软件开发过程中,确保代码的质量和安全至关重要。FindBugs作为一款静态代码分析工具,能够帮助开发者在程序运行前发现潜在的问题。...
python库。 资源全名:django_pg_returning-1.0.0-py2.py3-none-any.whl
Oracle的RETURNING子句是数据库操作中的一个强大特性,它允许在执行INSERT、UPDATE或DELETE语句时直接获取处理的数据结果。这在处理事务时非常有用,尤其是当你需要基于新插入或更新的数据进行进一步操作时。 1. **...
Spring AOP 定义 AfterReturning 增加实例分析 Spring AOP (面向方面编程) 是 Spring 框架中的一种编程范式,旨在将切面编程融入到应用程序中。AfterReturning 是 Spring AOP 中的一种Advice (通知),它在目标方法...
"attr_copy.rar_The_Returning"这个标题暗示我们关注的是一个与复制文件属性相关的程序或函数,可能是一个C语言编写的源代码文件,即"attr_copy.c"。这个程序的功能是复制一个属性列表,并返回所占用的字节数。 `...
Sub procedures are used for performing tasks without returning a value. ##### 6. Function Procedures Function procedures return a value after executing. ##### 7. Standard Module A standard module is...
Dec 17 13:59:26 localhost kernel: PAL Shim AllocRequest returning NULL Dec 17 13:59:26 localhost kernel: PAL Shim AllocRequest returning NULL Dec 17 13:59:26 localhost kernel: PAL Shim AllocRequest ...
在本篇博文中,我们将深入探讨Spring AOP(面向切面编程)中的一个重要概念——AfterReturning通知,这是Spring框架提供的一种强大的功能,允许我们在方法成功执行并返回后执行额外的操作。我们将通过源码分析和实际...
9.4小节“Returning Updated Rows”讨论了在执行Model操作后如何获取更新后的行。在执行涉及Model子句的DML操作时,我们可能需要查看或验证哪些行被修改了,或者新状态是什么。通常,SQL更新操作不会返回更新的行,...
- **2.5 Returning from Leaf to Root 从叶子节点返回到根节点**:讨论如何在搜索树中从一个叶子节点回到根节点,这对于更新路径上的节点非常有用。 - **2.6 Dealing with Nonunique Keys 处理非唯一键**:介绍如何...
"An Eco-tour of returning to nature.zip"这个压缩包文件为我们提供了一份关于生态旅游的详细资料,引领我们踏入一场回归自然的探索之旅。 首先,生态旅游(Eco-tourism)是一种尊重环境、保护生物多样性和促进...
Bug with returning an incorrect value in the RowsAffected property is fixed InterBase data provider Now RowsAffected returns a correct value without the need to prepare a statement explicitly Bug ...
% varargout cell array for returning output args (see VARARGOUT); % hObject handle to figure % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and ...
Aliases handling in the RETURNING clause is supported The WireCompression connection parameter for Firebird 3 is supported Bug with using BLOB data type in batch operations is fixed Bug with the ...
Aliases handling in the RETURNING clause is supported The WireCompression connection parameter for Firebird 3 is supported Bug with using BLOB data type in batch operations is fixed Bug with the ...
去-htmltopdf 转到 wkhtmltopdf 的绑定 - 使用 Webkit 将 HTML 转换为 PDF用法 package mainimport "github.com/pleximus/go-htmltopdf"converter := html2pdf . New ()// Converting HTML data and returning data ...
Aliases handling in the RETURNING clause is supported The WireCompression connection parameter for Firebird 3 is supported Bug with using BLOB data type in batch operations is fixed Bug with the ...