473,385 Members | 1,856 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Why use Functions in SQL if even if we have stored procedures

Recently, in a interview, I have been asked this question.
"WHY USE FUNCTIONS IF WE HAVE STORED PROCEDURES"

I answered we cannot perform DML operations using FUNCTIONS, We can use it to retrieve data only.

But Interviewer asked me, Stored procedures could also retrieve data then why use FUNCTIONS?

I ANSWERED: Don't know, I only use PROCEDURES...


I would like to know from all u guys, what u think about this questions.

Thanks
Aug 19 '13 #1

✓ answered by ck9663

You are correct, though. You cannot use DML in function.

We also use functions for the following:
1. If we need a result set that we will use for join or UNION to another result set, table or view.
2. You can use SP to call a function, not the other way around.
3. If we want to embed the returned value in a SELECT statement, you will need a function. You can't do that in an SP. This is also true in most part of the SELECT statement like WHERE, HAVING, GROUP BY, etc...

Anything I missed?


~~ CK

3 6894
ck9663
2,878 Expert 2GB
You are correct, though. You cannot use DML in function.

We also use functions for the following:
1. If we need a result set that we will use for join or UNION to another result set, table or view.
2. You can use SP to call a function, not the other way around.
3. If we want to embed the returned value in a SELECT statement, you will need a function. You can't do that in an SP. This is also true in most part of the SELECT statement like WHERE, HAVING, GROUP BY, etc...

Anything I missed?


~~ CK
Aug 19 '13 #2
Rabbit
12,516 Expert Mod 8TB
The answer to the question is most likely number 3. The crux of the question is not about the overall differences between stored procedures and functions. The point of the question is for you to answer when do you have no choice but to use a function. And the only situation that I can think of when you can use a function but can't use a stored procedure is number 3. They weren't asking about the limitations of functions.
Aug 19 '13 #3
Hi, thanks for answer, exactly what I was searching. I asked same question on another forum and what I got was differences between both (which I was already aware of).
Aug 21 '13 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Kent Lewandowski | last post by:
hi all, Recently I wrote some stored procedures using java jdbc code (admittedly my first stab) and then tried to implement the same within java packages (for code reuse). I encountered...
5
by: Rhino | last post by:
I am trying to determine the behaviour of stored procedures in DB2 V8.2.x in Windows/Unix/Linux and how I can control that behaviour. Some documentation in the manuals is confusing the issue...
5
by: Tim Marshall | last post by:
I was following the thread "Re: Access Treeview - Is it Safe Yet?" with interest and on reading the post describing Lauren Quantrell's SmartTree, I've run into something I don't understand: Stored...
45
by: John | last post by:
Hi When developing vb.bet winform apps bound to sql server datasource, is it preferable to use SELECTs or stored procedure to read and write data from/to SQL Server? Why? Thanks Regards
3
by: lemes_m | last post by:
Situation: Stored procedure 1 which is related with Stored procedure 2 which has parametar. Does anyone knows how to transfer value of some field from stored procedure 1 to parametar in stored...
28
by: mooreit | last post by:
The purpose for my questions is accessing these technologies from applications. I develop both applications and databases. Working with Microsoft C#.NET and Microsoft SQL Server 2000 Production and...
0
by: stoned99 | last post by:
Hi I'm having problems creating a function in DB2 8 for z/OS that contains basic procedural language such as "declares" The only function I have been able to successfuly compile is a code...
2
by: bshumsky06 | last post by:
Hi, I am trying to create stored procedures in MySQL. Does anyone have a suggestion on a good IDE to use. I have been working with the MySQL Query Browser but it generates errors very often and...
1
by: svkreddy | last post by:
Dear All, Please provide the simple information about stored procedures and stored functions in microsoft sql server2005. Please give the information about Nested procedures and nested funtions....
1
by: mansi sharma | last post by:
Functions ia a block of code that performs some task & returns value. Can somebody tell me What are Stored Procedures? I found abt Stored procedues from the Net-->Stored Procedures is a group of...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.