Stored Procedures

Stored Procedures

Stored procedures are a set of SQL statements with an assigned name. They are stored in a relational database management system as a group. This allows multiple programs to reuse and share them. Stored procedures are not tied to a specific database or object. However, they can access and modify the data in the database.

What are the benefits of stored procedures?

  • Stored procedures offer a vital security layer between the database and the user interface. This provides security through data access controls. As a result, users who do not write procedures cannot enter or change data.
  • Stored procedures guarantee the integrity of data. Information is entered in a consistent manner
  • They also improve productivity because statements in a stored procedure must only be written once
  • Stored procedures are modular. This means it  is easier to troubleshoot when there is a problem in an application. As a result, they have an advantage over embedding queries in a GUI (Graphical User Interface). Also, there is no need to modify the GUI source code to improve its performance since stored procedures are tunable
  • There is a significant reduction in network traffic between servers and clients because when it is used it stores the procedures. This is because commands are executed as a single batch of code. Instead of every single line of code being sent individually, only the call to execute the procedure is sent over the network.

Stored Procedures in SQL

In SQL Server, stored procedures accept input parameters and return multiple values of output parameters. They perform operations in the database and return a status value for calling a procedure or batch.

We can create user-defined procedures in a user-defined database or all system databases. This cannot, however, be done when we use a resource database (read-only database). The user-defined procedures are developed in Transact-SQL (T-SQL) or a reference to Microsoft. Also, we store temporary procedures in tempdb. There are two types of temporary procedures:

  • Global temporary procedures – Are visible to every user after they have been created
  • Local temporary procedures – These types of temporary procedures are only visible to the current user connection.

The SQL server comes with system procedures. These procedures are stored in an internal, hidden-resource database. The system procedures appear in a user-defined database as well as the SYS schema of each system.

Stored Procedures in Oracle

Stored procedures can also be found in Oracle’s database language PL/SQL. They are used to build applications within the Oracle database. Additionally, stored programs in the Oracle database are used by Information Technology experts to properly write and test codes. Once these codes have been compiled, they become stored procedures.

All stored procedures in Oracle conform to a basic PL/SQL block structure. This block structure consists of declarative, executable and exception-handling parts.

What is the difference between a stored procedure and a function?

Both a stored procedure and a function can be used to accomplish the same task. Also, they can both be custom-defined as part of any application. However, functions send their output to a T-SQL or a query statement. On the other hand, stored procedures return their outputs to the application. User-defined functions return table variables. They cannot change the server environment or operating system environment.

How to create a stored procedure

There are two methods of creating a stored procedure. You can use:

  • SQL Server Management Studio
  • Transact SQL

Let us see the steps of creating a procedure in Object Explorer for SQL Server Management Studio:

  • The first step is to connect to an instance of Database Engine and then expanding the instance
  • Then expand the databases, AdventureWorks database and Programmability in that order
  • You will see the stored procedures. Right-click on it and choose New Stored procedure
  • Go to the query menu and click on specify values for template parameters
  • You will see a dialog box after you click on specify values for template parameters. You should enter values for the parameters
  • When you are done, click OK
  • You can text the syntax on the query menu by clicking on parse. If you get an error message then you should cross-check and edit your statement
  • Click execute to create the procedure from  the query menu
  • Right-click on stored procedures and select refresh if you want to see the procedure listed in object explorer

Now, let us create a procedure using Transact-SQL

  • Connect to an instance of database engine in object explorer
  • Click on new query from the file menu
  • You can write or copy-paste  your program into the query window and click execute

If you need a code example that creates a stored procedure, avail of our SQL assignment help service.