SQL Stored Procedure In SQL Server Management Studio
The main purpose of stored procedures to hide direct SQL queries from the code and improve performance of database operations such as SELECT, UPDATE, and DELETE data.SQL stored procedure is a collection of SQL commands and SQL logic. It also can be use as caches and reused.
Type of stored procedure
There are two types of stored procedure:
- User defined stored procedure
- System defined stored procedure
User defined stored procedure
It developed by database developer or database administrators. These Stored Procedure contains one or more SQL statements to SELECT, UPDATE or DELETE records from database table. It can take input parameters and produce output parameters.It is a mixture of Data Definition Language and Data Manipulation Language commands.User Defined Stored Procedure can classified into two types:
- T-SQL Stored Procedure
Transact SQL (T-SQL) Stored Procedures receive and return parameters. It process the INPUT, UPDATE and DELETE queries with and without parameters and return data of rows as output.It most common way to write Stored Procedure in SQL Server.
- (Common Language Runtime ) CLR Stored Procedure
These are written in a CLR based programming language such as C# or VB.NET and executed by .NET framework
System Stored Procedure
It created and executed by the SQL server for server administrative activities
Benefits of SQL stored procedures
- Reduced server / client network traffic :
- Stronger security :
- Reuse of code :
- Easier maintenance :
When client applications call procedures and keep database operations in the data list, only procedures must be updated for any changes into the specific database. The application layers remains separate and does not have to know about database layouts , relationships or processes.
- Improved performance :
A procedure compiles the first time it is executed and creates an execution plan which is reused for subsequent executions by default. Since the query processor doesn't have to create a new plane, it typically takes less time to process the procedure.
The commands in a procedure are execute as a single batch of code.It only call to execute the procedure is sent across the network. Due to this, it can reduced network traffic between the server and client. Every individual line of the code would have to cross the network.
Even if users and programs don't have permission on those underlying objects, multiple users and client programs can perform operations on underlying database object through a procedure. That procedure controls what processes and activities are performed and protects the underlying database objects.It eliminates the requirement to grant permission at the individual object level and simplifies the security layers.
When calling a procedure over the network, it only call to execute the procedure is visible. Therefore, malicious user cannot see the table and database object names. Procedure parameters helps guard against SQL injection attacks.Since parameter input is treated as a literal value and not as executable code, it more difficult for an attacker to insert a command into the Transact-SQL statements
Comments