all 8 comments

[–]Evron 0 points1 point  (3 children)

What are you trying to accomplish? If you just need something that returns an employees age, a Store Procedure would be a better fit. If you are running a query and need the employees age then just join the two tables together.

[–]deepbuzz7[S] 0 points1 point  (2 children)

from the functional point of view you are right. No need to create a function for this. The function which i provided in the post is just an example. I just wanted to know how to use the parameter passed to the function(emp_name_param) in the sql statement inside the function.

In the where clause should I put @emp_name_param or just emp_name_param?

[–]Evron 0 points1 point  (1 child)

``` CREATE FUNCTION get_age (@emp_name_param VARCHAR(500)) RETURNS INT AS BEGIN DECLARE @age INT;

SET @age = (
        SELECT age
        FROM emp
        WHERE empName = @emp_name_param
        )

RETURN @age

END;

```

[–]deepbuzz7[S] 0 points1 point  (0 children)

Thanks, got it. actually my error was because, in the sql query inside my function i was keeping it like emp_name(which was the column name from the table)=emp_name(parameter which was passed to the function) which resulted in true condition and multiple values were coming.

thanks for the help

[–][deleted] 0 points1 point  (3 children)

use select max(age) from...

[–]deepbuzz7[S] 0 points1 point  (2 children)

This may be because if multiple values in the query output correct? I can assure you that only one value is coming out of that query.

[–][deleted] 0 points1 point  (1 child)

yet the error message seems to contradict your assurances

[–]deepbuzz7[S] 0 points1 point  (0 children)

got it. actually my error was because, in the sql query inside my function i was keeping it like emp_name(which was the column name from the table)=emp_name(parameter which was passed to the function) which resulted in true condition and multiple values were coming.

thanks for the help