Quantcast
Channel: Planet MySQL
Viewing all articles
Browse latest Browse all 18800

SQL Stored Procedure Example | Stored Procedure In SQL Tutorial

$
0
0
SQL Stored Procedure Example | Stored Procedure In SQL Tutorial

SQL Stored Procedure Example | Stored Procedure In SQL Tutorial is today’s topic. A stored procedure is the prepared SQL code that is used to save time and can be reused over and over again. This code is stored in the database and helps to get the required output by passing parameters. These stored procedures are used in PL/SQL or SQL SERVER. PL/SQL is a block-structured query language that helps developers to combine the power of SQL with procedural statements.

SQL Stored Procedure

A stored procedure is the prepared SQL code that you can save so that the code can be reused over and over again.

So if you have the SQL query that you write over and over again, save it as the stored procedure, and then call it to execute it.

You can also pass parameters to a stored procedure so that the stored procedure can act based on the parameter value(s) that is passed.

The procedure can be a function or a method that can be invoked through triggers, applications in java or PHP, etc.

Syntax

Create procedure <procedure_name>
AS
Begin
<SQL STATEMENT>
End
Go

Let’s understand the above syntax with a proper example:

EXAMPLE:

Consider table CUSTOMERS:

ID NAME AGE ADDRESS SALARY 
1 Tom 21 Kolkata 500
2 Karan 22 Allahabad 600
3 Hardik 23 Dhanbad 700
4 Komal 24 Mumbai 800

 

QUERY

CREATE PROCEDURE Sample

AS
SELECT * FROM Testdb.Customers

Go;

OUTPUT:

ID NAME AGE ADDRESS SALARY 
1 Tom 21 Kolkata 500
2 Karan 22 Allahabad 600
3 Hardik 23 Dhanbad 700
4 Komal 24 Mumbai 800

 

So, here we have created a procedure named Sample which displays the details of the customer’s table which was stored in Testdb database.

#MODIFYING THE EXISTING PROCEDURE

We can modify the procedure using the ALTER statement.

#SYNTAX

ALTER procedure <procedure_name>

AS
Begin

<SQL STATEMENT>

End

Go

QUERY:

ALTER PROCEDURE Sample

AS
SELECT * FROM Testdb.New_Customers

Go;

So, here, the customers’ table data will not be displayed New_customers; instead, data will be displayed, which was previously explicitly created in Testdb database.

#DROPPING A PROCEDURE:

The created procedure can be dropped using the DROP keyword.

SYNTAX:

DROP PROCEDURE procedure_name;

QUERY:

DROP PROCEDURE sample;

So, here, the procedure named sample is dropped, which was previously created above.

Let’s look at the disadvantages of Procedures.

#Stored Procedure With One Parameter

The following SQL statement creates the stored procedure that selects Customers from a particular City from the “Customers” table:

CREATE PROCEDURE SelectAllCustomers @City nvarchar(30)
AS
SELECT * FROM Customers WHERE City = @City
GO;

Execute the stored procedure above as follows:

EXEC SelectAllCustomers City = "Marbella";

#Stored Procedure With Multiple Parameters

Setting up multiple parameters is very easy. Just list the each parameter and the datatype separated by the comma as shown below.

The following SQL statement creates a stored procedure that selects Customers from a particular City with a specific PostalCode from the “Customers” table:

CREATE PROCEDURE SelectAllCustomers @City nvarchar(30), @PostalCode nvarchar(10)
AS
SELECT * FROM Customers WHERE City = @City AND PostalCode = @PostalCode
GO;

Execute the stored procedure above as follows.

EXEC SelectAllCustomers City = "Perth", PostalCode = "6021";

#Advantages of Stored Procedure

  1. They result in the improvement of the performance of the article. Whenever a procedure is being called frequently in an application in a single connection, the compiled version of that procedure is delivered.
  2. They reduce traffic between the database and application as the lengthy statements are already fed into the database and do not require to be sent again and again by the application.
  3. They are like code reusability as that of functions and methods in C/C++ and JAVA.

#Disadvantages

  1. Debugging of the stored procedure is not supported by MySQL.
  2. Stored procedures result in a lot of memory usage.

#Conclusion

It groups one or more TransactSQL statements into the logical unit and is stored as an object in the Database Server. When the stored procedure is called at the first time, MySQL creates the execution plan and stores it in the plan cache.

In the subsequent executions of that stored procedure, MySQL reuses a plan so that the stored procedure can execute very fast with the reliable performance.

Finally, the Stored Procedure In SQL Tutorial is over.

The post SQL Stored Procedure Example | Stored Procedure In SQL Tutorial appeared first on AppDividend.


Viewing all articles
Browse latest Browse all 18800

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>