Objective Theory: Stored Procedures

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

NAME: KAMRAN SULEMAN

ROLL# 2017-CE-008
Lab # 10 : Stored Procedures RDBMS

LAB # 10
STORED PROCEDURES

OBJECTIVE Creating and Running Stored Procedures


THEORY
A Stored Procedure is precompiled collection of T-SQL statements. Stored
procedures are named and processed as a single unit.
System Stored Procedures
SQL Server provides some readily available stored procedures form managing SQL Server and for
users and database information retrieval. These can only be executed.
The following table list some system stored procedures.
SYSTEM STORED PROCEDURES DESCRIPTION

sp_databases List all the available databases on the server

sp_server_info Lists long list of server information like

DBMS_NAME, DBMS_VER,

SYS_SPROC_VERSION,

ACCESSIBLE_TABLES etc

sp_stored_procedures Lists all the stored procedures available in the

current environment.

sp_tables Lists all the objects that can be queried in the

current environment.

sp_configure Changes the global configuration options of

the server. When using it without options, then

the current server settings are displayed.

sp_help Shows information about database objects

specified.

Here we shall focus on user defined stored procedures.

1. Creating User defined Stored Procedures:


NAME: KAMRAN SULEMAN
ROLL# 2017-CE-008
Lab # 10 : Stored Procedures RDBMS

Users can create their customized stored procedures using the CREATE
PROCEDURE statement which are stored in the current database.

Syntax CREATE PROC procedure_name AS T-SQL statement(s)

Create a Stored Procedure

1. Consider the following query (stored procedure):

CREATE PROCEDURE EMP_20 AS SELECT * FROM EMPLOYEE


WHERE DNO=20
A stored procedure will be created that will show the details of all the employees
from the employee table whose department number is 20.

2. Executing User defined Stored Procedures:


User-defined stored procedures can be executed using the EXECUTE statement.
Syntax EXECUTE procedure_name
Execute a Stored Procedure

Consider the following query which executes the EMP_20 stored


procedure (output is shown in figure):

EXECUTE EMP_20

3. Using Parameters in Stored Procedures:


Parameters can be passed to a stored procedure from the EXECUTE
statement. Syntax

CREATE PROCEDURE procedure_name


@parameter_name <data type>

AS

T-SQL Statement(s)

4. Passing Parameters to a Stored Procedure Now we create a stored


procedure that will show the details of the employee department whilst department
number will be passed as a parameter opposed to EMP_20 stored procedure which
can show the details of department number 20 only.

1. Consider the following stored procedure:


create procedure edetail @deptno int As select * from employee where eno=@deptno

Now you can execute this stored procedure while passing the argument as of the
data type of the parameter as shown below:
execute edetail 20
NAME: KAMRAN SULEMAN
ROLL# 2017-CE-008
Lab # 10 : Stored Procedures RDBMS

LAB TASK
CODE:

You might also like