从存储过程中调用存储过程并返回记录集

Calling a Stored Proc from within a Stored Proc and returning a recordset(从存储过程中调用存储过程并返回记录集)
本文介绍了从存储过程中调用存储过程并返回记录集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个回滚一系列操作的存储过程.我想从另一个 SP 中调用它.

I have a Stored Procedure that rolls-back a series of operations. I want to call this from within another SP.

问题是内部 SP 返回一个记录集,其中包含一个指示成功程度的值.

The problem is that the inner SP returns a record set with a single value that indicates the degree of success.

这种方法运行良好并且在我们的上下文中具有一些优势,但回想起来,我会使用返回值或输出参数以传统方式完成它.

This approach worked well and has some advantages in our context, but in retrospect, I would have done it the conventional way with a Return value or an Output parameter.

可以总是改变这个 SP 以使用这种方法并修改调用代码,但是 a) 我不想涉足任何不必要的代码,并且 b) 在智力水平,我很想知道可能有什么替代解决方案,如果有的话.

I could always change this SP to use this approach and modify the calling code, but a) I don't want to dabble with any more code than I have to, and b) at an intellectual level, I'm curious to see what alternative solution there may be, if any.

我如何(如果有的话)调用这个 SP 并确定返回的单例记录集的值?

How (if at all) can I call this SP and determine the value of the singleton recordset returned?

谢谢

推荐答案

一个存储过程像其他任何一样返回一个记录集,所以你实际上可以这样做:

A stored procedure returns a record set like any other, so you can actually do this:

插入 MyTable (我的价值)

INSERT INTO MyTable ( MyValue )

EXEC dbo.MyStoredProcedure

EXEC dbo.MyStoredProcedure

EXEC 代替了 SELECT 语句.要获取该值,只需从您插入的表中选择即可.通常,这将是一个临时表.

The EXEC takes the place of a SELECT statement. To get the value, just SELECT from the table you inserted into. Typically, this would be a temp table.

这篇关于从存储过程中调用存储过程并返回记录集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

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

相关文档推荐

Execute complex raw SQL query in EF6(在EF6中执行复杂的原始SQL查询)
SSIS: Model design issue causing duplications - can two fact tables be connected?(SSIS:模型设计问题导致重复-两个事实表可以连接吗?)
SQL Server Graph Database - shortest path using multiple edge types(SQL Server图形数据库-使用多种边类型的最短路径)
Invalid column name when using EF Core filtered includes(使用EF核心过滤包括时无效的列名)
How should make faster SQL Server filtering procedure with many parameters(如何让多参数的SQL Server过滤程序更快)
How can I generate an entity–relationship (ER) diagram of a database using Microsoft SQL Server Management Studio?(如何使用Microsoft SQL Server Management Studio生成数据库的实体关系(ER)图?)