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

來源:互聯網
上載者:User

 

最簡單的例子:

mysql> DELIMITER $$mysql> USE test $$Database changedmysql> 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>

一個稍微複雜的例子:

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 changedmysql> 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)BEGINSTART TRANSACTION;UPDATE t_BillNoSET SaleNo = IFNULL(SaleNo,0)+1 WHERE bmh = v_bmh;IF @@error_count = 0 THENBEGIN  SELECT Saleno INTO v_MaxNo FROM t_BillNo WHERE bmh = v_bmh;COMMIT;END;ELSEBEGIN     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)

如何在php中調用MySQL的預存程序?本人沒有測試過,從朋友那裡借鑒過執行個體,如下:

$sql = "call test.sp_GetMaxNumber(8,@c);";
mysql_query($sql);//調用sp_GetMaxNumber的預存程序
$result = mysql_query('select @c;');
$array = mysql_fetch_array($result);
echo '<pre>';print_r($array);

 

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

在PB W_MAIN表單的CB_1.CLICK事件中:

string ls_bmh, ls_errtext
int li_maxno
ls_bmh = '0901'

sp_GetMaxNumber(ls_bmh, li_maxno)
ls_errtext = sqca.sqlerrtext
messagebox('Error',string(li_maxno)+' ' +sqlca.sqlerrtext)

但PB顯示顯示錯誤:
0 SQLSTATE = 37000
[MYSQL] [ODBC 5.1 DRIVER]YOU HAVE AN ERROR IN YOUR SQL SYNTAX;CHECK THE MANUAL THAT CORRESPONDS TO YOUR MYSQL SERVER VERSION FOR THE RIGHT SYNTAX TO USE NEAR '?=CALL SP_GetMaxNumber(0,_GBK'0901') AT LINE 1.

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.