Presentation is loading. Please wait.

Presentation is loading. Please wait.

Designing and Implementing User- Defined Functions

Similar presentations


Presentation on theme: "Designing and Implementing User- Defined Functions"— Presentation transcript:

1 Designing and Implementing User- Defined Functions
20762B 10: Designing and Implementing User-Defined Functions Module 10 Designing and Implementing User- Defined Functions

2 Alternatives to Functions
20762B Module Overview 10: Designing and Implementing User-Defined Functions Alternatives to Functions

3 Lesson 1: Overview of Functions
20762B Lesson 1: Overview of Functions 10: Designing and Implementing User-Defined Functions System Functions Apart From Data Type and Data Time, Which SQL Server Functions Have You Used? Responses will vary, based on experience. Question Which of these is a ranking function? ( )Option 1: OPENROWSET ( )Option 2: ROWCOUNT_BIG ( )Option 3: GROUPING_ID ( )Option 4: ROW_NUMBER ( )Option 5: OPENXML Answer (√) Option -2: ROW_NUMBER

4 Functions cannot modify data
20762B Types of Functions 10: Designing and Implementing User-Defined Functions Types of functions: Scalar functions Table-valued functions Inline versus multistatement functions System functions Functions cannot modify data Begin by describing how functions are used in other programming languages, then discuss in general terms the distinctions between the different types of functions shown here. All of these functions will be covered in more detail in the following slides, so do not go into too much detail here. Mention that the inline TVF is similar to a view with parameters and the multistatement TVF is like a stored procedure you can select from. SQL Server comes with built-in system functions but also means users can create their own functions known as user-defined functions (UDFs). Like functions in programming languages, Microsoft SQL Server UDFs are routines that accept parameters, perform an action, such as a complex calculation, and return the result of that action as a value. The return value can either be a single scalar value, or a result set.

5 20762B System Functions 10: Designing and Implementing User-Defined Functions SQL Server includes a large number of built-in functions Rowset functions: for example, OPENQUERY, OPENROWSET Aggregate functions: for example, AVG, MAX, SUM Ranking functions: for example, RANK, ROW_NUMBER Scalar functions include: Configuration functions Conversion functions Cursor functions Date and time functions Mathematical functions Security functions SQL Server provides a wide variety of functions in T-SQL, similar to those provided in high level programming languages. Provide a few examples of functions that you find useful. In particular, show a few data type related functions, some date and time functions, and perhaps some string functions. For examples of more complex functions, show an example such as: SELECT Name, Description FROM sys.fn_helpcollations() WHERE Name like 'L%' AND Description LIKE '% binary sort';

6 Lesson 2: Designing and Implementing Scalar Functions
20762B Lesson 2: Designing and Implementing Scalar Functions 10: Designing and Implementing User-Defined Functions Demonstration: Working with Scalar Functions Question Which of these data types can be returned by a scalar function used in managed code? ( )Option 1: rowversion ( )Option 2: table ( )Option 3: cursor ( )Option 4: integer Answer (√) Option -2: rowversion (√) Option -2: integer True or false? A deterministic function is one that may return different results for the same set of input values each time it is called, even if the database remains in the same state. ( )False ( )True (√)False

7 What Is a Scalar Function?
20762B What Is a Scalar Function? 10: Designing and Implementing User-Defined Functions Scalar functions: Return a single data value Can return any data type except rowversion, cursor, and table when implemented in Transact-SQL Can return any data type except for rowversion, cursor, table, text, ntext, and image when implemented in managed code Mention that user-defined scalar functions return a single data value of the type defined in the RETURNS clause. Finally mention that the return type can be any data type except rowversion, cursor and table. Mention that scalar functions that are created in managed code have slightly different limitations on returned data types. Do not, however, dwell on managed code at this point because it is covered in Module 13: Implementing Managed Code in SQL Server.

8 Creating Scalar Functions
20762B Creating Scalar Functions 10: Designing and Implementing User-Defined Functions Scalar UDFs: Return a single data type from a database Usually include parameters Use two-part naming Stop on error Cannot have side effects CREATE FUNCTION must be the only statement in a batch By creating scalar user-defined functions you can extend the set of functions supplied with T-SQL. Most application-related functions would also need to have parameters passed to them. Return type must be specified and BEGIN and END are required to enclose the function. You may not change database data in a function; you should not call a stored procedure, and you cannot execute dynamic SQL.

