mysql 存储过程
/* Navicat MySQL Data Transfer Source Server : localhost Source Server Version : 50519 Source Host : localhost:3306 Source Database : test Target Server Type : MYSQL Target Server Version : 50519 File Encoding : 65001 Date: 2015-11-23 11:28:21 */ SET FOREIGN_KEY_CHECKS=0;
– Table structure for club_customer
DROP TABLE IF EXISTS club_customer;
CREATE TABLE club_customer (
id int(11) NOT NULL AUTO_INCREMENT,
custname varchar(255) DEFAULT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
– Records of club_customer
INSERT INTO club_customer VALUES (‘1’, ‘宅男’);
INSERT INTO club_customer VALUES (‘2’, ‘杜艳龙’);
INSERT INTO club_customer VALUES (‘3’, ‘金辉’);
表名为参数
use test;
delimiter // – 改变分隔符
drop procedure if exists pr_test1 //
create procedure pr_test1(a int ,b int , tablename varchar(255)) – 形参a,b
begin
declare c varchar(255) ; – 声明变量
if a is null then set a = 0; end if ; if b is null then set b = 2; end if ; set @c=tablename ; set @s= concat(‘select * from ‘,@c); prepare stmt from @s; execute stmt; DEALLOCATE prepare stmt; end 调用的时候 set @a = 1; set @b = 5 ; call pr_test1(@a,@b,‘club_customer’);