sql-server – 可以在SQL Server 2008中使用存储过程作为子查询
发布时间:2020-12-12 16:18:09 所属栏目:MsSql教程 来源:网络整理
导读:我有两个存储过程,其中一个返回一个付款列表,另一个返回这些付款的摘要,按货币分组.现在,我有一个重复的查询:返回付款列表的存储过程的主要查询是存储过程的子查询,它以货币形式返回付款摘要.我想通过使存储过程返回支付列表的存储过程来消除这种重复,该子查
我有两个存储过程,其中一个返回一个付款列表,另一个返回这些付款的摘要,按货币分组.现在,我有一个重复的查询:返回付款列表的存储过程的主要查询是存储过程的子查询,它以货币形式返回付款摘要.我想通过使存储过程返回支付列表的存储过程来消除这种重复,该子查询是按货币返回付款摘要的存储过程的子查询.在SQL Server 2008中有可能吗?
解决方法你最好将第一个proc转换成TABLE-VALUED函数.如果它涉及多个语句,则需要首先定义返回表结构并进行填充.样品: 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表单,这是高度优化的 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 第二个proc只是从第一个proc中选择 create proc getRecordsByStatus @t char(1) as select status,COUNT(*) CountRows from dbo.fn2_getRecords(@t) group by status 你曾经打电话的地方 EXEC firstProc @param 要获得结果,您现在可以从中选择 SELECT * FROM firstProc(@param) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |