PrepAway - Latest Free Exam Questions & Answers

Which Transact-SQL statement should you use?

You use a Microsoft SQL Server 2012 database that contains a table named BlogEntry that has the
following columns:
Id is the Primary Key.

You need to append the “This is in a draft stage” string to the Summary column of the recent 10
entries based on the values in EntryDateTime.
Which Transact-SQL statement should you use?

PrepAway - Latest Free Exam Questions & Answers

A.
UPDATE TOP(10) BlogEntry
SET Summary.WRITE(N’ This is in a draft stage’, NULL, 0)

B.
UPDATE BlogEntry
SET Summary = CAST(N’ This is in a draft stage’ as nvarchar(max))
WHERE Id IN(SELECT TOP(10) Id FROM BlogEntry ORDER BY EntryDateTime DESC)

C.
UPDATE BlogEntry
SET Summary.WRITE(N’ This is in a draft stage’, NULL, 0) FROM (
SELECT TOP(10) Id FROM BlogEntry ORDER BY EntryDateTime DESC) AS s
WHERE BlogEntry.Id = s.ID

D.
UPDATE BlogEntry
SET Summary.WRITE(N’ This is in a draft stage’, 0, 0)
WHERE Id IN(SELECT TOP(10) Id FROM BlogEntry ORDER BY EntryDateTime DESC)

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

  1. Bartosz says:

    “Cannot call methods on nvarchar.” so the .write does not work.
    Only C doean’t use that method

    In the question it states “You need to append the ” so I would change

    SET Summary = CAST(N’ This is in a draft stage’ as nvarchar(max))
    to
    SET Summary = CAST(N’ This is in a draft stage’ as nvarchar(max)) + summary




    0



    0
    1. Picasso says:

      Another person that cannot “check”, B Overwrites the text, it does not append as the question requests. C is correct, proof below in my post, i REALLY CHECKED




      0



      0
  2. Yoyo says:

    .WRITE (expression,@Offset,@Length)
    Specifies that a section of the value of column_name is to be modified. expression replaces @Length units starting from @Offset of column_name. Only columns of varchar(max), nvarchar(max), or varbinary(max) can be specified with this clause. column_name cannot be NULL and cannot be qualified with a table name or table alias.
    expression is the value that is copied to column_name. expression must evaluate to or be able to be implicitly cast to the column_name type. If expression is set to NULL, @Length is ignored, and the value in column_name is truncated at the specified @Offset.
    @Offset is the starting point in the value of column_name at which expression is written. @Offset is a zero-based ordinal position, is bigint, and cannot be a negative number. If @Offset is NULL, the update operation appends expression at the end of the existing column_name value and @Length is ignored. If @Offset is greater than the length of the column_name value, the Database Engine returns an error. If @Offset plus @Length exceeds the end of the underlying value in the column, the deletion occurs up to the last character of the value. If @Offset plus LEN(expression) is greater than the underlying declared size, an error is raised.
    @Length is the length of the section in the column, starting from @Offset, that is replaced by expression. @Length is bigint and cannot be a negative number. If @Length is NULL, the update operation removes all data from @Offset to the end of the column_name value.




    0



    0
  3. Picasso says:

    CREATE TABLE BlogEntry
    (
    Id BIGINT,
    EntryDateTime DATETIME,
    Summary NVARCHAR(MAX)
    )

    INSERT INTO BlogEntry(Id,EntryDateTime,Summary)
    VALUES(1,GETDATE()-11,’One’);
    INSERT INTO BlogEntry(Id,EntryDateTime,Summary)
    VALUES(2,GETDATE()-10,’Two’);
    INSERT INTO BlogEntry(Id,EntryDateTime,Summary)
    VALUES(3,GETDATE()-9,’Three’);
    INSERT INTO BlogEntry(Id,EntryDateTime,Summary)
    VALUES(4,GETDATE()-8,’Four’);
    INSERT INTO BlogEntry(Id,EntryDateTime,Summary)
    VALUES(5,GETDATE()-7,’Five’);
    INSERT INTO BlogEntry(Id,EntryDateTime,Summary)
    VALUES(6,GETDATE()-6,’Six’);
    INSERT INTO BlogEntry(Id,EntryDateTime,Summary)
    VALUES(7,GETDATE()-5,’Seven’);
    INSERT INTO BlogEntry(Id,EntryDateTime,Summary)
    VALUES(8,GETDATE()-4,’Eight’);
    INSERT INTO BlogEntry(Id,EntryDateTime,Summary)
    VALUES(9,GETDATE()-3,’Nine’);
    INSERT INTO BlogEntry(Id,EntryDateTime,Summary)
    VALUES(10,GETDATE()-2,’Ten’);
    INSERT INTO BlogEntry(Id,EntryDateTime,Summary)
    VALUES(11,GETDATE()-1,’Eleven’);

    –A (Updates text but not for most recent 10 by date (Id’s 11-2))
    UPDATE TOP(10) BlogEntry
    SET Summary.WRITE(N’ This is in draft stage’,NULL,0)

    –B (Edits correct records but overwrites existing text, does not append)
    UPDATE BlogEntry
    SET Summary = CAST(N’ This is in draft stage’ as nvarchar(max))
    WHERE Id IN(SELECT TOP(10) Id
    FROM BlogEntry ORDER BY EntryDateTime DESC)

    –C (CORRECT, NULL FORCES AN APPEND)
    UPDATE BlogEntry
    SET Summary.WRITE(N’ This is in draft stage’,NULL,0) FROM
    (SELECT TOP(10) Id FROM BlogEntry ORDER BY EntryDateTime DESC) AS s
    WHERE BlogEntry.Id = s.Id

    –D (Updates correct records but adds text to the beginning not the end)
    UPDATE BlogEntry
    SET Summary.WRITE(N’ This is in draft stage’,0,0)
    WHERE Id IN (SELECT TOP(10) Id FROM BlogEntry ORDER BY EntryDateTime DESC)

    SELECT * FROM BlogEntry
    GO;




    1



    0

Leave a Reply