Вы находитесь на странице: 1из 8

12/24/2016 SQL Server Interview Questions: What is the difference between a User Defined Function (UDF) and a Stored Procedure (SP) in SQL Server

5   More    Next Blog» Create Blog   Sign In

SQL Server Interview Questions
C#
Boo MV ASP. C SQL W Written HR Subsc SQL Buy
Program 5
ks C NET # Server CF Test Round ribe Tutorial DVD
s

If you like this website, please share


this site using g+1 button What is the difference between a User
+5   Recommend this on Google Defined Function (UDF) and a Stored
Procedure (SP) in SQL Server
Take the Android course

Best software training institute in bangalore for SAP ABAP,
SAP BI, .Net, Informatica, Software Testing, Siebel CRM
I am Venkat and this is my website. I did training in Pragim
Technologies and got job in Dell in less than a week. PRAGIM is the
best S/W training institute in Bangalore.
Click here for Hyderabad Center Website
For further details please call 09900113931.

 
1. Stored Procedure support deffered name resolution where as functions do
not support deffered name resolution. 
 

2. User Defined Function can be used in a select statement where as you
cannot use a stored procedure in a select statement. 
 

3. UDF's cannot return Image, Text where as a StoredProcedure can return any
datatype. 
 

4. In general User Defined Functions are used for computations where as
SAP BI Training in Bangalore Stored Procedures are used for performing business logic. 
Best SAP BI Training institute in Bangalore,  
Marathahalli. Real time project based training
provided by working software professionals 5. UDF should return a value where as Stored Procedure need not. 
having more than 10 years of experience.  
Informatica Training in Bangalore
Informatica training in bangalore delivered by 6. User Defined Functions accept lesser number of input parameters than
a real time software expert having 10 years Stored Procedures. UDF can have upto 1023 input parameters where as a
of experience.  Stored Procedure can have upto 21000 input parameters. 
Siebel CRM Training in Bangalore
Best software training institute for Siebel CRM
training in Marathahalli, Bangalore. 
Software testing training institute in
bangalore

http://venkatsqlinterview.blogspot.in/2011/05/what­is­difference­between­user­defined.html 1/8
12/24/2016 SQL Server Interview Questions: What is the difference between a User Defined Function (UDF) and a Stored Procedure (SP) in SQL Server

Software testing training on real time projects
and placements. 
MSBI Training Institute in Bangalore
Best MSBI Training in Bangalore by an
expert. MSBI training on real time projects
and placements. 
Basic SQL Server Interview Questions

SQL Server Interview Questions on


Temporary Tables

SQL Server Interview Questions on


Indexes ‐ Part 1

SQL Server Interview Questions on


Indexes ‐ Part 2

What is the difference between


Having and Where clause 7. Temporary Tables can not be used in a UDF where as a StoredProcedure can
use Temporary Tables. 
What is the difference between a  
Temporary Table and a Table Variable
8. UDF can not Execute Dynamic SQL where as a Stored Procedure can
What is the use of COALESCE in SQL execute Dynamic SQL. 
Server  

SQL Server Interview Questions on 9. User Defined Function does not support error handling where as Stored


triggers Procedure support error handling. RAISEERROR or @@ERROR are not allowed
in UDFs. 
Difference between User Defined
Function and Stored Procedure If you are aware of any other differences, please post them as comments,
so that they will be useful for others.
SQL Server Interview Questions on +5   Recommend this on Google
Views ‐ Part 1

SQL Server Interview Questions on


Views ‐ Part 2 34 comments:

Basic SQL Server Interview Questions


on Joins Anonymous June 4, 2011 at 12:21 AM
Explain Inner Join with an example You can not write PRINT Statement in UDF
Reply
Explain Left Outer Join with an
example

Explain Right Outer Join with an Anonymous June 14, 2011 at 5:43 AM
example
You can not write EXEC('any sql statement') inside UDF
Explain Full Outer Join with an Reply
example

Explain Self Join with an example


Anonymous June 15, 2011 at 1:25 PM
What is the difference between Index The core difference is that function has no side effect which
Scan and Index Seek
means that it cannot change anything outside the function
Write a SQL Query to delete from a
body.
table that is involved in a SQL join Reply
What are the advantages of using
stored procedures
Anonymous August 25, 2011 at 7:31 AM
What are the different ways to
replace NULL values in SQL Server
http://venkatsqlinterview.blogspot.in/2011/05/what­is­difference­between­user­defined.html 2/8
12/24/2016 SQL Server Interview Questions: What is the difference between a User Defined Function (UDF) and a Stored Procedure (SP) in SQL Server

Funcation can take only input parameter Where as Stored


