Blog_Banner_Asset
    Homebreadcumb forward arrow iconBlogbreadcumb forward arrow iconFull Stack Developmentbreadcumb forward arrow iconScalar Function: How to Create, Call, Modify & Remove Scalar Functions?

Scalar Function: How to Create, Call, Modify & Remove Scalar Functions?

Last updated:
26th May, 2021
Views
Read Time
6 Mins
share image icon
In this article
Chevron in toc
View All
Scalar Function: How to Create, Call, Modify & Remove Scalar Functions?

What are Scalar Functions?

SQL is one of the most commonly used languages for database management systems. The language is primarily used to enter and fetch data from repositories. Just like other programming languages, SQL has a set of functions. 

In SQL Server there are two major types of functions to manipulate data:

  • System defined functions
  • User-defined functions

System-defined functions are built-in functions predefined, and their functionality cannot be changed. On the other hand, user-defined functions are the ones that can be designed to perform a custom task as per our needs.

Check out our free courses to get an edge over the competition

Ads of upGrad blog

SQL Server provides three types of user-defined functions

  • Simple table-valued function
  • Scalar table-valued function
  • Multi-statement table-valued function

In this article, we will talk about the scalar value function (user-defined functions) and learn how to create it.

A scalar value function in SQL takes more than one parameter and returns a single value of any data type.  To create a scalar value function, the “Create” statement is used. These types of functions are included in the code to simplify the code. 

For example, you may need to carry out a challenging calculation at various steps in your code. Instead of writing the code, again and again, you can simply create a scalar function and encapsulate the code in it. Now, all you need to do for carrying out that tedious calculation is to call the scalar function with a simple syntax.

 Remember – The name of a scalar value function cannot be more than 128 characters and must start with the prefix fn for convenience. 

How to Create a Scalar Function?

For creating a scalar function, you need to use the “Create function” statement as mentioned below:

1 CREATE FUNCTION [schemaName.]fnName (parameterlist)

2 RETURNS data_type AS

3 BEGIN

4   statements

5   RETURN value

6 END

Check out upGrad’s Advanced Certification in Cloud Computing 

Explore Our Software Development Free Courses

Understanding the Syntax

Line 1 CREATE FUNCTION is the command telling the SQL server to create a scalar function. Here, the schema name is optional; if not specified, SQL uses dbo by default. The schema name is followed by the function’s name, which must have parameters enclosed within the parenthesis.

Line 2 – Specifies return value’s data type in the RETURN statement.

Line 3 –  Specifies return statements to return value inside the function’s body.  

Example

The following example creates a function that calculates the total sales based on the list price, quantity, and discount:

CREATE FUNCTION sales.TotalSale(

    @quantity QTY,

    @listprice JAN(15,4),

    @disnt Jan(13, 2)

Check out upGrad’s Advanced Certification in Cyber Security

)

RETURNS JAN(15,4)

AS 

BEGIN

    RETURN @quantity * @list_price * (1 – @disnt);

END;

This function can be used later to compute the total sales of any sales order from the database.

How to Call a Scalar Function?

A scalar function can be called just like a built-in function. 

Example: The statements mentioned below clearly show how to call the TotalSale () function:

SELECT 

    sales.TotalSale(10,100,0.1) total_sale;

How to Modify a Scalar Function?

In order to modify the scalar function, the keyword ALTER is used instead of CREATE. All the other statements after that remain the same:

ALTER FUNCTION [schemaName.]fnName (parameterList)

    RETURN data_type AS

    BEGIN

        statements

        RETURN value

    END

upGrad’s Exclusive Software Development Webinar for you –

SAAS Business – What is So Different?

Explore our Popular Software Engineering Courses

How to Remove a Scalar Function? 

DROP FUNCTION statement is used to remove a scalar function that already exists:

Here is the syntax of the function:

DROP FUNCTION [schemaName.]fnName;

For example, to remove the TotalSale( ) function, you can use the following statement:

DROP FUNCTION TotalSale

Scalar functions are one of the most commonly used functions in SQL. These functions work like built-in functions but are actually user-defined. You can create a number of functions if a set of statements needs to be repeated in your code. All you need to do after that is call the function and pass the appropriate parameters. Here are some key takeaways from SQL Scalar functions:

  • You can use scalar functions anywhere in the SQL statements.
  • Scalar functions use logics like  WHILE loops and IF blocks.
  • Scalar functions can call other functions.
  • Scalar functions cannot access data.
  • Scalar functions should have a RETURN value because they accept more than one parameter but return only a single value.

Learn Software Courses online from the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs, or Masters Programs to fast-track your career.

Conclusion

Ads of upGrad blog

