RETURNS
RETURNS in SQL is used to define the data type of the result that will be returned by a function or a stored procedure. It signifies the expected output that the function or procedure will deliver when executed.
Example
Output
Explanation
In the provided example, a function called getFullName
is created, which concatenates two input texts firstName
and lastName
with a space in between. The RETURNS
keyword indicates the data type of the output from this function, which in this case is ‘text’. The function is then invoked using a SELECT statement, returning ‘John Doe’.
Example
Output
This will output the price of the product with ProductID
5, let’s say it’s 19.99.
19.99
Explanation
In the example above, a scalar function ProductPrice
is created. This function takes one parameter @ProductID
of type integer. The RETURNS
keyword is used to specify the type of value the function will return, which is DECIMAL(10,2)
in this case.
Inside the function, a variable @Price
is declared to hold the price of the product and then the Price
from the Products
table is selected where ProductID
equals the given @ProductID
. This value is assigned to @Price
. Finally, @Price
is returned as the result of the function.
When we call the function with a specific ProductID
(5 in this case), it outputs the price of that particular product.
Example
Output
Explanation
The above example illustrates the use of RETURNS in SQL. Here, a function get_EmployeeName
is created that accepts an Employee ID as input (p_id
) and returns the corresponding FirstName
from the Employees
table. In the output section, a DBMS_OUTPUT.PUT_LINE
statement is used to display the FirstName
of the employee with ID 5, which, in this example, is ‘David’.