SQL Server interview questions on prcedure can take input and ouput parameters.
string manipulation functions
Reply
Write a Stored Procedure that takes
column name as a parameter and
returns the result sorted by the Anonymous September 18, 2011 at 10:46 PM
column that is passed
Stored procedures are compiled objects where as UDF are not
What is deferred name resolution in
SQL Server?
Reply

Anonymous September 24, 2011 at 11:50 PM


GETDATE can be used as the input parameter, but could not be
used inside the function itself. Other built in SQL functions that
cannot be used inside a User Defined Function include: RAND,
NEWID, @@CONNCECTIONS, @@TIMETICKS, and @@PACK_SENT.
Any built in function that is non‐deterministic cannot be used
inside the function itself.
Reply

Replies

Anonymous August 21, 2013 at 2:28 PM


Getdate() seems to be successfully returned from a
UDF in SQL 2012. You may want to check that again

Reply

Anonymous October 24, 2011 at 12:12 AM


What is deffered name resolution?
Reply

Replies

Venkat May 20, 2012 at 12:53 PM


Please follow this article, to understand deffered
name resolution in SQL Server.

Reply

Anonymous November 19, 2011 at 8:25 PM


nice difference it's good
Reply

Anonymous November 27, 2011 at 2:13 AM


Functions can be called from procedure whereas procedures
cannot be called from function.
Reply
http://venkatsqlinterview.blogspot.in/2011/05/what­is­difference­between­user­defined.html 3/8
12/24/2016 SQL Server Interview Questions: What is the difference between a User Defined Function (UDF) and a Stored Procedure (SP) in SQL Server

Reply

Anonymous December 12, 2011 at 1:58 AM


Yes, this is very good explanation and i read number of link
about this,but no link is good as same.so if some one having
really confusion about above question then this is proper
answer.
Reply

Anonymous December 30, 2011 at 12:31 PM


Why we can't use Stored procedure in UDF?
Reply

Anonymous February 11, 2012 at 2:42 AM


Stored procedures have capability to return multiple data sets
where as function can return only one data set either scalar or
table. May be this can be one of the reasons that the SP cannot
be called inside UDF...Pl comment.
Reply

Nitish Jain February 29, 2012 at 11:44 PM


The main difference is that we can use DML statements inside
the stored proc but not inside functions.
Reply

Anonymous April 13, 2012 at 7:16 AM


what is diffrd name resolution?pls explain
Reply

Replies

Venkat May 20, 2012 at 12:54 PM


What is deffered name resolution in sql server, with
an example

Reply

Anonymous September 7, 2012 at 1:45 PM


What parameters are passed in connection string. This question
is asked 2 times by two different architect.
Reply

Anonymous September 7, 2012 at 1:47 PM


http://venkatsqlinterview.blogspot.in/2011/05/what­is­difference­between­user­defined.html 4/8
12/24/2016 SQL Server Interview Questions: What is the difference between a User Defined Function (UDF) and a Stored Procedure (SP) in SQL Server

In GAC if two versions are present for same dlls which one to
chose by what application?
how ur code will identify which one is you should use. Give the
code or command which will identify?
Reply

Vikrant T November 20, 2012 at 2:29 AM


3 types of UDF:‐
a)Scalar Function
b)Inline Table Function
c)Multi‐statement Table Function
Reply

srinu vasu February 14, 2013 at 12:09 AM


we can change the server configurations through sp's but we
can not change through udfs.
Reply

Anonymous November 29, 2013 at 2:50 PM


Functions can't perform insert, update, delete and truncate
operations but stored procedures can
Reply

Anbharasan C.A. December 25, 2013 at 12:50 AM


Point #4 answers your question i hope.
Reply

Deepak Kashyap September 11, 2014 at 4:34 AM


In stored procedures you can call function but in function you
can't call stored procedures.
Reply

nageswararao konatham October 17, 2014 at 9:10 PM


hello sir
i faced a question in sql
emp table with columns empid int primary key identity,ename
nvarchar,email varchar
now write a single stored procedure to insert or update a row
if already row is there update and row is not there insert
Reply

Replies

Venkat October 18, 2014 at 3:53 AM

http://venkatsqlinterview.blogspot.in/2011/05/what­is­difference­between­user­defined.html 5/8
12/24/2016 SQL Server Interview Questions: What is the difference between a User Defined Function (UDF) and a Stored Procedure (SP) in SQL Server

Hi Nageswararao, please check the following video.


Hope the video answers your question.
If row exists perform an update otherwise insert

Reply

Venkata Maharshi November 1, 2015 at 8:15 AM


