12 April 2017

SQL Server: Functions trong SQL - User Defined Functions

scalar1
The above function returns an integer value. To test this function, we will write some code as shown below – 

Let us see another example which will fetch the number of orders processed by an employee for a given year. Write the following function in our query pad – 

We will test this function with different years for an employee as shown below – 


Table Valued Functions

Now let’s try an Inline Table valued function. Inline Table valued functions can return a row set using SQL Server Table datatype. You cannot perform addition logic in inline table valued functions. We will fetch the product details purchased by a customer as shown below – 

To test this example we will use a select statement as shown below – 

Another example of the Inline Table Valued Function is as shown below – 

To test this function, we will use different years as shown below – 


We will now see a Multi-Statement Table Valued Function. This function can be used to perform additional logic within the function. The code is as shown below – 


To use the Multi-Statement Table Valued function, use this code – 

tvfmultistatementtest

0 nhận xét:

Post a Comment

 

BACK TO TOP

Xuống cuối trang