9 Deterministic and Nondeterministic Functions
20762B Deterministic and Nondeterministic Functions 10: Designing and Implementing User-Defined Functions Deterministic functions Always return the same result given the same input (and the same database state) Nondeterministic May return different results given a specific input Built-in functions Can be deterministic or nondeterministic Mention that some built-in functions are deterministic and some are not. Finally, talk about how to establish whether a user-defined function is deterministic or not. There are several properties of user-defined functions that establish the ability of the SQL Server Database Engine to index the results of the function, either through indexes on computed columns that call the function, or through indexed views that reference the function. The determinism of a function is one such property. For example, a clustered index cannot be created on a view if the view references any nondeterministic functions. Built-in Function Determinism You cannot influence the determinism of any built-in function. Each built-in function is deterministic or nondeterministic, based on how the function is implemented by SQL Server. For example: The CAST function is deterministic unless used with datetime, smalldatetime, or sql_variant.

10 Demonstration: Working with Scalar Functions
20762B Demonstration: Working with Scalar Functions 10: Designing and Implementing User-Defined Functions In this demonstration, you will see how to: Work with scalar functions Preparation Steps Ensure that the 20762B-MIA-DC and 20762B-MIA-SQL virtual machines are running and, if not, then log on to 20762B-MIA-SQL as AdventureWorks\Student with the password Pa$$w0rd. Demonstration Steps Ensure that the 20762B-MIA-DC and 20762B-MIA-SQL virtual machines are running, and then log on to 20762B-MIA-SQL as ADVENTUREWORKS\Student with the password Pa$$w0rd. Run D:\Demofiles\Mod10\Setup.cmd as an administrator. In the User Access Control dialog box, click Yes. Wait for setup.cmd to complete successfully. On the taskbar, click Microsoft SQL Server Management Studio. In the Connect to Server dialog box, in Server name, type MIA-SQL and then click Connect. On the File menu, point to Open, and then click Project/Solution. In the Open Project dialog box, navigate to D:\Demofiles\Mod10\Demo10.ssmssqlproj, and then click Open. In Solution Explorer, expand the Queries folder, and then double-click 21 - Demonstration 2A.sql. Select the code under Step A to use the tempdb database, and then click Execute. Select the code under Step B to create a function that calculates the end date of the previous month, and then click Execute. Select the code under Step C to query the function, and then click Execute. Select the code under Step D to establish if the function is deterministic, and then click Execute. Select the code under Step E to drop the function, and then click Execute. (More notes on the next slide)

11 Lesson 3: Designing and Implementing Table-Valued Functions
10: Designing and Implementing User-Defined Functions Demonstration: Implementing Table-Valued Functions Question True or false? Both scalar functions and TVFs can return a table that might contain many rows of data, each with many columns—TVFs are a simpler way of returning data in tables. ( )False ( )True Answer (√)False You have learned that TVFs return tables. What are the two types of TVF and how do they differ? An inline TVF returns a table defined by one SELECT statement. A multistatement TVF is defined by multiple SELECT statements. Unlike scalar functions, TVFs return a table that can contain many rows of data, each with many columns. Table-Valued Functions There are two types of TVFs you can deploy. Inline TVFs These return an output table that is defined by a RETURN statement consisting of a single SELECT statement. Multistatement TVFs If the logic of the function is too complex to include in a single SELECT statement, you need to implement the function as a multistatement TVF. Multistatement TVFs construct a table within the body of the function, and then return the table. They also need to define the schema of the table to be returned. You can use both types of TVF as the equivalent of parameterized views.

12 What Are Table-Valued Functions?
10: Designing and Implementing User-Defined Functions Table-valued functions TVFs return a TABLE data type Inline TVFs have a function body with only a single SELECT statement Multistatement TVFs construct, populate, and return a table within the function TVFs are queried like a table TVFs are often used like parameterized views Stress that while views are useful, users often wish they could parameterize them.

13 Inline Table-Valued Functions
10: Designing and Implementing User-Defined Functions Inline table-valued functions Returns a single result set There is no function body with BEGIN and END The returned table definition taken is from the SELECT statement Can be seen as a parameterized view On this slide, students will learn about inline table-valued functions. Mention that user-defined table-valued functions return a table data type. Also mention that, for an inline table-valued function, there is no function body; the table is the result set of a single SELECT statement. Highlight the fact that you do not explicitly define the returned table. The definition is based on the SELECT statement involved.

