SQL Server存储过程调用多个存储过程并且无法返回结果

时间:2012-01-18 21:41:26

标签: sql-server tsql

我有一个存储过程(USA2YearPremiumAnalysis),它使用不同的变量调用另一个存储过程(2YearPremiumAnalysisByState)的两个变体。

所以SP-Parent的代码如下所示:

CREATE PROCEDURE USA2YearPremiumAnalysis
   @ContractYear1 INT
   , @importDate1 DATETIME
   , @importDate2 DATETIME
   , @EnrollmentYear1 INT
   , @EnrollmentYear2 INT
   , @EnrollmentMonth1 INT
   , @EnrollmentMonth2 INT
AS
   EXECUTE MAInfo.[2YearPremiumAnalysisByState] 
   @ContractYear1, 
   @importDate1, @importDate2, 
   @EnrollmentYear1, @EnrollmentYear2, 
   @EnrollmentMonth1, @EnrollmentMonth2,
   0

   EXECUTE MAInfo.[2YearPremiumAnalysisByState] 
   @ContractYear1, 
   @importDate1, @importDate2, 
   @EnrollmentYear1, @EnrollmentYear2, 
   @EnrollmentMonth1, @EnrollmentMonth2, 
   1
GO

这不会返回任何数据。但是,如果我打电话:

   EXECUTE MAInfo.[2YearPremiumAnalysisByState] 
   @ContractYear1, 
   @importDate1, @importDate2, 
   @EnrollmentYear1, @EnrollmentYear2, 
   @EnrollmentMonth1, @EnrollmentMonth2,
   0

   EXECUTE MAInfo.[2YearPremiumAnalysisByState] 
   @ContractYear1, 
   @importDate1, @importDate2, 
   @EnrollmentYear1, @EnrollmentYear2, 
   @EnrollmentMonth1, @EnrollmentMonth2, 
   1

我得到了我的期望。

Child proc相当复杂。当您使用传递给父proc的相同变量调用过程时,它会起作用。

如何让Parent过程返回2个结果表?

2 个答案:

答案 0 :(得分:2)

我建议你阅读Erland题为“如何在存储过程之间共享数据”的这篇文章 - http://www.sommarskog.se/share_data.html

这篇文章很好地解释了每个选项的例子。希望你会发现它很有用。

答案 1 :(得分:0)

子程序是一个动态编写的非常长的多步程序。有两个问题,一个是子进程中的一个问题导致动态sql被设置为NULL。第二个是我测试我的顶级问题的方式:当我手动运行时,我为子程序输入了不同的数字,因此,他们被愚弄认为他们正在工作。

如果我这样做了,我会立即知道我的问题是在子进程中立即出现的:

DECLARE @ContractYear1 INT = 2011
DECLARE @importDate1 DATETIME = '7/27/2011 12:00:00 AM'
DECLARE @importDate2 DATETIME = '6/7/2010 12:00:00 AM'
DECLARE @EnrollmentYear1 INT = 2011
DECLARE @EnrollmentYear2 INT = 2010
DECLARE @EnrollmentMonth1 INT = 7
DECLARE @EnrollmentMonth2 INT = 9

--EXEC MAInfo.USA2YearPremiumAnalysis @ContractYear1, -- int
--    @importDate1, -- datetime
--    @importDate2, -- datetime
--    @EnrollmentYear1, -- int
--    @EnrollmentYear2, -- int
--    @EnrollmentMonth1, -- int
--    @EnrollmentMonth2-- int

EXECUTE MAInfo.[2YearPremiumAnalysisByState] 
    @ContractYear1, 
    @importDate1, -- datetime
    @importDate2, -- datetime
    @EnrollmentYear1, -- int
    @EnrollmentYear2, -- int
    @EnrollmentMonth1, -- int
    @EnrollmentMonth2,
    0

EXECUTE MAInfo.[2YearPremiumAnalysisByState] 
    @ContractYear1, 
    @importDate1, @importDate2, 
    @EnrollmentYear1, @EnrollmentYear2, 
    @EnrollmentMonth1, @EnrollmentMonth2, 
    1

感谢您抽出宝贵的时间,感谢抱歉!