Monday 3 December 2012

How to call a function from stored procedure in SqlServer 2008.


In this post , You will see how to call a function  from stored procedure in SqlServer 2008 and 2012. In this post I have written a function named Multiply which accepts two parameters and return a single parameter. Then I have called a function from stored procedure. So let’s take a look as practical.
There are two type of function in sql server
·        System defined function
·        User defined function
Here I am taking about user defined function.
Used defined functions are three types as Scalar function, Table valued function and InLine valued function.
Now follow the Step……………………..

Create a user defined function in sql server :

create function Multiply
(
@no1 int,
@no2 int
)
returns int
as
begin
declare @result int // Declare int type
select @result=@no1*@no2;
return @result // return int
end
Execute the function.

Create Stored procedure in Sql Server:
create procedure callingFunction
(
@fno int,
@sno int
)
as
begin
declare @setval int
select dbo.Multiply(@fno,@sno)
end

Execute the stored procedure.
Now execute the stored procedure with duplicate value to see the result.

declare @retval int // declare int type
exec @retval=dbo.callingFunction
@fno=4,
@sno=5

Pres F5 to see the result. See the figure below..



No comments:

Post a Comment

Note: only a member of this blog may post a comment.