Important concepts related to the function in SQL Server are:
- Deterministic and Nondeterministic function
- Encrypting function
- Schema binding function
Deterministic Function
Deterministic function always returns the same result, any time they are called with the specific set of the input values and the same state of the database.
Example: Count (), Power (), Sum (), Avg() and Square().
- select * from Employee
If you locked this Employee table, there are two rows in this table. Execute the code, given below:
- select COUNT(*) TotalEmployee from Employee
Count function returns the total number of the records in the table in the employee. There are two records in the table, so count function returns two; no matter how many times you execute this query, it will return two. Now, if I change the database state; it means to insert new records or delete records. Afterwards, try the count command and it will change.
Let’s look at another example, Square() Function, which returns the square of the given number.
- select SQUARE(3) [Square]
If we execute this query, it will return nine -- every time you'll get nine as an output.
Nondeterministic Function
Nondeterministic function is just the opposite of the Deterministic function. Nondeterministic function may return a different result each time, it is called with a specific set of the input values, even if the database state remains the same.
Example: Getdate () and current_timestamp
- select current_timestamp
Every time, we execute it, it returns a different output.
Rand () Function
Rand () Function is a non-deterministic function, but if you provide the seed value, the function becomes Deterministic, as the same value returns the same seed value.
Example: When I say select Rand, this function expects an integer value, as input parameter is known as a seed value.
Let’s pass one as a seed value and if anyone executes this query, it returns the same output, every time.
- select RAND(1)
We can see that this function now works as a Deterministic function.
On the other hand, if we do not provide the seed value, Rand function works as a Non-Deterministic function, given below:
- select RAND()
Every time this function returns a different value.
Encrypting a function definition using WITH ENCRYPTION OPTION
If we want another user to not see your created function and not be able to modify it, it is possible with the encryption, as once its encrypted, you cannot view the text of the function using the “sp_helptext” system store procedure. If you try to, you will get a message that states ‘The text for object is encrypted.’
Example: Let’s create a scalar function,

Anu VPosted Jul 28, 2016, 8:11 AM
Nice
Prasanna MuraliPosted Jul 5, 2016, 12:09 PM
Nice one...
Saineshwar BageriPosted Jul 4, 2016, 1:08 PM
Nice one
Vignesh ManiPosted Jul 4, 2016, 11:08 AM
Nice one
kalu singh raoPosted Jul 4, 2016, 1:36 AM
Nice...
Yashwant VishwakarmaPosted Jul 4, 2016, 1:04 AM
Nice share , Thanks for sharing !!
Dharmesh SinghPosted Jul 4, 2016, 12:49 AM
Thanks.
RajaPosted Jul 4, 2016, 12:35 AM
Nice Share...
SubashPosted Jul 4, 2016, 12:34 AM
Thanks Dharesh How we decrypt the encrypted functions and stored procedure?