14 Multistatement Table-Valued Functions
10: Designing and Implementing User-Defined Functions Function body is enclosed by BEGIN and END Definition of returned table must be supplied Table variable is populated within function body and then returned On this slide, you will learn about multistatement table-valued functions. For a quick demo, you might consider executing the example code to show how to create a multistatement table-valued function. Mention that, for a multistatement table-valued function, the function body, defined in a BEGIN...END block, contains a series of Transact-SQL statements that build and insert rows into the table that will be returned. Also mention that multistatement table-valued functions differ from regular table-valued functions in the following ways: Multistatement table-valued functions have a function body. They declare a table-type variable at the beginning of the function. They pass rows from multiple select statements into the returned table.

15 Demonstration: Implementing Table-Valued Functions
10: Designing and Implementing User-Defined Functions In this demonstration, you will see how to: Implement TVFs Preparation Steps Complete the previous demo. Demonstration Steps In Solution Explorer, expand the Queries folder, and then double-click 31 - Demonstration 3A.sql. Select the code under Step A to use the AdventureWorks database, and then click Execute. Select the code under Step B to create a table-valued function, and then click Execute. Select the code under Step C to query the function, and then click Execute. Select the code under Step D to use CROSS APPLY to call the function, and then click Execute. Select the code under Step E to drop the function, and then click Execute. Close SQL Server Management Studio without saving any changes.

16 Lesson 4: Considerations for Implementing Functions
20762B Lesson 4: Considerations for Implementing Functions 10: Designing and Implementing User-Defined Functions Demonstration: Controlling the Execution Context Question True or false? The underuse of scalar functions is a common cause of performance problems in SQL Server systems. ( )False ( )True Answer (√)False True or false? Before you can create a function that executes as another user, you need to have IMPERSONATE permission on that user, or be part of the dbo role. (√)True (More notes on the next slide)

17 Performance Impacts of Scalar Functions
20762B Performance Impacts of Scalar Functions 10: Designing and Implementing User-Defined Functions The code for scalar functions is not incorporated into the query Different to views where the code is incorporated into the query Scalar functions used in SELECT lists or WHERE clause predicates can impact performance With scalar user-defined functions, the code that defines the function is not incorporated directly into the surrounding query. Performance problems with these functions are well-known in the SQL Server community. Particular impacts are caused when the functions are executed for each row in a table. CROSS APPLY is used to call a table-valued function for each row in a table. It’s worth noting that many people in the SQL Server community advise against almost all use of user- defined functions.

18 Performance Impacts of Table-Valued Functions
10: Designing and Implementing User-Defined Functions The code for inline TVFs is incorporated into the surrounding query The code for multistatement TVFs is not incorporated into the surrounding query Performance can be poor except where executed only once in a query Very common cause of performance problems CROSS APPLY can cause TVFs to be repeatedly executed With table-valued user-defined functions, the code that defines the function might not be incorporated directly into the surrounding query. Performance problems with these functions are well-known in the SQL Server community. It’s worth noting that many people in the SQL Server community advise against almost all use of user- defined functions.

19 Controlling the Execution Context
20762B Controlling the Execution Context 10: Designing and Implementing User-Defined Functions If a function and a table have the same owner, users can be given access to data via the function even if they do not have access to the table Enables filtered access to tables If a function and a table have different owners, a user will not have access to data via the function, even if the owner of the function has access to the table On this slide, you will learn about the features that comprise execution context. First, describe how execution context establishes the identity against which permissions to execute statements or perform actions are checked. You should then talk about how execution context is represented by a login token and a user token. Walk through the two examples shown on the slide. Execution context is determined by the user or login connected to the session, or executing (calling) a module. It establishes the identity against which permissions to execute statements or perform actions are checked. Execution context is represented by a pair of security tokens: a login token and a user token. The tokens identify the primary and secondary principals against which permissions are checked and the source used to authenticate the token. A login connecting to an instance of SQL Server has one login token and one or more user tokens, depending on the number of databases to which the account has access. User and Login Security Tokens A security token for a user or login contains the following: One server or database principal as the primary identity. One or more principals as secondary identities. Zero or more authenticators. The privileges and permissions of the primary and secondary identities. The tokens are as follows: Login Token: A login token is valid across the instance of SQL Server. It contains the primary and secondary identities against which server-level permissions and any database-level permissions associated with these identities are checked. The primary identity is the login itself. The secondary identity includes permissions inherited from rules and groups. User Token: A user token is valid only for a specific database. It contains the primary and secondary identities against which database-level permissions are checked. The primary identity is the database user itself. The secondary identity includes permissions inherited from database roles. User tokens do not contain server-role memberships and do not honor the server-level permissions granted to the identities in the token, including those that are granted to the server-level public role. (More notes on the next slide)

