本篇文章將介紹預存程序傳回多個結果集時是什麼結果,希望給大家以參考作用。
mysql儲存函數只傳回一個值。若要開發傳回多個值的預存程序,需要使用INOUT或OUT參數的預存程序。
咱們先來看一個orders表它的結構:
mysql> desc orders; +----------------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------------+-------------+------+-----+---------+-------+ | orderNumber | int(11) | NO | PRI | NULL | | | orderDate | date | NO | | NULL | | | requiredDate | date | NO | | NULL | | | shippedDate | date | YES | | NULL | | | status | varchar(15) | NO | | NULL | | | comments | text | YES | | NULL | | | customerNumber | int(11) | NO | MUL | NULL | | +----------------+-------------+------+-----+---------+-------+ 7 rows in set
然後嘞,咱們來看一個存儲過程,它接受客戶編號,並返回發貨(shipped),取消(canceled ),解決(resolved)和爭議(disputed)的訂單總數(多個結果集):
DELIMITER $$ CREATE PROCEDURE get_order_by_cust( IN cust_no INT, OUT shipped INT, OUT canceled INT, OUT resolved INT, OUT disputed INT) BEGIN -- shipped SELECT count(*) INTO shipped FROM orders WHERE customerNumber = cust_no AND status = 'Shipped'; -- canceled SELECT count(*) INTO canceled FROM orders WHERE customerNumber = cust_no AND status = 'Canceled'; -- resolved SELECT count(*) INTO resolved FROM orders WHERE customerNumber = cust_no AND status = 'Resolved'; -- disputed SELECT count(*) INTO disputed FROM orders WHERE customerNumber = cust_no AND status = 'Disputed'; END
其實,除IN參數之外,預存程序還需要4個額外的OUT參數:shipped, canceled , resolved 和disputed。在預存程序中,使用帶有count函數的select語句根據訂單狀態取得對應的訂單總數,並將其指派給對應的參數。按下上面的sql,我們如果要使用get_order_by_cust預存程序,可以傳遞客戶編號和四個使用者定義的變數來取得輸出值。執行預存程序後,我們再使用SELECT語句輸出變數值:
+----------+-----------+-----------+-----------+ | @shipped | @canceled | @resolved | @disputed | +----------+-----------+-----------+-----------+ | 22 | 0 | 1 | 1 | +----------+-----------+-----------+-----------+ 1 row in set
以上是mysql預存程序傳回多個結果集嗎的詳細內容。更多資訊請關注PHP中文網其他相關文章!