簡體   English   中英

加入 SQL 時態表截至時間

[英]Joining SQL Temporal tables as-of time

我正在嘗試執行以下操作:

SELECT report.*,
doc.*
FROM report
FOR system_time ALL report
JOIN Document
FOR system_time as of <<report.BeginDate>> doc ON report.DocumentId = doc.DocumentId

基本上我想獲取父表的所有歷史記錄,並且它是根據父行在正確時間關聯的子表。 這可能嗎?

不幸的是, FOR SYSTEM_TIME只允許使用文字或變量/參數。 它不能是查詢中的列。 但是您可以將其放入內聯表值 Function,然后APPLY function:

CREATE OR ALTER FUNCTION dbo.DocumentAsOfDate (@documentId int, @asOfDate datetime(7))
RETURNS TABLE
AS RETURN
SELECT d.*
FROM dbo.Document FOR SYSTEM_TIME AS OF @asOfDate AS d
WHERE d.DocumentId = @documentId;

GO
SELECT
  report.*,
  doc.*
FROM report FOR SYSTEM_TIME ALL report
CROSS APPLY dbo.DocumentAsOfDate (report.DocumentId, report.BeginDate) doc;

您可能需要考慮使用哪個日期(開始或結束),以及是否使用FOR SYSTEM_TIME BETWEEN...或其他過濾器。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM