Programming in MS SQL Server 2012

Scalar Function



User-defined scalar functions return a single data value of the type defined in the RETURNS clause. For an inline scalar function, there is no function body, the scalar value is the result of a single statement.

CREATE Function

The following example describes CREATE Scalar user-defined function.

-- Example 87 --

 

USE EBusiness

GO

 

IF EXISTS (SELECT *

           FROM sysobjects

           WHERE name = N'EmployeeName')

           DROP FUNCTION EmployeeName

GO

 

CREATE FUNCTION EmployeeName()

RETURNS varchar(100)

AS

BEGIN

   RETURN 'Prem Nath R.K'

END

GO

 

-- Execute Scalar User-defined Function --

 

SELECT EBusiness.dbo.EmployeeName()

 

SELECT EBusiness.dbo.EmployeeName() AS 'Employee Name'

 

SELECT 'Employee Name' = EBusiness.dbo.EmployeeName()


EXECUTE Function

To execute / call a user-defined function, you must specify the database in which it was created and the dbo.factor like DatabaseName.dbo.FunctionName( )

Query Output Screen



* * * * *


Email Your Comment To AUTHOR