how to create store procedure

3
Storeprocedure: A stored procedure is nothing more than prepared SQL code that you save so you can reuse the code over and over again. So if you think about a query that you write over and over again, instead of having to write that query each time you would save it as a stored procedure and then just call the stored procedure to execute the SQL code that you saved as part of the stored procedure. SP is pre-define and pre-compiled set of souce code. To create a stored procedure to do this the code would look like this: CREATE PROCEDURE GetEmployeeDetails @EmployeeID int = 0 AS BEGIN SET NOCOUNT ON;

Upload: durgaprasad-yadav

Post on 11-Apr-2017

89 views

Category:

Education


0 download

TRANSCRIPT

Page 1: How to create Store Procedure

Storeprocedure:

A stored procedure is nothing more than prepared SQL code that you save so you can reuse the code over and over again.  So if you think about a query that you write over and over again, instead of having to write that query each time you would save it as a stored procedure and then just call the stored procedure to execute the SQL code that you saved as part of the stored procedure.

SP is pre-define and pre-compiled set of souce code.

To create a stored procedure to do this the code would look like this:

CREATE PROCEDURE GetEmployeeDetails      @EmployeeID int = 0ASBEGIN      SET NOCOUNT ON;      SELECT FirstName, LastName, BirthDate, City, Country      FROM Employees WHERE EmployeeID=@EmployeeIDEND

GO

Page 2: How to create Store Procedure

Alter or Modify a Stored Procedure

ALTER PROCEDURE GetEmployeeDetails      @EmployeeID int = 0ASBEGIN      SET NOCOUNT ON;      SELECT FirstName, LastName, BirthDate, City, Country      FROM Employees WHERE EmployeeID=@EmployeeIDEND

GO

Below figure displays the syntax for alter a stored procedure. As you can see below to modify a stored

procedure ALTER keyword is used rest all remains the same.

Drop or Delete a Stored Procedure

Figure below displays how to drop a stored procedure. As you can see below to delete a stored

procedure DROP keyword is used proceeded by the name of the stored procedure.

Page 3: How to create Store Procedure

Example

DROP PROCEDURE GetEmployeeDetails