0% found this document useful (0 votes)
50 views8 pages

Procedures

Functions return a value in SQL, while procedures do not return a value. Stored procedures allow users to save blocks of SQL code for reuse, and can accept parameters to make the code more flexible. Stored procedures are defined using CREATE PROCEDURE and executed using EXEC, and can accept single or multiple parameters of various data types.

Uploaded by

manisha mudgal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views8 pages

Procedures

Functions return a value in SQL, while procedures do not return a value. Stored procedures allow users to save blocks of SQL code for reuse, and can accept parameters to make the code more flexible. Stored procedures are defined using CREATE PROCEDURE and executed using EXEC, and can accept single or multiple parameters of various data types.

Uploaded by

manisha mudgal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Procedures

Function
Function, in computer programming language context,
a set of instructions which takes some input and
performs certain tasks. In SQL, a function returns a
value.
Procedure
Procedure, as well, is a set of instructions which takes
input and performs certain task. In SQL, procedure
does not return a value. In java, procedure and
functions are same and also called sub-routines.
Following are the important differences between SQL
Function and SQL Procedure.
A stored procedure is a prepared SQL code
that you can save, so the code can be
reused over and over again.
So if you have an SQL query that you write
over and over again, save it as a stored
procedure, and then just call it to execute
it.
You can also pass parameters to a stored
procedure, so that the stored procedure
can act based on the parameter value(s)
that is passes.
Stored Procedure Syntax

CREATE PROCEDURE procedure_name
AS
sql_statement
GO;

Execute a Stored
Procedure
EXEC procedure_name;
Stored Procedure With One Parameter
The following SQL statement creates a stored procedure
that selects Customers from a particular City from the
"Customers" table:

CREATE PROCEDURE SelectAllCustomers @City
nvarchar(30)
AS
SELECT * FROM Customers WHERE City = @City
GO;

EXEC SelectAllCustomers @City = 'London';


Stored Procedure With Multiple Parameters
Setting up multiple parameters is very easy. Just list each parameter and the
data type separated by a comma as shown below.
The following SQL statement creates a stored procedure that selects Customers
from a particular City with a particular PostalCode from the "Customers" table:

Example
CREATE PROCEDURE SelectAllCustomers @City
nvarchar(30), @PostalCode nvarchar(10)
AS
SELECT * FROM Customers WHERE City =
@City AND PostalCode = @PostalCode
GO;
Execute the stored procedure above as follows:
Example
EXEC SelectAllCustomers @City = 'London',
@PostalCode = 'WA1 1DP';

You might also like