Question: I have a stored procedure like this:

```
create proc calcaulateavaerage
@studentid int
as
begin

-- some complicated business and query

return @result  -- single decimal value 
end

```

and then I want to 

create proc the whole result

```
select * , ................................ from X where X.value >  (calculateaverage X.Id)

```

It always gives an error that reads like "multi-part identifier calculateaverage couldn't be bound." Any idea how to solve that?

Answer: 
You don't want a stored procedure. You want a function.
Use Output variables to output the data out of stored procedure:

```
create proc calcaulateavaerage
@studentid int, @result int
as
begin

-- some complecated business and query

select @result = id from sometable;
end

-- Declaring output variable named result;
declare @result int;
-- Passing output variable to stored procedure.
exec calculateaverage 1, @result;

-- Now you can display the result or do whatever you like.
print @result 

```