English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

give me example using where we will stored procedure and where we will use userdefined function

2007-02-18 14:45:26 · 5 answers · asked by venkat 1 in Computers & Internet Programming & Design

5 answers

A user-defined function is preferable when you have a function that needs to be done many times on a piece of data. A stored procedure is for a complete transaction. For example, if you need to do a string manipulation on a record, you would put the string manipulation into a UDF and then the SELECT in a stored procedure:

SELECT columnA, dbo.udfStringManip(columnB) as columnB_corrected FROM dbo.tbl

2007-02-18 15:00:15 · answer #1 · answered by Rex M 6 · 0 0

In addition to what the other posters have stated... one of the key differences you need to keep in kind is the alteration of data...

If you plan to alter a database table in any way (Insert, Update, Delete) you will have to use a stored procedure. User defined function cannot be used to alter table data.

You will also want to use a User defined function if you need functionality that is to be executed as part of a SQL statement. For instance, lets say you have a numeric field that actually stores a date in the format YYYMMDD you could create a function that will convert it to a displayable date of MM/DD/YYYY and place that function in your select statement. Since stored procedures cannot be called from within a Select statement, you would be required to use a User defined function.

2007-02-18 15:07:23 · answer #2 · answered by Scottee25 4 · 0 1

A stored procedure (SQL Server) is useful if you have several programs or interfaces accessing the same information. A stored procedure can be changed without changing the program in which it is called. This is a great way to create modular coding.

a user defined function can mean many things........depending on the context in which you are referring....may or may not mean the same thing. Generally a user defined function is one which is created by the programmer and is not necessarily as modular as a stored procedure.

2007-02-18 15:04:14 · answer #3 · answered by Jeffrey F 6 · 0 2

in most of the situation we use the stored procedure
but if want to reduce the complexity of the code we write the userdefine function.

if the predefined function is not suitable for our needs
we should write the functions. otherwise we will use the predefined function

2007-02-18 14:59:55 · answer #4 · answered by ganboo 1 · 0 2

Both are almost same but with this difference.

The user defined function returns only one value.
Whereases a stored procedure can return more then one values.

So it depends upon the situation whether you need one value or multiple.

2007-02-18 21:29:23 · answer #5 · answered by Atif Majid 3 · 0 2

fedest.com, questions and answers