Home > Database > Mysql Tutorial > body text

Create delimited stored procedure in MySQL

王林
Release: 2023-09-09 17:57:08
forward
726 people have browsed it

Create delimited stored procedure in MySQL

You can use the create procedure command to create a stored procedure. The syntax is as follows −

delimiter //
CREATE PROCEDURE yourStoreProcedureName()
BEGIN
   Declare variable here
   The query statement
END //

delimiter //
Copy after login

Apply the above syntax to create a stored procedure. The query is as follows −

mysql> use test;
Database changed
mysql> delimiter //
mysql> create procedure Sp_callTableStoredProcTable()
   −> begin
   −> select *from StoredProcTable;
   −> end //
Query OK, 0 rows affected (0.54 sec)
Copy after login

Now you need to change the delimiter with ; to call stored procedure −

mysql> delimiter ;
Copy after login

You can call stored procedure using CALL command. The syntax is as follows −

CALL yourStoredProcedureName();
Copy after login

The above stored procedure can be called using CALL command as shown in the below query −

mysql> call Sp_callTableStoredProcTable();
Copy after login

The following is the output−

+-----------+------+
| FirstName | Age  |
+-----------+------+
| John      | 23   |
| Bob       | 24   |
| David     | 20   |
+-----------+------+
3 rows in set (0.03 sec)

Query OK, 0 rows affected (0.06 sec)
Copy after login

In the above, we used the table 'StoredProcTable', this The table has three records. All records are displayed using a stored procedure.

You can use stored procedure to check how many records are in the table -

mysql> delimiter //
mysql> create procedure CountingRecords()
   −> begin
   −> select count(*) as AllRecords from StoredProcTable;
   −> end //
Query OK, 0 rows affected (0.19 sec)

mysql> delimiter ;
Copy after login

Call the stored procedure using CALL command. The query is as follows −

mysql> call CountingRecords();
Copy after login

The following output is shown Count of records −

+------------+
| AllRecords |
+------------+
|          3 |
+------------+
1 row in set (0.31 sec)

Query OK, 0 rows affected (0.33 sec)
Copy after login

The above is the detailed content of Create delimited stored procedure in MySQL. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!