Note: This question is part of a series of questions that present the same scenario. Each question in
the series contains a unique solution that might meet the stated goals. Some question sets might have
more than one correct solution, while others might not have a correct solution.
After you answer a question in this section. You will NOT be able to return to it. As a result, these
questions will not appear in the review screen.
You have a database that tracks orders and deliveries for customers in North America. The database contains
the following tables:
Sales.Customers
Application.Cities
Sales.CustomerCategories
The company’s development team is designing a customer directory application. The application must list
customers by the area code of their phone number. The area code is defined as the first three characters of the
phone number.
The main page of the application will be based on an indexed view that contains the area and phone number
for all customers.
You need to return the area code from the PhoneNumber field.
Solution: You run the following Transact-SQL statement:
Does the solution meet the goal?
A.
Yes
B.
No
Explanation:
The function should return nvarchar(10) and not a TABLE.
https://sqlstudies.com/2014/08/06/schemabinding-what-why/
Explanation of answer is fine but the following reference would be better:
https://docs.microsoft.com/en-us/sql/t-sql/functions/string-split-transact-sql
0
0
TVF can not be used in Indexed View, this is the reason
9
0
Thanks Steven, I Confirm What you said.
–CREATING FUNCTION
CREATE FUNCTION dbo.Get1AND2()
RETURNS TABLE
WITH SCHEMABINDING
AS
RETURN
(
SELECT 1 AS id
UNION ALL
SELECT 2
)
GO
–CREATING VIEW
CREATE VIEW myview
WITH SCHEMABINDING
AS
SELECT id
FROM dbo.Get1AND2() AS schedule
GO
–CHECKING RESULTSET
SELECT *
FROM myview
–CREATING INDEXED VIEW
CREATE UNIQUE CLUSTERED INDEX idx_cl_id ON myview(id); –breaks here
–RESET
DROP VIEW IF EXISTS myview
DROP FUNCTION IF EXISTS dbo.Get1AND2
1
0
no ,por 2 razones:
The area code is defined as the first three characters of the phone number–>left or string_split
The main page of the application will be based on an indexed view that contains the area and phone number for all customer—->with schemabinding
alter FUNCTION test.AreaCode(
@phoneNumber VARCHAR(250)
)
RETURNS VARCHAR(250)
BEGIN
DECLARE @phone VARCHAR(250)
SELECT @PHONE=left(@phoneNumber,3)
RETURN @PHONE
END
alter FUNCTION test.AreaCode(
@phoneNumber VARCHAR(250)
)
RETURNS VARCHAR(250)
BEGIN
DECLARE @phone VARCHAR(250)
SELECT @PHONE=value from string_split(@phoneNumber,3)
RETURN @PHONE
END
SELECT phone,test.AreaCode(phone) FROM test.employees
0
0
A ME OLVIDABA ,IS A SCALAR FUNCTION
0
0