PdfPrep.com

Which Transact-SQL statement should you use?

Note: This question is part of a series of questions that use the same scenario. For your convenience, the scenario is repeated in each question. Each question presents a different goal and answer choices, but the text of the scenario is exactly the same in each question in this series.

You have a database that tracks orders and deliveries for customers in North America. System versioning is enabled for all tables.

The database contains the Sales.Customers, Application.Cities, and Sales.CustomerCategories tables.

Details for the Application.Cities table are shown in the following table:

Details for the Sales.CustomerCategories table are shown in the following table:

You discover an application bug that impacts customer data for records created on or after January 1, 2014. In order to fix the data impacted by the bug, application programmers require a report that contains customer data as it existed on December 31, 2013.

You need to provide the query for the report.

Which Transact-SQL statement should you use?
A . DECLARE @sdate DATETIME, @edate DATETIME
SET @sdate = DATEFROMPARTS (2013, 12, 31)
SET @edate = DATEADD (d, 1, @sdate)
SELECT * FROM Sales.Customers FOR SYSTEM_TIME ALL
WHERE ValidFrom > @sdate AND ValidTo < @edate
B . DECLARE @sdate DATETIME, @edate DATETIME
SET @sdate = DATEFROMPARTS (2013, 12, 31)
SET @edate = DATEADD (d, -1, @sdate)
SELECT * FROM Sales.Customers
FOR SYSTEM_TIME BETWEEN @sdate AND @edate
C . DECLARE @date DATE
SET @date = DATEFROMPARTS (2013, 12, 31)
SELECT * FROM Sales.Customers
FOR SYSTEM_TIME ALL AS OF @date

D . DECLARE @date DATE
SET @date = DATEFROMPARTS (2013, 12, 31)
SELECT * FROM [Sales].[Customers] FOR SYSTEM_TIME ALL
WHERE @date BETWEEN ValidFrom AND ValidTo
E . DECLARE @date DATE
SET @date = DATEFROMPARTS (2013, 12, 31)
SELECT * FROM [Sales].[Customers] FOR SYSTEM_TIME ALL
WHERE SYSDATETIME = @date

Answer: C

Explanation:

To perform any type of time-based analysis, use the new FOR SYSTEM_TIME clause with four temporal-specific sub-clauses to query data across the current and history tables.

Use the AS OF sub-clause when you need to reconstruct state of data as it was at any specific time in the past.

References: https://docs.microsoft.com/en-us/sql/relational-databases/tables/querying-data-in-a-system-versioned­temporal-table?view=sql-server-2017

Exit mobile version