Home > DeveloperSection > Forums > how to create scalar valued function in sql server.
Ankit Singh

Total Post:341

Points:2389
Posted on    October-15-2015 11:30 PM

 MSSQL Server SQL Server  SQL Server 2008  SQL Server 2012 
Ratings:


 1 Reply(s)
 310  View(s)
Rate this:
Can anyone please help me how to solve this problem.


aditya kumar Patel

Total Post:242

Points:1722
Posted on    October-16-2015 4:18 AM



--let’s start querying create a table.

 CREATE TABLE ProductMasters(

       ProductId int IDENTITY(1,1) primary key,

       ProductName nvarchar(max) ,

       Price decimal(18, 2)  ,

       CategoryId int  

 )

 --We will now create a scalar function, which returns the total of price filtered by a given  category . Write the following code in your query pad –

 Create function fnGetTotatPrice

( 

 @categoryId int

)

returns decimal(10,2)

As

Begin return ( select sum(price) from ProductMasters where CategoryId=@categoryId)

end

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

select dbo.fnGetTotatPrice(2) as price


Don't want to miss updates? Please click the below button!

Follow MindStick