教程集 www.jiaochengji.com
教程集 >  数据库  >  mysql  >  正文 sql游标中循环使用方法

sql游标中循环使用方法

发布时间:2023-05-04   编辑:jiaochengji.com
教程集为您提供sql游标中循环使用方法等资源,欢迎您收藏本站,我们将为您提供最新的sql游标中循环使用方法资源

sql游标中循环使用方法

select * from Employee;
------ ------------ ----------- ------------ ------------ --------- ----------- -------------
| id   | first_name | last_name | start_date | end_date   | salary  | city      | description |
------ ------------ ----------- ------------ ------------ --------- ----------- -------------
|    1 | Jason      | Martin    | 1996-07-25 | 2006-07-25 | 1234.56 | Toronto   | Programmer  |
|    2 | Alison     | Mathews   | 1976-03-21 | 1986-02-21 | 6661.78 | Vancouver | Tester      |
|    3 | James      | Smith     | 1978-12-12 | 1990-03-15 | 6544.78 | Vancouver | Tester      |
|    4 | Celia      | Rice      | 1982-10-24 | 1999-04-21 | 2344.78 | Vancouver | Manager     |
|    5 | Robert     | Black     | 1984-01-15 | 1998-08-08 | 2334.78 | Vancouver | Tester      |
|    6 | Linda      | Green     | 1987-07-30 | 1996-01-04 | 4322.78 | New York  | Tester      |
|    7 | David      | Larry     | 1990-12-31 | 1998-02-12 | 7897.78 | New York  | Manager     |
|    8 | James      | Cat       | 1996-09-17 | 2002-04-15 | 1232.78 | Vancouver | Tester      |
------ ------------ ----------- ------------ ------------ --------- ----------- -------------
8 rows in set (0.00 sec)

mysql>
mysql>
mysql>
mysql> delimiter $$
mysql> CREATE PROCEDURE myProc (in_customer_id INT)
    -> BEGIN
    ->
    ->      DECLARE l_first_name  VARCHAR(30);
    ->      DECLARE l_id          INT;
    ->      DECLARE l_city        VARCHAR(30);
    ->      DECLARE l_department_count INT;
    ->      DECLARE no_more_departments INT;
    ->
    ->      DECLARE dept_csr CURSOR FOR
    ->           SELECT id,first_name, city
    ->             FROM employee;
    ->
    ->      DECLARE CONTINUE HANDLER FOR NOT FOUND SET no_more_departments=1;
    ->
    ->     SET no_more_departments=0;
    ->       OPEN dept_csr;
    ->      dept_loop:WHILE(no_more_departments=0) DO
    ->           FETCH dept_csr INTO l_id,l_first_name,l_city;
    ->           IF no_more_departments=1 THEN
    ->                LEAVE dept_loop;
    ->           END IF;
    ->           SET l_department_count=l_department_count 1;
    ->           select l_id,l_first_name,l_city;
    ->      END WHILE dept_loop;
    ->      CLOSE dept_csr;
    ->      SET no_more_departments=0;
    ->
    -> END$$
Query OK, 0 rows affected (0.00 sec)

mysql> delimiter ;
mysql>
mysql> call myProc(1);
------ -------------- ---------
| l_id | l_first_name | l_city  |
------ -------------- ---------
|    1 | Jason        | Toronto |
------ -------------- ---------
1 row in set (0.01 sec)

------ -------------- -----------
| l_id | l_first_name | l_city    |
------ -------------- -----------
|    2 | Alison       | Vancouver |
------ -------------- -----------
1 row in set (0.01 sec)

------ -------------- -----------
| l_id | l_first_name | l_city    |
------ -------------- -----------
|    3 | James        | Vancouver |
------ -------------- -----------
1 row in set (0.01 sec)

------ -------------- -----------
| l_id | l_first_name | l_city    |
------ -------------- -----------
|    4 | Celia        | Vancouver |
------ -------------- -----------
1 row in set (0.01 sec)

------ -------------- -----------
| l_id | l_first_name | l_city    |
------ -------------- -----------
|    5 | Robert       | Vancouver |
------ -------------- -----------
1 row in set (0.01 sec)

------ -------------- ----------
| l_id | l_first_name | l_city   |
------ -------------- ----------
|    6 | Linda        | New York |
------ -------------- ----------
1 row in set (0.01 sec)

------ -------------- ----------
| l_id | l_first_name | l_city   |
------ -------------- ----------
|    7 | David        | New York |
------ -------------- ----------
1 row in set (0.03 sec)

------ -------------- -----------
| l_id | l_first_name | l_city    |
------ -------------- -----------
|    8 | James        | Vancouver |
------ -------------- -----------
1 row in set (0.03 sec)

Query OK, 0 rows affected (0.03 sec)

mysql>
mysql>
mysql> drop procedure myProc;
Query OK, 0 rows affected (0.00 sec)

mysql>
mysql>
mysql>
mysql>
mysql>
mysql> drop table Employee;
Query OK, 0 rows affected (0.00 sec)

mysql>

您可能感兴趣的文章:
有关mysql游标的使用相关问题
mysql存储过程中使用游标的实例详解
MySQL存储过程游标使用实例详解
mysql动态游标与mysql存储过程游标(示例)
Python数据库API(DB API)
Python(SQLite)executescript用法(
jquery $.each 和for怎么跳出循环终止本次循环
mysql 存储过程中游标与多游标实例
mysql的游标怎么使用
while循环中不支持循环使用curl的实例代码

[关闭]
~ ~