drop PROCEDURE if exists my_procedure; create PROCEDURE my_procedure(in my_id int,out my_name char) BEGIN select list_name into my_name from aa_list where list_id=my_id; end; call my_procedure(24,@a); select @a;
mysql> DELIMITER //
mysql> CREATE PROCEDURE HelloWorld2(
-> IN vUserName VARCHAR(10),
-> OUT vOutValue VARCHAR(10),
-> INOUT vInOutValue VARCHAR(10))
-> BEGIN
-> SELECT CONCAT('Hello ', vUserName);
-> SET vOutValue = 'A';
-> SET vInOutValue = 'B';
-> END//
Query OK, 0 rows affected (0.00 sec)
mysql> call HelloWorld2('Edward', @a, @b)//
+-----------------------------+
| CONCAT('Hello ', vUserName) |
+-----------------------------+
| Hello Edward |
+-----------------------------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
mysql> select @a//
+------+
| @a |
+------+
| A |
+------+
1 row in set (0.00 sec)
mysql> select @b//
+------+
| @b |
+------+
| B |
+------+
1 row in set (0.00 sec)
參考資料:hi.baidu.com/...1.html
表名是不可以直接用變量,你可以多看看 PREPARE用法。
eg:
DROP PROCEDURE IF EXISTS `table`;
DELIMITER //
CREATE PROCEDURE `table`(IN tname varchar(64))
BEGIN
SET @sqlcmd = CONCAT('SELECT count(1) FROM ', tname);
PREPARE stmt FROM @sqlcmd;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END//
DELIMITER ;
call table('test');