+91-90427 10472
         
Dot net training in Chennai -Maria Academy

Stored Procedure – SQL

11 Sep 2022

Stored Procedure:

A stored procedure is a collection of Structured Query Language (SQL) statements with a unique name that are stored as a group in a relational database management system (RDBMS) to be reused and shared by multiple programmes.

Stored procedures can access or modify data in a database, but they are not tied to a specific database or object, which provides several benefits.

So, if you have a SQL query that you write repeatedly, save it as a stored procedure and simply call it to execute it.

Additionally, you can send parameters to a stored procedure, allowing it to take action based on the value(s) of the passed parameters.

 

Advantages of Using Stored Procedures:

An essential layer of protection is added by a stored procedure between the user interface and the database. Because end users can add or modify data but not develop procedures, it enables security through data access controls. Since data is entered consistently while using a stored procedure, data integrity is maintained. Because statements in a stored method just need to be written once, productivity is increased.

Stored procedures are preferable to embedding queries in a graphical user interface (GUI). When a problem arises in an application, it is easier to troubleshoot because stored procedures are modular. Stored procedures are also tuneable, so there is no need to change the GUI source code to improve its performance. It is simpler to code stored procedures than it is to build a query using a GUI.

When using stored procedures, network traffic between clients and servers can be reduced Because commands are performed as a single batch of code. This implies that only the call to run the procedure—rather than each individual line of code—is delivered across a network.

 

Stored Procedure in SQL Server:

SQL Server stored procedures can accept input parameters and return multiple values of output parameters; stored procedures in SQL Server programme statements to perform database operations and return a status value to a calling procedure or batch.

Except when a read-only (resource database) is utilised, user-defined procedures can be created in user-defined databases or in all system databases. They are created using Microsoft’s T-SQL or Transact-SQL. There are two sorts of temporary procedures: local and global, both of which are kept in Temporary database. While global procedures are visible to any user once they are generated, local procedures are only available to the user connection that is active at the time. Physically, system procedures are kept in a hidden resource database that comes with SQL Server. They can be found in a user-defined database as well as each system’s SYS schema.

 

Stored Procedure Syntax in SQL:

Syntax To Create Stored Procedure:

CREATE PROCEDURE Procedurename
AS
SQL STATEMENT
GO;

Syntax To Execute Stored Procedure:

EXEC Procedurename;

 

Various types of stored procedures Server SQL:

 

System Defined Stored Procedure: SQL Server already has these stored procedures defined. These are physically stored in a hidden SQL Server Resource Database and logically appear in each user-defined and system-defined database’s sys schema. The sp_ prefix is used to begin this procedure. As a result, we avoid using this prefix when naming user-defined procedures.

Extended Procedure: Extended procedures interface with external programmes to perform various maintenance tasks. These extended procedures are stored in the Master database and begin with the xp_ prefix. Essentially, these are used to automatically call programmes on the server from a stored procedure or a trigger run by the server.

User-Defined Stored Procedure: The user creates these procedures for his or her own actions. Except for the Resource database, these can be created in any system database or in a user-defined database.

CLR Stored Procedure: CLR stored procedures are a type of procedure that is based on the.net framework’s CLR (Common Language Runtime). CLR procedure integration was introduced with SQL Server 2008, allowing procedures to be coded in.NET languages such as C#, Visual Basic, and F#. I’ll go over the CLR stored procedure later.

Social tagging: > > > > >