hello sir help me....i face a problem whn i am dng my project
have table like branchMaster , columns like branchid
varchar(30) and branchname varchar(30) .....now i need to
insert autoincrement on branchid varchar(30)....can u help me
please ASAP.

regards: maharshi,9032270738,maharshi357@gmail.com
Reply

Manoj Sharma December 24, 2015 at 8:34 PM


Dynamic SQL ?
Sir Please can u give an example with UDF and proc.
Reply

vijay negi January 15, 2016 at 12:15 PM


Udf can not modify a table values...means insert,update,delete
will not work under udf..but they work normally in stored
procedures
Reply

Manish April 14, 2016 at 2:48 AM


Hi Venkat, we appreciate your efforts and your tutorials are
excellent.
Just wanted to make a small correction in the above
differences that you have mentioned. The correction is
maximum number of input parameters allowed in SP is '2100'
and not '21000'. Please let me know if I am correct or not?
Reply

Mark April 30, 2016 at 7:58 AM


Functions and stored procedures serve separate purposes.
Although it's not the best analogy, functions can be viewed
literally as any other function you'd use in any programming
language, but stored procs are more like individual programs or
a batch script.

Functions normally have an output and optionally inputs. The


output can then be used as the input to another function (a SQL
Server built‐in such as DATEDIFF, LEN, etc) or as a predicate to
http://venkatsqlinterview.blogspot.in/2011/05/what­is­difference­between­user­defined.html 6/8
12/24/2016 SQL Server Interview Questions: What is the difference between a User Defined Function (UDF) and a Stored Procedure (SP) in SQL Server

a SQL Query ‐ e.g., SELECT a, b, dbo.MyFunction(c) FROM table


or SELECT a, b, c FROM table WHERE a = dbo.MyFunc(c).

Stored procs are used to bind SQL queries together in a


transaction, and interface with the outside world. Frameworks
such as ADO.NET, etc. can't call a function directly, but they
can call a stored proc directly.

Functions do have a hidden danger though: they can be misused


and cause rather nasty performance issues: consider this query:

SELECT * FROM dbo.MyTable WHERE col1 =


dbo.MyFunction(col2)
Where MyFunction is declared as:

CREATE FUNCTION MyFunction (@someValue INTEGER)


RETURNS INTEGER
AS
BEGIN
DECLARE @retval INTEGER

SELECT localValue
FROM dbo.localToNationalMapTable
WHERE nationalValue = @someValue

RETURN @retval
END
What happens here is that the function MyFunction is called for
every row in the table MyTable. If MyTable has 1000 rows, then
that's another 1000 ad‐hoc queries against the database.
Similarly, if the function is called when specified in the column
spec, then the function will be called for each row returned by
the SELECT.

So you do need to be careful writing functions. If you do SELECT


from a table in a function, you need to ask yourself whether it
can be better performed with a JOIN in the parent stored proc
or some other SQL construct (such as CASE ... WHEN ... ELSE ...
END)
Reply

Mark April 30, 2016 at 8:02 AM


It is mandatory for Function to return a value while it is not for
stored procedure.
Select statements only accepted in UDF while DML statements
not required.
Stored procedure accepts any statements as well as DML
statements.
UDF only allows inputs and not outputs.
Stored procedure allows for both inputs and outputs.
Catch blocks cannot be used in UDF but can be used in stored
procedure.
No transactions allowed in functions in UDF but in stored
procedure they are allowed.
Only table variables can be used in UDF and not temporary
tables.
Stored procedure allows for both table variables and temporary
tables.

http://venkatsqlinterview.blogspot.in/2011/05/what­is­difference­between­user­defined.html 7/8
12/24/2016 SQL Server Interview Questions: What is the difference between a User Defined Function (UDF) and a Stored Procedure (SP) in SQL Server

UDF does not allow stored procedures to be called from


functions while stored procedures allow calling of functions.
UDF is used in join clause while stored procedures cannot be
used in join clause.
Stored procedure will always allow for return to zero. UDF, on
the contrary, has values that must come ‐ back to a
predetermined point.
Reply

anshu kumar August 8, 2016 at 11:49 AM


how we can store image in sql ?
Reply

Replies

Anonymous August 14, 2016 at 2:16 PM


store the file path of image and for display just fetch
the file path from sql table.

Reply

Enter your comment...

Comment as:  Select profile...

Publish
  Preview

If you are aware of any other sql server questions asked in an


interview, please post them below. If you find anything missing or
wrong, please feel free to correct by submitting the form below.

Newer Post Home Older Post

Subscribe to: Post Comments (Atom)

 
Disclaimer ­ Terms of use ­ Contact Us

http://venkatsqlinterview.blogspot.in/2011/05/what­is­difference­between­user­defined.html 8/8

Вам также может понравиться