GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Testing  >  DataBase Testing
Go To First  |  Previous Question  |  Next Question 
 DataBase Testing  |  Question 29 of 50    Print  
What is the difference between functions & procedures?

  
Total Answers and Comments: 8 Last Update: May 29, 2008     Asked by: rupalikadam 
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: Mike
 

This discussion pertains to SQL Server, but should apply to other database products as well.

A function returns a value directly. The value will replace the function in the location the function was called from. For example, in the expression
  x = fn(y) + 3
the value of y will be passed to the function fn, which will then calculate a result and pass the value back to the expression. Suppose fn(y) results in a value of 7. The previous expression will then be equivalent to
  x = 7 + 3
A function cannot change the state of the system. Any changes made in the body of the function must be undone when the function exits.
A stored procedure can change the state of the system. Stored procedures are the workhorses of a database application. Although it is not the purpose of a stored procedure to return a value, a store proc can return a single status value (usually an integer) to indicate success or failure, and it can return one or more values through output parameters. A major difference between the usage of a function and a stored procedure is the syntax for capturing the return values. A function can be used anyplace you would use a value, as shown in the previous example. The output values from a store procedure must first be captured in variables, then used in expressions:
  declare @status int
  declare @out1  varchar(10)
  declare @out2  int
  execute @status = prTest @out1 output, @out2 output
  select "Return status: ", @status
  select "Output 1: ", @out1
  select "Output 2: ", @out2

Getting and using  a return value from a stored proc is usually a three-step process:
  1. Declare one or more variables to capture the return value(s).
  2. Execute the stored proc and capture the return value(s).
  3. Do something with the return value(s). You could skip this step, but then steps 1 and 2 would be a waste of time.



Above answer was rated as good by the following members:
ramumcato
November 04, 2006 03:49:47   #1  
suresh        

RE: What is the difference between functions & procedu...
functions returns value whereas procedures doesn't
 
Is this answer useful? Yes | No
November 07, 2006 01:50:56   #2  
boyina Member Since: January 2006   Contribution: 45    

RE: What is the difference between functions & procedu...

Hi Suresh

I am not agreee with your answer because Procedure also will return the value as same as Function. tha difference between function and procedure is always function returns the value and procedure return the value only once


 
Is this answer useful? Yes | No
November 09, 2006 05:03:47   #3  
Ashok Kumar Prasad        

RE: What is the difference between functions & procedu...

Hi

Difference between functions and procedure are as follows:

1. Print statement is not valid within the function because functions are meant for process or execute group of statement and return a value where as procedure are meant for executing group of statement and display output.

2. Return keyword is use to return a value from a stored function with specified data type of the value expecting to be returned from stored function.

3. One of the major advantage of stored function is that can be used in the query directly where as an stored procedure can never be used in a query as it is not returning any value.


 
Is this answer useful? Yes | No
December 08, 2006 01:34:03   #4  
thangamani        

RE: What is the difference between functions & procedu...
according to me functions returns a value but procedure doesnot if procedure returns i need to know how it returns please give explanation.thank you
 
Is this answer useful? Yes | No
December 15, 2006 03:09:57   #5  
sreenivasaRao        

RE: What is the difference between functions & procedu...

Hi all

Function having return type but Procedure does n't


 
Is this answer useful? Yes | No
January 05, 2007 17:20:48   #6  
Mike        

RE: What is the difference between functions & procedu...

This discussion pertains to SQL Server but should apply to other database products as well.

A function returns a value directly. The value will replace the function in the location the function was called from. For example in the expression
x fn(y) + 3
the value of y will be passed to the function fn which will then calculate a result and pass the value back to the expression. Suppose fn(y) results in a value of 7. The previous expression will then be equivalent to
x 7 + 3
A function cannot change the state of the system. Any changes made in the body of the function must be undone when the function exits.
A stored procedure can change the state of the system. Stored procedures are the workhorses of a database application. Although it is not the purpose of a stored procedure to return a value a store proc can return a single status value (usually an integer) to indicate success or failure and it can return one or more values through output parameters. A major difference between the usage of a function and a stored procedure is the syntax for capturing the return values. A function can be used anyplace you would use a value as shown in the previous example. The output values from a store procedure must first be captured in variables then used in expressions:
declare @status int
declare @out1 varchar(10)
declare @out2 int
execute @status prTest @out1 output @out2 output
select Return status: @status
select Output 1: @out1
select Output 2: @out2

Getting and using a return value from a stored proc is usually a three-step process:
1. Declare one or more variables to capture the return value(s).
2. Execute the stored proc and capture the return value(s).
3. Do something with the return value(s). You could skip this step but then steps 1 and 2 would be a waste of time.


 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
August 28, 2007 03:20:12   #7  
krishna pal singh        

RE: What is the difference between functions & procedu...
Function always return a values using the return statement but a procedure may or may not return values
 
Is this answer useful? Yes | No
May 29, 2008 12:19:04   #8  
ramumcato Member Since: May 2008   Contribution: 7    

RE: What is the difference between functions & procedures?
Function returns some value where as procudure doesn't return any value.
 
Is this answer useful? Yes | No


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape