联合或使用流控制逻辑来确定要报告的表

发布于 2024-07-13 11:50:49 字数 1266 浏览 3 评论 0原文

我正在使用第三个应用程序,我无法更改表格。 我们构建了自定义匹配的“每月”表,并附加了一个日期时间列“AsOfDate”,我们在月底转储数据,并用该月最后一天的日期标记这些数据。

我希望能够创建单个存储过程(应用程序设计为需要视图或存储过程作为所有报告的源。)并使用一个参数,该参数将使用当前数据表(参数可能为 NULL 或 = 今天的数据表)日期)或使用月末表并按月末日期进行筛选。 这样,我就有了一份报告,用户可以使用当前数据或特定月末期间的数据。

您更喜欢哪一个(以及为什么) 抱歉,这没有完全编码

解决方案#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

I'm working with a 3rd pary app where I can't alter the tables. We built custom matching "Monthly" tables with an additional datetime column "AsOfDate" where we dump the data at the end of the month and flag those data with the date of last day of the month.

I want to be able to create a single Stored Procedure (Application is designed to require a view or stored proc as the source of all reports.) and use a parameter which will either use the current data table (Parameter may be NULL or = Today's Date) or use the month end table and filter by the End of the month date. This way, I have one report where the user can either use current or data from a particular month end period.

Which would you prefer (And why) Sorry, this is not fully coded

Solution #1 Union Query

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

Solution #2 Use If Statement to select table

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

Again, this is not fully coded and is sort of db agnostic (But it is SQL Server 2005).

Edit: Variation to Solution #2 using separate stored procedures

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜司空 2024-07-20 11:50:49

如何设置,第二种方法可能会更快。 如果您要使用分区视图,那么您可以设置约束,使优化器知道忽略选择中的一个或多个表,并且您将获得相同的性能。 这还可以让您将所有逻辑保留在一个语句中,而不必使两个语句保持同步。 根据 SELECT 语句的复杂程度,这对您来说可能是问题,也可能不是问题。

但要记住的一件事是,如果您使用第二种方法,请务必将您的存储过程标记为WITH(RECOMPILE)(我不记得括号是否是必需的 - 检查语法)。 这样,优化器将根据需要执行 IF 语句的哪个分支来创建新的查询计划。

How you have things set up, the second method will probably be faster. If you were to use a partitioned view then you could set up constraints in such a way that the optimized would know to ignore one or more of the tables in the select and you would get the same performance. This would also let you keep all of your logic in one statement rather than having to keep two statements in sync. That may or may not be an issue for you based on how complex the SELECT statement is.

One thing to remember though, is that if you use the second method, be sure to mark your stored procedure as WITH (RECOMPILE) (I can't remember if the parentheses are require or not - check the syntax). That way the optimizer will create a new query plan based on which branch of the IF statement needs to be executed.

夕色琉璃 2024-07-20 11:50:49

我更喜欢非工会解决方案。 从单个表中选择总是比执行联合并从联合中选择单个表的数据更快。

I prefer the non-union solution. Selecting from a single table will always be faster than doing a union and selecting a single table's data from the union.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文