Differences between functions and stored procedures?

Answers were Sorted based on User's Feedback



Differences between functions and stored procedures?..

Answer / avinash

1. Functions can used with Select statement
SP are not
2. If we have a syntax error or bug in SP it ignore it at
execution. In case of Function it won't.
3. Function should return atlease one value.
SP may or maynot return values.
4. Function cannot include in other function.
We can execute one SP in other.

Is This Answer Correct ?    9 Yes 4 No

Differences between functions and stored procedures?..

Answer / paras

UDF
1. Must return a value – a single result set
2. Directly used in select, order by,where, from
3. UDF can’t use nondeterministic function Such as
getdate(), rand()
4. Can’t change server enviroment variables
5. Stops execution of T-SQL code when error occurs
6. Can’t use temp table in UDF

Stored Procedure
1. Can return value – multiple result set
2. Can’t use in select
3. Can use nondeterministic functions
4. Can change server enviroment variables
5. Stored procedure move to the next instruction if you used
proper error handling,
6. Can use temp table in SP

Is This Answer Correct ?    6 Yes 2 No

Differences between functions and stored procedures?..

Answer / santhoshkumar.k

store procedure takes less time to execute the statement
than functions, also we can create loops inside the store
procedure.

Is This Answer Correct ?    4 Yes 0 No

Differences between functions and stored procedures?..

Answer / gautam

function does have update or delete statment.

Is This Answer Correct ?    3 Yes 0 No

Differences between functions and stored procedures?..

Answer / rathi

a stored procedure is like a subroutine. It is used to
perform tasks within the database, whether it be to INSERT,
UPDATE, DELETE, SELECT, send return values, send output
parameters, send e-mail, call command line arguments,
encapsulate business logic, enforce data integrity, or any
combination thereof. Here is a fictitious example:

CREATE PROCEDURE dbo.doStuff
@dt SMALLDATETIME
AS
BEGIN
SET NOCOUNT ON

DECLARE @un SYSNAME,
@now SMALLDATETIME

SET @un = SUSER_SNAME()
SET @now = GETDATE()

BEGIN TRANSACTION

INSERT dbo.myLog
(
ProcName,
UserName,
dt
)
SELECT
'doStuff',
@un,
@now

DELETE dbo.myLog
WHERE dt < (@now-7)

UPDATE dbo.Users
SET LastActivity = @now
WHERE UserName = @un

COMMIT TRANSACTION

SELECT TOP 3 ProcName, dt
FROM MyLog
WHERE UserName = @un
ORDER BY dt DESC

DECLARE @subject VARCHAR(255)
SET @subject = @un + ' used the doStuff procedure.'

EXEC master..xp_smtp_sendmail
@from = 'foo@bar.com',
@to = 'bar@foo.com',
@server = 'mail.myserver.com',
@subject = @subject

DECLARE @cmd VARCHAR(255)
SET @cmd = 'del c:\users\'+@un+'\archive\*.log'
EXEC master..xp_cmdshell @cmd, NO_OUTPUT

RETURN 0
END
GO

Stored procedures accept parameters, and are the preferred
method of both manipulating data and simply returning data.
They are compiled when first run, and the query plans are
stored and cached by SQL Server's optimizer, and those
cached plans are swapped out depending on frequency of
usage. Generally, a stored procedure will perform faster
than an ad hoc query, but there are certain cases (e.g.
when a bad plan is cached) that this is not the case.

User-Defined Functions (UDFs)

In general, UDFs can be a serious source of performance
issues. Also, UDFs cannot be used for DML operations
(INSERT/UPDATE/DELETE), cannot use non-deterministic
functions ,cannot use dynamic SQL, and cannot have error-
handling (e.g. RAISERROR).

Is This Answer Correct ?    2 Yes 0 No

Differences between functions and stored procedures?..

Answer / samba shiva reddy . m

1. Functions are compiled and executed at run time.
Stored procedures are stored in parsed and compiled format in the database(Pree compiled).

2. Functions cannot affect the state of the database which means we cannot perform insert,delete,update and create operations on the database.
Stored Procedures can affect the state of the database by using insert,delete,update and create operations.

3 Functions are basically used to compute values. We passes some parameters to functions as input and then it performs some operations on the parameter and return output.
Stored procedures are basically used to process the task.

4.Function can not change server environment and our operating system environment.
Stored procedures can change server environment and our operating system environment.

5.Functions can invoked from SQL Statements.
example : select udf from table name

Stored procedures can't invoked from SQL staements.
example : select spname from table name

6.Functions can run an executable file from SQL SELECT or an action query.
operating system use Execute or Exec to run

7.We can use User Defined function in Stored procedure
we can't use stored procedure in UDF.

Is This Answer Correct ?    2 Yes 0 No

Differences between functions and stored procedures?..

Answer / purushotam bhardwaj

FUnction use when you want to return any value only
but SP is best for all

Is This Answer Correct ?    1 Yes 0 No

Differences between functions and stored procedures?..

Answer / manoj pandey

Check following blog post for difference between UDF & Stored Procedures: http://sqlwithmanoj.wordpress.com/2011/09/21/stored-procedures-vs-functions-difference-between-sp-udf/

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More SQL Server Interview Questions

What is usually the first word in a sql query?

0 Answers  


What are the types of subscriptions in SQL Server replication?

0 Answers   HCL,


Tell me what is de-normalization and what are some of the examples of it?

0 Answers  


What is the difference between drop table and truncate table?

0 Answers  


Write a code to select distinct records without using the DISTINCT keyword.

0 Answers   Aspiring Minds,






What is user-defined multi-statement table-valued function?

0 Answers  


How much memory that we are using in Logshipping Concept?

0 Answers  


what is difference between nchar and char in Sql server ?

3 Answers  


What is partition index in sql server?

0 Answers  


Why are you getting errors when creating a new odbc dsn?

0 Answers  


What is mean by clustered index and non clustered index, give syntax of creation? : sql server database administration

0 Answers  


In the primary key have a multiple field or not?

8 Answers   TCS,


Categories