When would you use stored procedure or functions ?

Explain the scenario of the best usage

Questions by jayshree13   answers by jayshree13

Showing Answers 1 - 1 of 1 Answers

Hi,

suppose you want top 3 salaries from emp table.

syntax for top 3 salaries:

select distinct top (3) (sal) from emp order by sal desc

we have to use this query dialy or monthly.Means have to use more times. That's why i am putting this query into stored procedure.

create proc usp_topsal
as
select
distinct top (3) (sal) from emp order by sal desc


exec usp_totalsal

we get top3 salaries from emp table. But no one want either top3 or top4

i want top n salaries, on that time i pass one input variable into stored procedure

create proc usp_topsal(@n int)
as
select
distinct top (@n) (sal) from emp order by sal desc


exec usp_totalsal 4

  Was this answer useful?  Yes

ptmich

  • May 28th, 2012
 

It is best to go for a function when a value needs to be computed but a stored procedure is useful when you need to execute business logic.

  Was this answer useful?  Yes

krishna

  • Dec 17th, 2015
 

When there is return value then we use function else we use procedure and for DMLs also

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions