我正在使用第三个Pary应用程序,我无法更改表。我们用附加的日期时间列“asofdate”构建了自定义匹配的“monthly”表,在该列中,我们在月末转储数据,并将这些数据标记为该月最后一天的日期。
我希望能够创建一个单独的存储过程(应用程序设计为需要一个视图或存储过程作为所有报告的源),并使用一个参数,该参数将使用当前数据表(参数可能为空或=今天的日期),或者使用月末表并在月末日期前进行筛选。这样,我就有了一个报表,用户可以在其中使用当前值或特定月末期间的数据。
你更喜欢哪一种(为什么)对不起,这个没有完全编码
解决方案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