---
title: "What is a function in SQL? Create a function that returns stu_Name when passed stu_ID."  
description: "What is a function in SQL? Create a function that returns stu_Name when passed stu_ID."  
author: "Revati S Misra"  
published: 2023-03-24  
updated: 2023-04-10  
canonical: https://www.mindstick.com/forum/157562/what-is-a-function-in-sql-create-a-function-that-returns-stu_name-when-passed-stu_id  
category: "mssql server"  
tags: ["database", "mysql", "mssql server", "sql server", "sql"]  
reading_time: 2 minutes  

---

# What is a function in SQL? Create a function that returns stu_Name when passed stu_ID.

What is a [function in SQL](https://www.mindstick.com/forum/156839/what-is-function-in-sql-server)? Create a [function that returns](https://www.mindstick.com/forum/160058/create-an-arrow-function-that-returns-a-random-number-between-1-and-100) stu_Name when passed stu_ID of any specific user from the [Student table](https://www.mindstick.com/forum/157561/what-is-the-stored-procedure-create-a-procedure-to-find-the-record-by-stu_id-from-the-student-table).

## Replies

### Reply by Krishnapriya Rajeev

In [SQL](https://www.mindstick.com/articles/13115/types-of-keys-in-sql-or-oracle-database), a [function](https://www.mindstick.com/articles/13001/multi-statement-table-valued-user-defined-function-in-sql-server) 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:

```plaintext
CREATE FUNCTION get_student_name (student_id INT)
RETURNS VARCHAR(50) AS
BEGIN
DECLARE student_name VARCHAR(50);
SELECT stu_Name INTO student_name FROM students WHERE stu_ID = student_id;
RETURN student_name;
END;
```

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](https://www.mindstick.com/articles/157138/student-loan-default-wage-garnishment-and-student-loan) 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:

```plaintext
SELECT get_student_name(12);
```

This will return the name of the student with ID 12.


---

Original Source: https://www.mindstick.com/forum/157562/what-is-a-function-in-sql-create-a-function-that-returns-stu_name-when-passed-stu_id

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
