This repository has been archived by the owner on Oct 9, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
ex_procedure.sql
96 lines (75 loc) · 1.75 KB
/
ex_procedure.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
USE sakila;
DELIMITER //
CREATE PROCEDURE record_count()
BEGIN
SELECT
'Country count ',
COUNT(*)
FROM Country;
SELECT
'City count ',
COUNT(*)
FROM City;
SELECT
'CountryLanguage count',
COUNT(*)
FROM CountryLanguage;
END//
DELIMITER ;
/*
CALL statement
*/
CALL record_count();
-- ---------------------------------------------------------------------
-- ---------------------------------------------------------------------
USE test;
CREATE TABLE actor(actor_id int, first_name varchar(10), last_name varchar(10));
-- loop leave
CREATE PROCEDURE actor_insert()
BEGIN
set @x = 0;
ins: LOOP
set @x = @x + 1;
IF @x = 10 THEN
leave ins;
ELSEIF mod(@x,2) = 0 THEN
ITERATE ins;
END IF;
INSERT INTO actor(actor_id,first_name,last_name) VALUES (@x+200,'TEST',@x);
END LOOP ins;
END;
$$
CALL actor_insert();
-- ---------------------------------------------------------------------
-- ---------------------------------------------------------------------
USE test;
create table test_table(
tid int ,
tname varchar(20)
);
DROP PROCEDURE IF EXISTS insert_many_rows;
delimiter //
create PROCEDURE insert_many_rows (in loops int)
BEGIN
DECLARE v1 int;
set v1 = loops;
WHILE v1 > 0 DO
insert into test_table VALUES ( v1, 'qqqqqqqqqqwwwwwwwwww');
set v1 = v1 - 1;
end WHILE;
END;
//
DELIMITER ;
CALL insert_many_rows(10);
-- ---------------------------------------------------------------------
-- ---------------------------------------------------------------------
USE test;
DELIMITER $$
DROP PROCEDURE IF EXISTS P_TEST $$
CREATE PROCEDURE P_TEST()
BEGIN
SELECT NOW() FROM DUAL;
END;
$$
DELIMITER ;
CALL P_TEST();