是否可以在 SQL Server 2008 中使用存储过程作为子查询?

Is it possible to use a Stored Procedure as a subquery in SQL Server 2008?(是否可以在 SQL Server 2008 中使用存储过程作为子查询?)
本文介绍了是否可以在 SQL Server 2008 中使用存储过程作为子查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个存储过程,其中一个返回付款列表,而另一个返回这些付款的摘要,按货币分组.现在,我有一个重复的查询:返回支付列表的存储过程的主查询是返回按货币支付的摘要的存储过程的子查询.我想通过使返回付款列表的存储过程成为返回按货币支付的摘要的存储过程的子查询来消除这种重复性.这在 SQL Server 2008 中是否可行?

I have two stored procedures, one of which returns a list of payments, while the other returns a summary of those payments, grouped by currency. Right now, I have a duplicated query: the main query of the stored procedure that returns the list of payments is a subquery of the stored procedure that returns the summary of payments by currency. I would like to eliminate this duplicity by making the stored procedure that returns the list of payments a subquery of the stored procedure that returns the summary of payments by currency. Is that possible in SQL Server 2008?

推荐答案

最好将第一个 proc 转换为 TABLE-VALUED 函数.如果涉及多条语句,需要先定义返回表结构并填充.

You are better off converting the first proc into a TABLE-VALUED function. If it involves multiple statements, you need to first define the return table structure and populate it.

示例:

CREATE proc getRecords @t char(1)
as
set nocouut on;
-- other statements --
-- final select
select * from master..spt_values where type = @t
GO

-- 变成--

CREATE FUNCTION fn_getRecords(@t char(1))
returns @output table(
    name sysname,
    number int,
    type char(1),
    low int,
    high int,
    status int) as
begin
-- other statements --
-- final select
insert @output
select * from master..spt_values where type = @t
return
end;

但是如果是直接选择(或者可以写成单条语句),那么可以使用高度优化的INLINE tvf形式

However, if it is a straight select (or can be written as a single statement), then you can use the INLINE tvf form, which is highly optimized

CREATE FUNCTION fn2_getRecords(@t char(1))
returns table as return
-- **NO** other statements; single statement table --
select * from master..spt_values where type = @t

第二个过程只是从第一个过程中选择

The second proc simply selects from the first proc

create proc getRecordsByStatus @t char(1)
as
select status, COUNT(*) CountRows from dbo.fn2_getRecords(@t)
group by status

还有你曾经打电话的地方

And where you used to call

EXEC firstProc @param

要获得结果,您现在可以从中选择

to get a result, you now select from it

SELECT * FROM firstProc(@param)

这篇关于是否可以在 SQL Server 2008 中使用存储过程作为子查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

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

相关文档推荐

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)图?)