In this post, we will check one user-defined function in SQL Server 2008.
New to Office 365 SharePoint Online? Get Office 365 Enterprise E3 Subscription & Try out all the features
You can also check some useful posts like:
Get all user-defined tables in SQL Server 2008
Difference between primary key and foreign key in SQL server.
User-defined functions can be created by users.
Below is a function that will take one input parameter and also returns one integer value.
Here it will take the empid and returns the contact number for the particular empid.
Create function EmployeeContact(@Empid int)
returns int
as
begin
declare @result int
select @result=EmloyeeDetail.ContactNo from EmloyeeDetail where employee detail.Empid=@Empid
return @result
end
You can call the function like below
Select dbo.EmployeeContact(1) as ContactNumber from EmloyeeDetail
This will return the contact number of empid =1.
Here are some points to remember about functions:
– The function returns only a single value.
– The function accepts only input parameters.
– The function can not be used to Insert, Update, Delete data in database table.
– The function doesn’t support Exception handling.