+ All Categories
Home > Documents > Functions

Functions

Date post: 22-Mar-2016
Category:
Upload: ezhno
View: 27 times
Download: 0 times
Share this document with a friend
Description:
Functions. Lesson 10. Skills Matrix. Function. A function is a piece of code or routine that accepts parameters and stored as an object in SQL Server. The function always returns a result or result set from invocation. - PowerPoint PPT Presentation
Popular Tags:
22
Functions Functions Lesson 10
Transcript
Page 1: Functions

FunctionsFunctionsLesson 10

Page 2: Functions

Skills MatrixSkills Matrix

Page 3: Functions

FunctionFunction• A function is a piece of code or

routine that accepts parameters and stored as an object in SQL Server. The function always returns a result or result set from invocation.

• A function can be called within a SELECT statement or even a WHERE clause, whereas a stored procedure must be called using an EXEC[UTE] procedure statement.

Page 4: Functions

FunctionFunction• SQL Server supports several types of

functions:– Built-in functions– Scalar functions– Inline table-valued functions– Multistatement table-valued functions– CLR functions

Page 5: Functions

Built-in FunctionsBuilt-in Functions• You need to become familiar with a

large number of functions provided to you by Microsoft.

• Aggregate functions perform operations that combine multiple values into one value by grouping, summarizing, or averaging the values.

Page 6: Functions

Built-in FunctionsBuilt-in Functions

Page 7: Functions

Built-in FunctionsBuilt-in Functions• Configuration scalar functions return

information about system settings. • Cryptographic functions support

encryption, decryption, digital signing, and the validation of digital signatures. – EncryptByKey( )– DecryptByKey( )

Page 8: Functions

Built-in FunctionsBuilt-in Functions• Configuration functions include

server_name( ) and db_name( ), which gives you information about server and database configurations, respectively.

• Cursor functions return information about the status of a cursor.

• Date and time functions provide you with the capability to manipulate and calculate with dates and time values.

Page 9: Functions

Mathematical FunctionsMathematical Functions

Page 10: Functions

Ranking FunctionsRanking Functions• Ranking functions are

nondeterministic functions that return a ranking value for each row in a partition.

• Ranking functions are new with SQL Server 2005 and allow you to use a rank or a row number within a result set.

Page 11: Functions

Row Set, Security and String FunctionsRow Set, Security and String Functions• Rowset functions return the rowsets

that can be used in place of a table referenced in a Transact-SQL statement

• Security functions return information about users and roles.

• String functions manipulate character text. Once again, examine each function in turn.

Page 12: Functions

Execution ContextExecution Context• Execution context establishes the

identity against which permissions are checked.

• Without specifying the execution context, the user or login calling the module, such as a stored procedure or function, usually determines the permissions invoked.

Page 13: Functions

Three Function TypesThree Function Types• A scalar function passes and/or

returns a single value.• A multistatement table-valued

function proves to be a combination of a view and a stored procedure.

• Inline table-valued functions return a table and are referenced in the FROM clause, just like a view.

Page 14: Functions

CLR FunctionsCLR Functions• In the same way you can write managed

code procedures, you now can also write a user-defined function in any .NET programming language.

• Also, as with the scalar functions or a table-valued Transact-SQL function, a managed code (CLR) function can be scalar or table-valued.

• Before you can use a managed function, you first need to enable CLR support on the server.

Page 15: Functions

Deterministic and Nondeterministric Deterministic and Nondeterministric FunctionsFunctions• SQL Server marks a function as:

– A deterministic function always returns the same result, given a specific input value.

– A nondeterministric function always returns a different value each time invoked.

Page 16: Functions

Deterministic FunctionDeterministic Function• You can create an index on a computed

column if a function is deterministic. – This means whenever you update the row,

the index also updates, and you could gain a lot of query performance when using the function in a query expression.

• User-defined functions are deterministic when they are:– Schema-bound.– Defined with only deterministic user-

defined or built-in functions.

Page 17: Functions

CLR FunctionsCLR Functions• As with managed procedures, you

use CLR functions to perform complex calculations or conversions that are outside the scope of a data-centric environment, or to create functionality that scopes outside of SQL Server and cannot be resolved within a Transact-SQL function.

• All functions are deterministic or nondeterministic.

Page 18: Functions

Nondeterministic Built-in FunctionsNondeterministic Built-in Functions• SQL Server permits the use of

nondeterministic built-in functions within user-defined functions, with the exception of NEWID( ), RAND(, NEWSEQUENTIALID( ), and TEXTPTR( ).

Page 19: Functions

Schema BindingSchema Binding• Schema binding connects the function to the

object that it references. – All attempts to drop the object referenced by

a schema-bound function fails. – To create a function with the WITH

SCHEMABINDING option, the following must be true:

• All views and user-defined functions referenced by the function must be schema-bound as well.

• All objects referenced by the function must be in the same database.

Page 20: Functions

SummarySummary• You learned that functions have

three forms: scalar, multistatement table-valued, and inline table-valued. – Scalar types return a single value; for

example, GETDATE( ) returns the current day and time. Both forms of table-valued functions return a dataset; for example, rows and columns.

Page 21: Functions

SummarySummary• Built-in functions perform common

tasks. They have been developed over the years, and their number keeps increasing as new situations warrant the inclusion of new solutions in SQL Server.

• You can create your own functions.

Page 22: Functions

Summary for Certification ExaminationSummary for Certification Examination• Know the use of functions.

– Know when to apply any of the three forms to specific scenarios.

• Know, in general, the built-in functions. – Know, in particular, Substring( ),

Datediff( ) and Dateadd( ).• Know the consequences of including

nondeterministic functions and functions that reference alias data types.


Recommended