在MySQL中建立帶有分隔符號的預存程序

王林
發布: 2023-09-09 17:57:08
轉載
726 人瀏覽過

在MySQL中建立帶有分隔符號的預存程序

您可以使用create procedure指令建立預存程序。語法如下 −

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

delimiter //
登入後複製

將上述語法套用至建立預存程序。查詢如下−

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)
登入後複製

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

mysql> delimiter ;
登入後複製

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

CALL yourStoredProcedureName();
登入後複製

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

mysql> call Sp_callTableStoredProcTable();
登入後複製

以下是輸出−

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

Query OK, 0 rows affected (0.06 sec)
登入後複製

在上面,我們使用了表格'StoredProcTable',這個表格有三筆記錄。使用預存程序顯示了所有記錄。

您可以使用預存程序檢查表格中有多少筆記錄-

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

mysql> delimiter ;
登入後複製

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

mysql> call CountingRecords();
登入後複製

以下輸出顯示記錄的計數−

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

Query OK, 0 rows affected (0.33 sec)
登入後複製

以上是在MySQL中建立帶有分隔符號的預存程序的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:tutorialspoint.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!