首頁 > 資料庫 > mysql教程 > MySQL 預存程序帶in和out參數以及PHP,PB如何呼叫的小例子

MySQL 預存程序帶in和out參數以及PHP,PB如何呼叫的小例子

黄舟
發布: 2017-02-17 11:50:15
原創
1725 人瀏覽過

 

最簡單的例子:

mysql> DELIMITER $$
mysql> USE test $$
Database changed
mysql> DROP PROCEDURE IF EXISTS `sp_add`$$
Query OK, 0 rows affected (0.00 sec)

mysql> CREATE PROCEDURE sp_add(a INT, b INT,OUT c INT)
    -> BEGIN 
    -> SET c=a+ b;
    -> END$$
Query OK, 0 rows affected (0.00 sec)

mysql> DELIMITER ;
登入後複製
mysql> CALL sp_add (1,2,@c);
Query OK, 0 rows affected (0.00 sec)mysql> SELECT @c;
+------+
| @c   |
+------+
|    3 |
+------+
1 row in set (0.00 sec)mysql>
登入後複製


一個稍微複雜的例子:

如何在php中呼叫MySQL的存儲過程?本人沒有測試過,從朋友那裡借鑒過實例,如下:

mysql> show create table t_BillNo;
+----------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table    | Create Table                                                                                                                                                                        |
+----------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| t_BillNo | CREATE TABLE `t_billno` (
  `SaleNo` bigint(20) DEFAULT NULL,
  `bmh` varchar(20) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 CHECKSUM=1 DELAY_KEY_WRITE=1 ROW_FORMAT=DYNAMIC |
+----------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> select * from t_BillNo;
+--------+------+
| SaleNo | bmh  |
+--------+------+
|      1 | 2    |
|      4 | 3    |
|      4 | 5    |
|      7 | 7    |
|     12 | 8    |
+--------+------+
5 rows in set (0.00 sec)

mysql> 
mysql> DELIMITER $$
mysql> USE test $$
Database changed
mysql> DROP PROCEDURE IF EXISTS `sp_GetMaxNumber`$$
Query OK, 0 rows affected (0.01 sec)

DELIMITER $$
USE test $$
DROP PROCEDURE IF EXISTS `sp_GetMaxNumber`$$
CREATE PROCEDURE sp_GetMaxNumber (IN v_bmh VARCHAR(6), OUT v_MaxNo INT)
BEGIN
	START TRANSACTION;
	UPDATE t_BillNo
	SET SaleNo = IFNULL(SaleNo,0)+1 
	WHERE bmh = v_bmh;
	IF @@error_count = 0 THEN
		BEGIN
		  SELECT Saleno INTO v_MaxNo FROM t_BillNo WHERE bmh = v_bmh;
		COMMIT;
		END;
	ELSE
		BEGIN
		     ROLLBACK;
		     SET v_MaxNo = 0;
		END;
	END IF;
END$$
DELIMITER ;

mysql> CREATE PROCEDURE sp_GetMaxNumber (IN v_bmh VARCHAR(6), OUT v_MaxNo INT)
    -> BEGIN
    -> START TRANSACTION;
    -> UPDATE t_BillNo
    -> SET SaleNo = IFNULL(SaleNo,0)+1 
    -> WHERE bmh = v_bmh;
    -> IF @@error_count = 0 THEN
    -> BEGIN
    ->   SELECT Saleno INTO v_MaxNo FROM t_BillNo WHERE bmh = v_bmh;
    -> COMMIT;
    -> END;
    -> ELSE
    -> BEGIN
    ->      ROLLBACK;
    ->      SET v_MaxNo = 0;
    -> END;
    -> END IF;
    -> END$$
Query OK, 0 rows affected (0.00 sec)

mysql> DELIMITER ;
mysql> 

mysql> call sp_GetMaxNumber(8,@v_MaxNo);
Query OK, 0 rows affected (0.00 sec)

mysql> select @v_MaxNo;
+----------+
| @v_MaxNo |
+----------+
|       12 |
+----------+
1 row in set (0.00 sec)
登入後複製


 


但是在PB中調用,報錯如下(來自itpub的網友的例子),有類似經歷的朋友請分享下經驗啊:

在PB W_MAIN窗體的CB_1.CLICK事件中:


$sql = "call test.sp_GetMaxNumber(8,@c);";
mysql_query($sql);//调用sp_GetMaxNumber的存储过程
$result = mysql_query('select @c;');
$array = mysql_fetch_array($result);
echo &#39;<pre class="brush:php;toolbar:false">&#39;;print_r($array);
登入後複製

但PB顯示顯示錯誤:

string ls_bmh, ls_errtext
int li_maxno
ls_bmh = &#39;0901&#39;
sp_GetMaxNumber(ls_bmh, li_maxno)
ls_errtext = sqca.sqlerrtext
messagebox(&#39;Error&#39;,string(li_maxno)+&#39; &#39; +sqlca.sqlerrtext)
登入後複製
MyH呼叫的小例子的內容,更多相關內容請關注PHP中文網(www.php.cn)! 

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板