HOW TO CREATE SCALAR VALUED FUNCTION IN SQL SERVER.

Ankit Singh

Total Post:341

Points:2389
Posted by  Ankit Singh
 724  View(s)
Ratings:
Rate this:
Can anyone please help me how to solve this problem.
  1. aditya kumar Patel

    Post:254

    Points:1810
    Re: how to create scalar valued function in sql server.

    
    
    

    --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

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!