mysql 创建和使用存储过程

一、连接数据库

mysql -uroot -p 

输入密码后,

a、使用show databases; 查看数据库。

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test               |
| webinfo            |
+--------------------+
6 rows in set (0.00 sec)

b、切换数据库 use webinfo;

mysql> use webinfo;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed

c、查看表及表结构 

show tables;

mysql> show tables;
+--------------------+
| Tables_in_webinfo  |
+--------------------+
| spider_event       |
| spider_record      |
| spider_record_list |
+--------------------+
3 rows in set (0.00 sec)

desc spider_record;

mysql> desc spider_record;
+--------------------+--------------+------+-----+---------+----------------+
| Field              | Type         | Null | Key | Default | Extra          |
+--------------------+--------------+------+-----+---------+----------------+
| id                 | int(11)      | NO   | PRI | NULL    | auto_increment |
| starttime          | datetime     | YES  |     | NULL    |                |
| endtime            | datetime     | YES  |     | NULL    |                |
| keyword            | varchar(512) | YES  |     | NULL    |                |
| result_type        | int(11)      | YES  |     | NULL    |                |
| findcounts         | int(11)      | YES  |     | NULL    |                |
| keyword_findcounts | int(11)      | YES  |     | NULL    |                |
+--------------------+--------------+------+-----+---------+----------------+
7 rows in set (0.00 sec)

二、创建存储过程

a、CREATE PROCEDURE 存储过程名

delimiter //
CREATE PROCEDURE proc_add_spider_recorda(
IN stime datetime,
IN etime datetime,
IN kword nvarchar(100), 
IN rtype INTEGER,
IN fcounts INTEGER,
IN kfcounts INTEGER,
OUT retid int
)
BEGIN

start transaction;
insert into spider_record(starttime,endtime,keyword,result_type,findcounts,keyword_findcounts) 
values(stime,etime,kword,rtype,fcounts,kfcounts);
SELECT LAST_INSERT_ID() into retid;
commit; 
END
//

注意:delimiter // 是定义结束符。mysql 默认的结束符是;号,这里重新定义了结束符为//,否则中间的语句中遇到 ;号,会认为语句已经结束了。所以需要重新定义结束符,否则会报错。END 后面加//,就是代表已经结束。感觉好坑

b、查看存储过程

show produre proc_add_spider_recorda;

 

三、使用存储过程 

SET @retid=-1;#@retid 是 out参数
CALL proc_add_spider_record('2019-01-01 12:00:00','2019-01-01 12:05:00','a',1,0,0,@retid) ;
SELECT @retid;

 

 

你可能感兴趣的:(互联网及相关)