PrepAway - Latest Free Exam Questions & Answers

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 on 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 Sales.Customers table are shown in the following table:

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?

PrepAway - Latest Free Exam Questions & Answers

A.

B.

C.

D.

Explanation:
The datetime datetype defines a date that is combined with a time of day with fractional seconds that is based
on a 24-hour clock.
The DATEFROMPARTS function returns a date value for the specified year, month, and day.
Incorrect Answers:
A: ValidFrom should be less (<) than @sdate AND ValidTo should be greater (>) than @edate.
B: We should add a day with DATEADD, not subtract one day.C: We cannot compare a date to an exact datetime.
https://msdn.microsoft.com/en-us/library/ms187819.aspx

11 Comments on “Which Transact-SQL statement should you use?

  1. B says:

    C is correct. A has incorrect conditions, B wouldn’t take into account records added on 30.12, and D doesn’t even use a temporal query, meaning it’ll miss rows removed after 31.12.




    32



    0
  2. eyadviser says:

    C is right.

    D is not temporal query. A normal temporal query with BETWEEN should look like this:
    /* Query using BETWEEN…AND sub-clause*/
    SELECT
    [DeptID]
    , [DeptName]
    , [SysStartTime]
    , [SysEndTime]
    , IIF (YEAR(SysEndTime) = 9999, 1, 0) AS IsActual
    FROM [dbo].[Department]
    FOR SYSTEM_TIME BETWEEN ‘2015-01-01’ AND ‘2015-12-31’
    WHERE DeptId = 1
    ORDER BY SysStartTime DESC;




    12



    0
    1. eyadviser says:

      The answer says “C: We cannot compare a date to an exact datetime.”
      Of course we can use a date time for this! see the MS doc example:

      /*State of entire table AS OF specific date in the past*/
      SELECT [DeptID], [DeptName], [SysStartTime],[SysEndTime]
      FROM [dbo].[Department]
      FOR SYSTEM_TIME AS OF ‘2015-09-01 T10:00:00.7230011’ ;

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




      1



      0
  3. eder says:

    AQUI VA UN EJEMPLO PARA QUE PUEDAN COMPRENDERLO

    declare @sdate DATETIME,@edate datetime
    set @sdate=DATEFROMPARTS(2018,10,02)
    set @edate=dateadd(d,1,@sdate)
    SELECT @SDATE,@edate




    0



    0

Leave a Reply