What is a function in SQL? Create a function that returns stu_Name when passed stu_ID of any specific user from the Student table.
What is a function in SQL? Create a function that returns stu_Name when passed stu_ID.
24824-Mar-2023
Updated on 10-Apr-2023
Krishnapriya Rajeev
10-Apr-2023In SQL, a function is a piece of code that performs a specific task and can be called from within SQL statements. A function can accept input parameters and return a value.
To create a function in SQL that returns stu_Name when passed stu_ID, you can use the following syntax:
This function is called get_student_name and takes one input parameter student_id of type integer. It returns a value of type VARCHAR(50), which is the name of the student whose ID matches the input parameter.
The function uses a local variable student_name of type VARCHAR(50) to store the name of the student retrieved from the database table students based on the input parameter value student_id. Finally, the function returns the value of student_name.
You can then call this function in a SQL statement like this:
This will return the name of the student with ID 12.