20 The EXECUTE AS Clause The EXECUTE AS clause: Enables impersonation
10: Designing and Implementing User-Defined Functions The EXECUTE AS clause: Enables impersonation Provides access to modules via impersonation Can be used to impersonate server-level principals or logins via the EXECUTE AS LOGIN statement Can be used to impersonate database level principals or users via the EXECUTE AS USER statement Mention that the EXECUTE AS clause sets the execution context of a session, then discuss the four points on the slide to fully describe what the EXECUTE AS clause does. Mention what types of permissions are available and how to use them with various users and logins. Step through each of the options that can be used with EXECUTE AS.

21 Guidelines for Creating Functions
20762B Guidelines for Creating Functions 10: Designing and Implementing User-Defined Functions Determine function type Create one function for one task Qualify object names inside function Consider the performance impacts of how you intend to use the function Particular issues exist with the inability to index function results Functions cannot contain structured exception handling On this slide, you will review guidelines for creating user-defined functions. Stress that following these guidelines will help avoid problems and ensure that the new functions operate as intended. Consider the following guidelines when you create user-defined functions: Determine and use the type of function that is most suitable to your needs. Stress the importance of qualifying object names within functions (and within views, stored procedures, and so on). Create each function to accomplish a single task. This makes the function easier to maintain and to debug any problems. Stress the need to consider performance and note that it will be discussed in the slides that follow. Consider the ability of SQL Server to index the results of your function. Factors such as determinism, precision, and data access, affect the ability of SQL Server to index the results of your function. For example, if you need to include your function in an index on a computed column, or as part of an indexed view, you will need to ensure that your function is deterministic. Show an example of wrapping a column in a function within a WHERE clause predicate that would cause SQL Server to be unable to use otherwise useful indexes. Note that you cannot include BEGIN TRY, BEGIN CATCH, and so on, within function code.

22 Demonstration: Controlling the Execution Context
20762B Demonstration: Controlling the Execution Context 10: Designing and Implementing User-Defined Functions In this demonstration, you will see how to: Alter the execution context of a function Preparation Steps Ensure that the 20762B-MIA-DC and 20762B-MIA-SQL virtual machines are running and, if not, log on to B-MIA-SQL as AdventureWorks\Student with the password Pa$$w0rd. Demonstration Steps Alter the Execution Context of a Function Ensure that the 20762B-MIA-DC and 20762B-MIA-SQL virtual machines are running, and then log on to 20762B-MIA-SQL as ADVENTUREWORKS\Student with the password Pa$$w0rd. Run D:\Demofiles\Mod10\Setup.cmd as an administrator. In the User Access Control dialog box, click Yes. On the taskbar, click Microsoft SQL Server 2016 Management Studio. In the Connect to Server dialog box, in Server name box, type MIA-SQL, and then click Connect. On the File menu, point to Open, click Project/Solution. In the Open Project dialog box, navigate to D:\Demofiles\Mod10\Demo10.ssmssqlproj, and then click Open. In Solution Explorer, expand the Queries folder, and then double-click 41 - Demonstration 4A.sql. Select the code under Step A to use the master database, and then click Execute. Select the code under Step B to create a test login, and then click Execute. Select the code under Step C to use the AdventureWorks database and create a user, and then click Execute. Select the code under Step D to create a function with default execution context, and then click Execute. Select the code under Step E to try to add WITH EXECUTE AS, and then click Execute. Select the code under Step F to recreate the function as a multistatement table-valued function, and then click Execute. (More notes on the next slide)

23 Lesson 5: Alternatives to Functions
20762B Lesson 5: Alternatives to Functions 10: Designing and Implementing User-Defined Functions Comparing Table-Valued Functions and Views Question What is wrong with this TVF code fragment? SELECT * FROM (EXEC dbo.GetCriticalPathNodes); ( )Option 1: Incorrect syntax for a TVF. ( )Option 2: You cannot select from a stored procedure in a TVF. ( )Option 3: dbo.GetCriticalPathNodes does not exist. ( )Option 4: The statement needs more parentheses. ( )Option 5: None of these—this code is good. Answer (√) Option -2: You cannot select from a stored procedure in a TVF. True or false? You can update views, inline TFVs, and multistatement TVFs. ( )False ( )True (√)False

