Returning Data from Stored Procedures

该文章来自: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!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
AOP(Aspect-Oriented Programming)是面向切面编程的一种编程范式,通过在程序的不同阶段植入通用的切面逻辑,使得这些逻辑可重用并可以在不同的模块中引用。其中,aop returning是AOP的一个关键方面,用于在方法执行成功返回之后执行的操作。 aop returning是在被通知的方法成功返回结果之后,执行一些附加操作的一种机制。通常,这些附加操作包括日志记录、性能监测、事务管理等。通过aop returning,我们可以将这些通用操作与原始方法逻辑解耦,并将它们封装在切面中。 在使用aop returning时,我们首先需要定义一个切面(Aspect),其中包含切入点(Pointcut)和通知(Advice)。切入点决定了哪些方法会被植入切面逻辑,通知则定义了方法执行前、后以及返回时的具体操作。 在aop returning中,通知类型为返回通知(AfterReturning Advice)。当被通知的方法成功执行并返回结果时,返回通知即会触发。我们可以在返回通知中编写所需的逻辑,比如记录返回结果、日志输出或是检查业务逻辑等。 通过使用aop returning,我们可以集中处理各个方法执行成功后的附加操作,避免了在每个方法中显式编写这些逻辑的重复性工作。这样可以提高代码的可维护性、可读性和代码复用程度。 总而言之,aop returning是AOP编程中用于在方法执行成功返回之后执行的一种机制。通过使用aop returning,我们可以在方法返回后执行一些附加操作,提高代码的可维护性和代码复用程度,并有效地解耦原始方法和通用操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值