我正在使用无法更改表格的 3rd pary 应用程序。我们使用附加的日期时间列“AsOfDate”构建了自定义匹配的“每月”表,我们在其中转储了月末的数据,并将这些数据标记为当月最后一天的日期。
我希望能够创建单个存储过程(应用程序旨在要求视图或存储过程作为所有报告的来源。)并使用将使用当前数据表的参数(参数可能为 NULL 或 = Today's日期)或使用月末表并按月末日期过滤。这样,我有一个报告,用户可以使用当前或特定月末期间的数据。
你更喜欢哪个(以及为什么)对不起,这不是完全编码的
解决方案 #1 联合查询
Create Proc Balance_Report (@AsOfDate)
AS
Select Column1
From
(Select GetDate() as AsOfDate
, Column1
From Current.Balance
Union
Select AsOfDate
, Column1 From MonthEnd.Balance
) AS All_Balances
Where All_Balances.AsOfDate = @AsOfDate
解决方案 #2 使用 If 语句选择表
Create Proc Balance_Report (@AsOfDate)
AS
If @AsOfDate IS NULL or @AsOfDate = GetDate()
Select GetDate() as AsOfDate
, Column1
From Current.Balance
Else
Select AsOfDate
, Column1 From MonthEnd.Balance
Where AsOfDate = @AsOfDate
同样,这不是完全编码的,并且与数据库无关(但它是 SQL Server 2005)。
编辑:使用单独的存储过程对解决方案 #2 的变体
Create Proc Balance_Report (@AsOfDate)
AS
If @AsOfDate IS NULL or @AsOfDate = GetDate()
Exec Current_Balance_Date -- no param necessary
Else
exec MonthEnd_Balance_Date @AsOfDate