We are independent & ad-supported. We may earn a commission for purchases made through our links.
Advertiser Disclosure
Our website is an independent, advertising-supported platform. We provide our content free of charge to our readers, and to keep it that way, we rely on revenue generated through advertisements and affiliate partnerships. This means that when you click on certain links on our site and make a purchase, we may earn a commission. Learn more.
How We Make Money
We sustain our operations through affiliate commissions and advertising. If you click on an affiliate link and make a purchase, we may receive a commission from the merchant at no additional cost to you. We also display advertisements on our website, which help generate revenue to support our work and keep our content free for readers. Our editorial team operates independently of our advertising and affiliate partnerships to ensure that our content remains unbiased and focused on providing you with the best information and recommendations based on thorough research and honest evaluations. To remain transparent, we’ve provided a list of our current affiliate partners here.
Internet

Our Promise to you

Founded in 2002, our company has been a trusted resource for readers seeking informative and engaging content. Our dedication to quality remains unwavering—and will never change. We follow a strict editorial policy, ensuring that our content is authored by highly qualified professionals and edited by subject matter experts. This guarantees that everything we publish is objective, accurate, and trustworthy.

Over the years, we've refined our approach to cover a wide range of topics, providing readers with reliable and practical advice to enhance their knowledge and skills. That's why millions of readers turn to us each year. Join us in celebrating the joy of learning, guided by standards you can trust.

What is a Stored Procedure?

By Allan Robinson
Updated: May 17, 2024
Views: 2,485
Share

A stored procedure is a subroutine in a relational database system. It's stored in the data dictionary and is available to applications that can access the database. Common functions performed by stored procedures include access control and data validation. A stored procedure centralizes code that exists in multiple applications, making it easier to maintain. Stored procedure can call other stored procedures, which allows them to be nested.

A user-defined function (UDF) is similar to a stored procedure, except that a UDF can be used like an expression in a Structured Query Language (SQL) statement. A stored procedure is invoked with the CALL statement. A stored procedure can return the result of a SELECT statement, also known as a result set. An application can then process the result set with a cursor.

Stored procedures can declare cursors to move through a result set, and they also can declare variables to process data. SQL provides these capabilities through commands such as WHILE, LOOP and REPEAT. A stored procedure can also modify variables, receive variables and return variables.

Most database systems support stored procedures, although the specific implementation depends on the database system. Some database systems allow stored procedures to be implemented in programming languages such as C++ or Java. Stored procedures written in these languages might not be able to execute SQL statements.

Some database systems can use stored procedures to control transaction management. They also can run stored procedures inside a transaction, so that the transaction is transparent to the stored procedure. A condition handler can invoke a stored procedure, which allows database administrators to track errors. A database trigger such as an insert or update can also invoke a stored procedure.

Stored procedures can eliminate much of the compilation overhead of inline, or dynamic, SQL statements. Dynamic SQL generally needs to be compiled, although most databases use statement caching to avoid the repeated compilation of dynamic SQL. Stored procedures are already stored directly in the database and therefore don't need to be compiled. This also allows stored procedures to reduce network traffic, because they run inside the database.

This advantage is somewhat offset by the fact that stored procedures make it more difficult to optimize an execution plan. Some arguments in the stored procedure call aren't available at compilation time, which can impair the performance of stored procedures. The impact that stored procedures have on performance depends on the configuration and implementation of the database.

Share
WiseGeek is dedicated to providing accurate and trustworthy information. We carefully select reputable sources and employ a rigorous fact-checking process to maintain the highest standards. To learn more about our commitment to accuracy, read our editorial process.

Editors' Picks

Discussion Comments
Share
https://www.wisegeek.net/what-is-a-stored-procedure.htm
Copy this link
WiseGeek, in your inbox

Our latest articles, guides, and more, delivered daily.

WiseGeek, in your inbox

Our latest articles, guides, and more, delivered daily.