Creating scalar functions in your programs is a good practice in SQL. Scalar functions are one of the best and effective ways to make the code more accessible for anyone to read. We hope that the article has helped you learn about scalar functions. If you are looking forward to becoming a programmer and have comprehensive knowledge of the subject, consider taking courses from upGrad. upGrad offers the best computer science courses from leading global universities. 

In-Demand Software Development Skills

If you’re interested to learn more about full-stack software development, check out upGrad & IIIT-B’s Executive PG Programme in Software Development – Specialisation in Full Stack Development which is designed for working professionals and offers 500+ hours of rigorous training, 9+ projects, and assignments, IIIT-B Alumni status, practical hands-on capstone projects & job assistance with top firms.

Read our Popular Articles related to Software Development

Profile

Rohan Vats

Blog Author
Software Engineering Manager @ upGrad. Passionate about building large scale web apps with delightful experiences. In pursuit of transforming engineers into leaders.

Frequently Asked Questions (FAQs)

1What are functions in SQL?

Function is a special type of procedure in SQL that returns a value. Function returns a single value, returns multiple values in a list, or aggregates a set of rows into a single value. In other words, it makes a calculation on the fly, rather than storing the information in the database, so that your query can proceed rapidly. Functions are the basic set of defined predicates in SQL, which are used to compose SQL statements. Functions are categorized in to scalar and Aggregate functions. Scalar functions are the ones that return a single value from an expression.

2What is SQL?

SQL stands for Structured Query Language. It is a special programming language which is used for managing data. It is used in most of the database systems. The data can be in the form of text, numbers, images and videos. SQL language is used to perform data query and data entry. SQL language is one of the most powerful languages. It is used to store data in the database. It is supported by various database systems like MySQL, Oracle, PostgreSQL, etc. These systems can be used to store large amount of data which can be used to form the database. It is the core language for database management systems. It is universal language for database systems. The database is structured by the SQL, so it is called structured query language. SQL is used for database management and to manage the database operations. It is a standard language for database query and data entry.

3What is the difference between SQL and MongoDB?

SQL ( Structured Query Language ) is a programming language designed for managing data in relational database management systems . SQL is one of the most important and widely used programming language in the world. It is a standard for storing, retrieving and managing data in relational database management systems. For example, Google, Amazon, Facebook, Instagram, Twitter are using SQL to store and manage billions of user data. MongoDB is a non-relational database. It is a document based and schema-free database. Data in MongoDB is stored in the form of JSON-like documents.

Explore Free Courses

Suggested Blogs

Top 7 Node js Project Ideas & Topics
31546
Node.JS is a part of the famous MEAN stack used for web development purposes. An open-sourced server environment, Node is written on JavaScript and he
Read More

by Rohan Vats

05 Mar 2024

How to Rename Column Name in SQL
46899
Introduction We are surrounded by Data. We used to store information on paper in enormous file organizers. But eventually, we have come to store it o
Read More

by Rohan Vats

04 Mar 2024

Android Developer Salary in India in 2024 [For Freshers & Experienced]
901298
Wondering what is the range of Android Developer Salary in India? Software engineering is one of the most sought after courses in India. It is a reno
Read More

by Rohan Vats

04 Mar 2024

7 Top Django Projects on Github [For Beginners & Experienced]
51987
One of the best ways to learn a skill is to use it, and what better way to do this than to work on projects? So in this article, we’re sharing t
Read More

by Rohan Vats

04 Mar 2024

Salesforce Developer Salary in India in 2024 [For Freshers & Experienced]
909120
Wondering what is the range of salesforce salary in India? Businesses thrive because of customers. It does not matter whether the operations are B2B
Read More

by Rohan Vats

04 Mar 2024

15 Must-Know Spring MVC Interview Questions
34720
Spring has become one of the most used Java frameworks for the development of web-applications. All the new Java applications are by default using Spr
Read More

by Arjun Mathur

04 Mar 2024

Front End Developer Salary in India in 2023 [For Freshers & Experienced]
902367
Wondering what is the range of front end developer salary in India? Do you know what front end developers do and the salary they earn? Do you know wh
Read More

by Rohan Vats

04 Mar 2024

Method Overloading in Java [With Examples]
26129
Java is a versatile language that follows the concepts of Object-Oriented Programming. Many features of object-oriented programming make the code modu
Read More

by Rohan Vats

27 Feb 2024

50 Most Asked Javascript Interview Questions & Answers [2024]
4327
Javascript Interview Question and Answers In this article, we have compiled the most frequently asked JavaScript Interview Questions. These questions
Read More

by Kechit Goyal

26 Feb 2024

Schedule 1:1 free counsellingTalk to Career Expert
icon
footer sticky close icon