24 Comparing Table-Valued Functions and Stored Procedures
10: Designing and Implementing User-Defined Functions Both can often achieve similar outcomes Some source applications can only call one or the other Functions Can have their output consumed more easily in code Can return table output in a variable Cannot have data-related side effects Often cause significant performance issues when they are multistatement functions Stored procedures Can alter the data Can execute dynamic SQL statements Can include detailed exception handling Can return multiple result sets On this slide, you review the comparison between table-valued functions and stored procedures. Note that not all stored procedures can be rewritten as table-valued functions anyway. To do this, the following conditions must be met: The logic is expressible in a single SELECT statement but is a stored procedure, rather than a view, only because of the need for parameters. This scenario can be handled with an inline table-valued function. The stored procedure does not perform update operations, except to table variables. There is no need for dynamic EXECUTE statements. The stored procedure returns one result set. The primary purpose of the stored procedure is to build intermediate results that are to be loaded into a temporary table, which is then queried in a SELECT statement.

25 Comparing Table-Valued Functions and Views
10: Designing and Implementing User-Defined Functions Both can often achieve similar outcomes Views Can be consumed by almost all applications Are very similar to tables Can be updatable Can have INSTEAD OF triggers associated with them TVFs Are like parameterized views Can often lead to significant performance problems Can be updatable when inline Avoid multistatement TVFs if there is any option to apply the same logic inline Table-valued functions can also be used in similar places to views. Stress that, while most client applications can query views, not all can pass parameters to table-valued functions. Note that, instead of depending upon filters being applied to views, TVFs enforce the need for parameters. INSTEAD OF triggers allow us to create updatable views, usually for views based on multiple base tables. No such concept exists for table-valued functions, as they may not modify database data. (They can modify data in table variables within the functions.) In general, the potential performance problems associated with user-defined functions should not be underestimated.

26 Lab: Designing and Implementing User-Defined Functions
Exercise 2: Modify an Existing Function Exercise 1: Format Phone Numbers Your manager has noticed that different users tend to format phone numbers that are entered into the database in different ways. She has asked you to create a function that will be used to format the phone numbers. You need to design, implement, and test the function. The main tasks for this exercise are as follows: Review the design requirements. Design and create the function. Test the function. Instructor Note: SQL code used in this lab is in the D:\LabFiles\Lab10\Solution folder. Exercise 2: Modify an Existing Function An existing function, dbo.StringListToTable, takes a comma-delimited list of strings and returns a table. In some application code, this causes issues with data types because the list often contains integers rather than just strings. Review the requirements. Test the function by using an alternate delimiter, such as the pipe character (|). Logon Information Virtual machines: 20762B-MIA-SQL User name: ADVENTUREWORKS\Student Password: Pa$$w0rd Estimated Time: 30 minutes

27 20762B Lab Scenario 10: Designing and Implementing User-Defined Functions The existing marketing application includes some functions. Your manager has requested your assistance in creating a new function for formatting phone numbers—you also need to modify an existing function to improve its usability.

28 20762B Lab Review 10: Designing and Implementing User-Defined Functions In this lab, you have learned how to assess function design requirements, design functions, create functions, and change functions.

29 Module Review and Takeaways
20762B Module Review and Takeaways 10: Designing and Implementing User-Defined Functions Best Practice Review Question(s) Question When you are using the EXECUTE AS clause, what privileges should you grant to the login or user that is being impersonated? Answer For the login or user that is being impersonated, specify a login or user that has the least privileges needed to perform the operations that are required in the session. For example, do not specify a login name with server-level permissions if only database-level permissions are required. Also, do not specify a database owner account unless those permissions are required. When you are using the EXECUTE AS clause, what privileges should you grant to the login or user who is creating the code? You should grant IMPERSONATE permission for the login or user who is creating the code. Best Practice: When working with functions, consider the following best practices: Avoid calling multistatement TVFs for each row of a query. In many cases, you can dramatically improve performance by extracting the code from the query into the surrounding query. Use the WITH EXECUTE AS clause to override the security context of code that needs to perform actions that the user who is executing the code does not have.


Download ppt "Designing and Implementing User- Defined Functions"

Similar presentations


Ads by Google