- Output parameters, which can return either data (such as an integer or character value) or a cursor variable (cursors are result sets that can be retrieved one row at a time).
- Return codes, which are always an integer value.
- A result set for each SELECT statement contained in the stored procedure or any other stored procedures called by the stored procedure.
- A global cursor that can be referenced outside of that stored procedure.
There are lot of benefits of SQL stored procedures in term of modular programming, faster execution, reduce network traffic and security mechanism such as mentioned below:
Modular Programming
|
They
are created once in the database and call it any number of times by the
programs. They can be easily modified independently of the program source
code and there will no need in the business application.
|
Faster Execution
|
They
are parsed and optimized when they are first executed, and a compiled version
of the stored procedure remains in memory cache for later use. This means the
stored procedure does not need to be reparsed and re-optimized with each use
resulting in much faster execution times.
|
Reduce Network Traffic
|
Stored
procedures can have many individual SQL queries but can be executed with a
single statement. This allows us to reduce the number and size of calls from
the client to server.
|
Security Mechanism
|
Users
can be granted permission to execute a stored procedure even if they do not
have permission to execute the procedure's statements directly.
|
protection from SQL injection attacks
|
Stored
procedures can be used to protect against injection attacks. Stored procedure
parameters will be treated as data even if an attacker inserts SQL commands.
Also, some DBMSs will check the parameter's type. A stored procedure that in
turn generates dynamic SQL using the input is however still vulnerable to SQL
injections unless proper precautions are taken.
|
Best Practices for Stored Procedures
Use meaningful names: Choose names that clearly describe the purpose of the stored procedure.
Document your code: Add comments to explain the logic of the stored procedure.
Handle errors gracefully: Use
TRY...CATCHblocks to handle potential errors.Validate input parameters: Ensure that input parameters are valid before using them in SQL statements.
Use transactions: Use transactions to ensure data consistency.
Keep stored procedures short and focused: Break down complex tasks into smaller, more manageable stored procedures.
Regularly review and optimize stored procedures: Identify and address performance bottlenecks.
Implement proper security measures: Protect stored procedures from unauthorized access.
Considerations When Using Stored Procedures
While stored procedures offer numerous benefits, there are also some considerations to keep in mind:
Debugging: Debugging stored procedures can be more challenging than debugging ad-hoc SQL queries. Database management systems often provide debugging tools, but they may not be as user-friendly as those available for other programming languages.
Version Control: Managing versions of stored procedures can be complex, especially in large database environments. It is important to have a robust version control system in place to track changes to stored procedures.
Overhead: While stored procedures generally improve performance, there is some overhead associated with creating and managing them. This overhead is usually minimal, but it is important to consider it when designing database applications.
Security Risks: If not properly secured, stored procedures can be exploited by attackers to gain unauthorized access to the database. It is important to carefully review the security implications of stored procedures and implement appropriate security measures.
- Debugging large stored procedures can be very difficult in case of any failure.
- Stored procedure languages are quite often vendor-specific. Switching to another vendor's database most likely requires rewriting any existing stored procedures.
- Stored procedure languages from different vendors have different levels of sophistication.
- Tool support for writing and debugging stored procedures is often not as good as for other programming languages, but this differs between vendors and languages.
- Changes to stored procedures are more difficult to keep track of within a version control system than other code.
- Changes must be reproduced as scripts to be stored in the project history to be included, and differences in procedures can be more difficult to merge and track correctly.
- Stored procedures can grow into an unmaintainable mess if not written carefully. While this is true of any language, it's especially true of SQL server stored procedures.
- Create Stored Procedure
- Execute Stored Procedure
- Modify or Alter Stored Procedure
- Delete Stored Procedure
- If you have a good balance of which parts of your business logic goes inside your SPs, you will be in a good place.
- If you want to make your system fast and reliable, try to use as much db features as possible. Coding in a database independent way is totally waste until the day you want to change database.
- If you want to utilise extensively your db features in your application, as a result there will be lot of unnecessary DB calls which itself will result in other issues e.g db sessions, connection leakages and so on.
No comments:
Post a Comment