Test Flashcards
You develop a Microsoft SQL Server 2012 server database that supports an application.The application contains a table that has the following definition:<br></br>CREATE TABLE Inventory (<br></br>ItemID int NOT NULL PRIMARY KEY,<br></br>ItemsInStore int NOT NULL,<br></br>ItemsInWarehouse int NOT NULL)<br></br>You need to create a computed column that returns the sum total of the ItemsInStore andItemsInWarehouse values for each row. The new column is expected to be queried heavily, and you needto be able to index the column.<br></br>Which Transact-SQL statement should you use?<br></br>A. ALTER TABLE Inventory ADD TotalItems AS ItemslnStore + ItemsInWarehouse<br></br>B. ALTER TABLE Inventory ADD TotalItems AS ItemsInStore + ItemsInWarehouse PERSISTED<br></br>C. ALTER TABLE Inventory ADD TotalItems AS SUM(ItemsInStore, ItemsInWarehouse) PERSISTED<br></br>D. ALTER TABLE Inventory ADD TotalItems AS SUM(ItemsInStore, ItemsInWarehouse)
B.
ALTER TABLE Inventory ADD TotalItems
AS
ItemsInStore + ItemsInWarehouse PERSISTED
<span>You need to create an audit record only when either the MobileNumber or HomeNumber column isupdated.</span>
<span>Which Transact-SQL query should you use?<br></br> A. CREATE TRIGGER TrgPhoneNumberChange<br></br> ON Customers FOR UPDATE AS IF COLUMNS_UPDATED (HomeNumber, MobileNumber) - - Create Audit Records<br></br> B. CREATE TRIGGER TrgPhoneNumberChange ON Customers FOR UPDATE AS IF EXISTS( SELECT<br></br> HomeNumber FROM inserted) OR EXISTS (SELECT MobileNumber FROM inserted) - - Create Audit Records<br></br> C. CREATE TRIGGER TrgPhoneNumberChange ON Customers FOR UPDATE AS IF COLUMNS_CHANGED (HomeNumber, MobileNumber) - - Create Audit Records<br></br> D. CREATE TRIGGER TrgPhoneNumberChange ON Customers FOR UPDATE AS IF UPDATE<br></br> (HomeNumber) OR UPDATE (MobileNumber)<br></br> - - Create Audit Records</span>
Answer: D.
CREATE TRIGGER TrgPhoneNumberChange ON Customers FOR UPDATE AS IF UPDATE<br></br> (HomeNumber) OR UPDATE (MobileNumber)<br></br> - - Create Audit Records
2 tables named SavingAccounts andLoanAccounts. Both tables have a column named AccountNumber of the nvarchar data type. You use athird table named Transactions that has columns named TransactionId AccountNumber, Amount, andTransactionDate. You need to ensure that when multiple records are inserted in the Transactions table,only the records that have a valid AccountNumber in the SavingAccounts or LoanAccounts are inserted.Which Transact-SQL statement should you use?
A. CREATE TRIGGER TrgValidateAccountNumber ON Transactions<br></br> INSTEAD OF INSERT<br></br> AS<br></br> BEGIN<br></br> INSERT INTO Transactions<br></br> SELECT TransactionID,AccountNumber,Amount,TransactionDate<br></br> FROM inserted WHERE AccountNumber IN<br></br> (SELECT AccountNumber FROM LoanAccounts<br></br> UNION SELECT AccountNumber FROM SavingAccounts))<br></br> END<br></br> B. CREATE TRIGGER TrgValidateAccountNumber ON Transactions<br></br> FOR INSERT<br></br> AS<br></br> BEGIN<br></br> INSERT INTO Transactions<br></br> SELECT TransactionID,AccountNumber,Amount,TransactionDate<br></br> FROM inserted WHERE AccountNumber IN<br></br> (SELECT AccountNumber FROM LoanAccounts<br></br> UNION SELECT AccountNumber FROM SavingAccounts))<br></br> END<br></br> C. CREATE TRIGGER TrgValidateAccountNumber ON Transactions<br></br> INSTEAD OF INSERT<br></br> AS<br></br> BEGIN<br></br> IF EXISTS (<br></br> SELECT AccountNumber FROM inserted EXCEPT<br></br> (SELECT AccountNumber FROM LoanAccounts<br></br> UNION SELECT AccountNumber FROM SavingAccounts))<br></br> BEGIN<br></br> ROLLBACK TRAN<br></br> END<br></br> END<br></br> D. CREATE TRIGGER TrgValidateAccountNumber ON Transactions<br></br> FOR INSERT<br></br> AS<br></br> BEGIN<br></br> IF EXISTS (<br></br> SELECT AccountNumber FROM inserted EXCEPT<br></br> (SELECT AccountNumber FROM LoanAccounts<br></br> UNION SELECT AccountNumber FROM SavingAccounts))<br></br> BEGIN<br></br> ROLLBACK TRAN<br></br> END<br></br> END
A.
CREATE TRIGGER TrgValidateAccountNumber ON Transactions<br></br> INSTEAD OF INSERT<br></br> AS<br></br> BEGIN<br></br> INSERT INTO Transactions<br></br> SELECT TransactionID,AccountNumber,Amount,TransactionDate FROM inserted<br></br> WHERE AccountNumber IN<br></br> (SELECT AccountNumber FROM LoanAccounts<br></br> UNION<br></br> SELECT AccountNumber FROM SavingAccounts)<br></br> END
You develop a Microsoft SQL Server 2012 database.<br></br> You create a view that performs the following tasks:
- Joins 8 tables that contain up to 500,000 records each.
- Performs aggregations on 5 fields.
The view is frequently used in several reports. You need to improve the performance of the reports.<br></br> What should you do?<br></br> A. Convert the view into a table-valued function.<br></br> B. Convert the view into a Common Table Expression (CTE).<br></br> C. Convert the view into an indexed view.<br></br> D. Convert the view into a stored procedure and retrieve the result from the stored procedure into atemporary table.
Answer: C
C. Convert the view into an indexed view
You need to ensure that the CustomerId column in the Orders table contains only values that exist in the CustomerId column of the Customer table. Which Transact-SQL statement should you use?
A. ALTER TABLE Orders<br></br>ADD CONSTRAINT FX_Orders_CustomerID FOREIGN KEY (CustomerId) REFERENCES Customer (CustomerId)<br></br>B. ALTER TABLE Customer<br></br>ADD CONSTRAINT FK_Customer_CustomerID FOREIGN KEY {CustomerID) REFERENCES Orders (CustomerId)<br></br>C. ALTER TABLE Orders<br></br>ADD CONSTRAINT CK_Crders_CustomerID CHECK (CustomerId IN (SELECT CustomerId FROM Customer))<br></br>D. ALTER TABLE Customer<br></br>ADD OrderId INT NOT NULL;<br></br>ALTER TABLE Customer<br></br>ADD CONSTRAINT FK_Customer_OrderID FOREIGN KEY (CrderlD) REFERENCES Orders(CrderlD);<br></br>E. ALTER TABLE Orders<br></br>ADD CONSTRAINT PK Orders CustomerId PRIMARY KEY (CustomerID)
Answer: A
ALTER TABLE Orders
ADD CONSTRAINT FX_Orders_CustomerID FOREIGN KEY (CustomerId) REFERENCES Customer (CustomerId)
You need to ensure that users can update only the phone numbers and email addresses by using thisview.<br></br>What should you do?<br></br>A. Alter the view. Use the EXPAND VIEWS query hint along with each SELECT statement.<br></br>B. Create an INSTEAD OF UPDATE trigger on the view.<br></br>C. Drop the view. Re-create the view by using the SCHEMABINDING clause, and then create an index on<br></br>the view.<br></br>D. Create an AFTER UPDATE trigger on the view.
Answer: B
B. Create an INSTEAD OF UPDATE trigger on the view.
CREATE VIEW vOrders WITH SCHEMABINDING<br></br> AS<br></br> SELECT o.ProductID o.OrderDate,<br></br> SUM(od.UnitPrice * od.OrderQty) AS Amount<br></br> FROM OrderDetails AS od INNER JOIN<br></br> Orders as o on od.OrderID = o.OrderID<br></br> Where od.SalesOrderID = o.SalesOrderID<br></br> Group by o.OrderDate,o.ProductID
You need to ensure that users are able to modify data by using the view.<br></br> What should you do?<br></br> A. Create an AFTER trigger on the view.<br></br> B. Modify the view to use the WITH VIEW_METADATA clause.<br></br> C. Create an INSTEAD OF trigger on the view.<br></br> D. Modify the view to an indexed view.
Answer: <strong>C</strong>
<strong>C</strong>. Create an INSTEAD OF trigger on the view.
You have a view that was created by using the following code:
<span>CREATE VIEW Sales.OrderByTerritory AS</span>
<span>SELECT OrderID, OrderDate, SalesTerritoryID, TotalDue FROM Sales.Orders</span>
You need to create an inline table-valued function named Sales.fn_OrdersByTerritory, which must meet<br></br> the following requirements:<br></br> - Accept the @T integer parameter.<br></br> - Use one-part names to reference columns.<br></br> - Filter the query results by SalesTerritoryID.<br></br> - Return the columns in the same order as the order used in OrdersByTerritoryView.<br></br> Which code segment should you use? To answer, type the correct code in the answer area.
CREATE FUNCTION Sales.fn_OrdersByTerritory (@T int)<br></br> RETURNS TABLE<br></br> AS<br></br> RETURN<br></br> (<br></br> SELECT OrderID,OrderDate,SalesTerrirotyID,TotalDue<br></br> FROM Sales.OrdersByTerritory<br></br> WHERE SalesTerritoryID = @T<br></br> )
You need to create a table named Sales.OrderDetails on the new server. Sales.OrderDetails must meet the following requirements:
- Write the results to a disk.
- Contain a new column named LineItemTotal that stores the product of ListPrice and Quantity for each row.
- The code must NOT use any object delimiters.
The solution must ensure that LineItemTotal is stored as the last column in the table. Which code segment should you use? To answer, type the correct code in the answer area.
Answer:
CREATE TABLE Sales.OrderDetails (
ListPrice money not null,
Quantity int not null,
LineItemTotal as (ListPrice * Quantity) PERSISTED)
You need to create a view named uv_CustomerFullName to meet the following requirements:
- The code must NOT include object delimiters.
- The view must be created in the Sales schema.
- Columns must only be referenced by using one-part names.
- The view must return the first name and the last name of all customers.
- The view must prevent the underlying structure of the customer table from being changed.
- The view must be able to resolve all referenced objects, regardless of the user’s default schema.
Which code segment should you use? To answer, type the correct code in the answer area.
CREATE VIEW Sales.uv_CustomerFullName
WITH SCHEMABINDING
AS
SELECT FirstName, LastName
FROM Sales.Customers
You have an XML schema collection named <strong>Sales.InvoiceSchema</strong>. You need to declare a variable ofthe XML type named XML1. The solution must ensure that XML1 is validated by usingSales.InvoiceSchema.Which code segment should you use? To answer, type the correct code in the answer area.
Answer:
DECLARE @XML1 XML(Sales.InvoiceSchema)
You need to create a query that returns a list of products from Sales.ProductCatalog.The solution must meet the following requirements:
- UnitPrice must be returned in descending order
- The query must use two-part names to reference the table
- The query must use the RANK function to calculate the results
- The query must return the ranking of rows in a column named PriceRank
- The list must display the columns in the order that they are defined in the table.
- PriceRank must appear last.
<strong>SELECTProductCatalog.CatID, ProductCatalog.CatName, ProductCatalog.ProductID, ProductCatalog.ProdName, ProductCatalog.UnitPrice</strong><br></br> RANK<strong>() </strong>OVER<strong> (</strong>ORDER BY<strong> ProductCatalog.UnitPrice DESC) AS </strong>PriceRank<br></br> FROM <strong>Sales.ProductCatalog</strong><br></br> ORDER BY<strong> ProductCatalog.UnitPrice DESC</strong>
You have a parameter named @Count that uses the int data type. App1 is configured to pass @Count to a stored procedure. You need to create a stored procedure named usp_Customers for App1.
Usp_Customers must meet the following requirements:
- NOT use object delimiters
- Minimize sorting and counting.
- Return only the last name of each customer in alphabetical order
- Return only the number of rows specified by the @Count parameter.
- The solution must NOT use BEGIN and END statements.
- *Answer**:
- *CREATE PROCEDURE** usp_Customers @Count int
- *AS**
- *SELECT TOP**(@Count) Customers.LastName
- *FROM** Customers
- *ORDER BY** Customers.LastName
Procedure1 has a single parameter named Parameter1.<br></br>Parameter1 uses the varchar type and is configured to pass the specific date to Procedure1. A DBAdiscovers that OrderDate is not being compared correctly to Parameter1 after the data type<br></br>of the column is changed to datetime.You need to update the SELECT statement to meet the following requirements
- The code must NOT use aliases.
- The code must NOT use object delimiters.
- The objects called in Procedure1 must be able to be resolved by all users.
- OrderDate must be compared to Parameter1 after the data type of Parameter1 is changed to datetime
Answer:
SELECT Orders.OrderID
FROM Orders
WHERE Orders.OrderDate > CONVERT(datetime,@Parameter1)
You use Microsoft SQL Server 2012 database to develop a shopping cart application. You need to
invoke a table-valued function for each row returned by a query. Which Transact-SQL operator should you use?
- A. CROSS JOIN
- B. UNPIVOT
- C. PIVOT
- D. CROSS APPLY
Answer: **D **CROSSAPPLY
You need to write a query that displays the following details:<br></br> -Total sales made by <strong>sales people, year, city, and country</strong><br></br> - <strong>Sub totals </strong>only at the <strong>city</strong> level and <strong>country </strong>level<br></br> - A grand total of the sales amount<br></br> Which Transact-SQL query should you use?
A. SELECT SalesPerson.Name, Country, City,<br></br> DatePart(yyyy, SaleDate) AS Year, Sum(Amount) AS Total<br></br> FROM Sale INNER JOIN SalesPerson<br></br> ON Sale.SalesPersonID = SalesPerson.SalesPersonID<br></br> GROUP BY GROUPING SETS((SalesPerson.Name, Country, City, DatePart(yyyy,SaleDate)), (Country, City), (Country), ())
B.SELECT SalesPerson.Name, Country, City,<br></br> DatePart(yyyy, SaleDate) AS Year, Sum(Amount) AS Total<br></br> FROM Sale INNER JOIN SalesPerson<br></br> ON Sale.SalesPersonID = SalesPerson.SalesPersonID<br></br> GROUP BY CUBE(SalesPerson.Name, Country, City, DatePart(yyyy, SaleDate))
C. SELECT SalesPerson.Name, Country, City,<br></br> DatePart(yyyy, SaleDate) AS Year, Sum(Amount) AS Total<br></br> FROM Sale INNER JOIN SalesPerson<br></br> ON Sale.SalesPersonID = SalesPerson.SalesPersonID<br></br> GROUP BY CUBE(SalesPerson.Name, DatePart(yyyy, SaleDate), City, Country)
D. SELECT SalesPerson.Name, Country, City,<br></br> DatePart(yyyy, SaleDate) AS Year, Sum(Amount) AS Total<br></br> FROM Sale INNER JOIN SalesPerson<br></br> ON Sale.SalesPersonID = SalesPerson.SalesPersonID
Answer: A
SELECT SalesPerson.Name, Country, City,
DatePart(yyyy, SaleDate) AS Year, Sum(Amount) AS Total
FROM Sale INNER JOIN SalesPerson
ON Sale.SalesPersonID = SalesPerson.SalesPersonID
GROUP BY GROUPING SETS((SalesPerson.Name, Country, City, DatePart(yyyy, SalesDate)), (Country, City), (Country), ())
You are developing a database that will contain price information. You need to store the prices that include a fixed precision and a scale of six digits. Which data type should you use?
A. Float
B. Money
C. Smallmoney
D. Numeric
Answer: D Numeric
You administer a Microsoft SQL Server database that supports a banking transaction managementapplication. You need to retrieve a list of account holders who live in cities that do not have a branchlocation.<br></br> Which Transact-SQL query or queries should you use? (Choose all that apply.)<br></br> A. SELECT AccountHolderIDFROM AccountHolder<br></br> WHERE CityID NOT IN (SELECT CityID FROM BranchMaster)<br></br> B. SELECT AccountHolderIDFROM AccountHolder<br></br> WHERE CityID <> ALL (SELECT CityID FROM BranchMaster)<br></br> C. SELECT AccountHolderlDFROM AccountHolder<br></br> WHERE CityID <> SOME (SELECT CityID FROM BranchMaster)<br></br> D. SELECT AccountHolderIDFROM AccountHolder<br></br> WHERE CityID <> ANY (SELECT CityID FROM BranchMaster)
Answer: A, B
A. SELECT AccountHolderIDFROM AccountHolder<br></br> WHERE CityID NOT IN (SELECT CityID FROM BranchMaster)<br></br> B. SELECT AccountHolderIDFROM AccountHolder<br></br> WHERE CityID <> ALL (SELECT CityID FROM BranchMaster)
You develop three Microsoft SQL Server 2012 databases named Database1, Database2, andDatabase3.<span>You have permissions on both Database1 and Database2. You plan to write and deploy a</span><span>stored procedure named dbo.usp_InsertEvent in Database3. dbo.usp_InsertEvent must execute other</span><span>stored procedures in the other databases. You need to ensure that callers that do not have permissions</span><span>on Database1 or Database2 can execute the stored procedure.</span><span>Which Transact-SQL statement should you use?</span>
A. USE Database2<br></br> B. EXECUTE AS OWNER<br></br> C. USE Database1<br></br> D. EXECUTE AS CALLER
Answer: B Execute As Owner
You develop a Microsoft SQL Server 2012 database that contains tables named Customers andOrders. The tables are related by a column named CustomerId . You need to create a query that meetsthe following requirements:<br></br> - Returns the CustomerName for all customers and the OrderDate for any orders that they have placed.<br></br> - Results must not include customers who have not placed any orders.
A. SELECT CustomerName, OrderDateFROM Customers<br></br> LEFT OUTER JOIN OrdersON Customers.CuscomerlD = Orders.CustomerId<br></br> B. SELECT CustomerName, OrderDateFROM Customers<br></br> RIGHT OUTER JOIN OrdersON Customers.CustomerID = Orders.CustomerId<br></br> C. SELECT CustomerName, OrderDateFROM Customers<br></br> CROSS JOIN OrdersON Customers.CustomerId = Orders.CustomerId<br></br> D. SELECT CustomerName, OrderDateFROM Customers<br></br> JOIN OrdersON Customers.CustomerId = Orders.CustomerId
Answer: D
You develop a Microsoft SQL Server 2012 database.You need to create a batch process that meets the following requirements:
- Status information must be logged to a status table.
- If the status table does not exist at the beginning of the batch, it must be created.
A. Scalar user-defined function<br></br> B. Inline user-defined function<br></br> C. Table-valued user-defined function<br></br> D. Stored procedure
Answer: D.Stored Procedure
You use a Microsoft SQL Server 2012 database that contains a table named BlogEntry that has the<br></br> following columns:<br></br> Id BigInt<br></br> EntryDateTime datetime<br></br> Summary nvarchar(max)
Id is the Primary Key.<br></br> 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.
A. UPDATE TOP(10) BlogEntry<br></br> SET Summary.WRITE(N’ This is in a draft stage’, NULL, 0)<br></br> B. UPDATE BlogEntry<br></br> SET Summary = CAST(N’ This is in a draft stage’ as nvarchar(max))<br></br> WHERE Id IN(SELECT TOP(10) Id FROM BlogEntry ORDER BY EntryDateTime DESC)<br></br> C. UPDATE BlogEntry<br></br> SET Summary.WRITE(N’ This is in a draft stage’, NULL, 0) FROM (<br></br> SELECT TOP(10) Id FROM BlogEntry ORDER BY EntryDateTime DESC) AS s<br></br> WHERE BlogEntry.Id = s.ID<br></br> D. UPDATE BlogEntry<br></br> SET Summary.WRITE(N’ This is in a draft stage’, 0, 0)<br></br> WHERE Id IN(SELECT TOP(10) Id FROM BlogEntry ORDER BY EntryDateTime DESC)
Answer: D.<br></br> UPDATE BlogEntry<br></br> SET Summary.WRITE(N’ This is in a draft stage’, 0, 0)<br></br> WHERE Id IN(SELECT TOP(10) Id FROM BlogEntry ORDER BY EntryDateTime DESC)<br></br> <u><em>.WRITE(expression, @Offset, @Length)</em></u><br></br> If @offset is null, the update operation appends expression at the end of the existing expression value and @Length is ignored.<br></br> If @Length is NULL, the update operation removes all data from @Offset to the end of the expression value.
How to use .WRITE?
.WRITE (expression,@Offset,@Length)
Specifies that a section of the value is to be modified.
- Expression replaces @Length units starting from @Offset column.
- Only columns of varchar(max), nvarchar(max), or varbinary(max) can be specified with this clause
@@ERROR
- Unstructured error handling
- Test individual statement immediately after they executed
- TSQL records an error status of the execution result in @@ERROR.
- @@ERROR = 0 good.
- Capture @@ERROR in a variable and test the variable
BEGIN CATCH
________
COMMIT TRANSACTION
END CATCH
The stored procedure can be called within other transactions. You need to ensure that when the DELETE<br></br> statement from the HumanResourcesJobCandidate table succeeds, the modification is retained even if<br></br> the insert into the Audit.Log table fails.
A. IF @@TRANCOUNT = 0<br></br> B. IF (XACT_STATE ( ) ) = 0<br></br> C. IF (XACT_STATE ( ) ) = 1<br></br> D. IF @@TRANCOUNT = 1
Answer: C
IF (XACT_STATE ( ) ) = 1
LAG
Use this analytic function in a SELECT statement to compare values in the current row with values in a previous row.
LAG (scalar_expression [,offset] [,default])OVER ( [partition_by_clause] order_by_clause )
offset -The number of rows back from the current row from which to obtain a value. If not specified, the default is 1.
default -The value to return when scalar_expression at offset is NULL. If a default value is not specified, NULL is returned
You use Microsoft SQL Server 2012 to develop a database application. Your application sends data to<br></br> an NVARCHAR(MAX) variable named @var. You need to write a Transact-SQL statement that will find<br></br> out the success of a cast to a decimal (36, 9).
A. BEGIN TRY<br></br> SELECT convert(decimal(36,9), @var) AS Value, ‘True’ AS BadCast<br></br> END TRY<br></br> BEGIN CATCH<br></br> SELECT convert(decimal(36,9), @var) AS Value, ‘False’ AS BadCast<br></br> END CATCH<br></br> B. TRY(<br></br> SELECT convert(decimal(36,9), @var)<br></br> SELECT ‘True’ AS BadCast<br></br> )<br></br> CATCH(SELECT ‘False’ AS BadCast)<br></br> C. SELECT<br></br> CASE<br></br> WHEN convert(decimal(36,9), @var) IS NULL<br></br> THEN ‘True’<br></br> ELSE ‘False’<br></br> END<br></br> AS BadCast<br></br> D. SELECT<br></br> IIF(TRY_PARSE(@var AS decimal(36,9)) IS NULL, ‘True’, ‘False’)<br></br> AS BadCast
Answer: D
You are writing a set of queries against a FILESTREAM-enabled database. You create a stored<br></br> procedure that will update multiple tables within a transaction. You need to ensure that if the stored<br></br> procedure raises a run-time error, the entire transaction is terminated and rolled back.<br></br> Which Transact-SQL statement should you include at the beginning of the stored procedure?<br></br> A. SET TRANSACTION ISOLATION LEVEL SERIALIZABLE<br></br> B. SET XACT_ABORT OFF<br></br> C. SET TRANSACTION ISOLATION LEVEL SNAPSHOT<br></br> D. SET IMPLICIT_TRANSACTIONS ON<br></br> E. SET XACT_ABORT ON<br></br> F. SET IMPLICIT TRANSACTIONS OFF
Answer: E