mysql游标使用mydb;

wgeznvg7  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(390)

谢谢你的回答。我用你的改变改变了程序,但我做错了什么。你能告诉我代码有什么问题吗?所以我需要在主要问题的末尾加上代码。尝试2在底部。
有人能告诉我这个程序有什么问题吗?系统告诉我在以下位置有错误:

DECLARE myemail_cursor CURSOR FOR 
USE v_db; SELECT DISTINCT u.mail FROM users u INNER JOIN users_roles ur ON u.uid=ur.uid INNER JOIN role r ON ur.rid=r.rid WHERE r.name = 'court administrator' AND from_unixtime(u.access) >= NOW() - INTERVAL 1 YEAR;

如果我运行mydb;在查询中选择…它可以正常工作。我不能使用'使用变量;在程序中选择?

BEGIN

 DECLARE v_finished, v_finished1 BOOLEAN DEFAULT FALSE;
 DECLARE v_email varchar(4000) DEFAULT "";
 DECLARE v_db varchar(400) DEFAULT "";

 DECLARE mydb_cursor CURSOR FOR 
SELECT TABLE_SCHEMA AS 'database' FROM information_schema.TABLES WHERE TABLE_TYPE = 'BASE TABLE' and table_name='users';

 DECLARE CONTINUE HANDLER FOR NOT FOUND SET v_finished = TRUE; 
open mydb_cursor;
get_mydb: LOOP
FETCH FROM mydb_cursor INTO v_db;
 IF v_finished THEN 
CLOSE mydb_cursor;
 LEAVE get_mydb;
 END IF;

 DECLARE myemail_cursor CURSOR FOR 
USE v_db; SELECT DISTINCT u.mail FROM users u INNER JOIN users_roles ur ON u.uid=ur.uid INNER JOIN role r ON ur.rid=r.rid WHERE r.name = 'court administrator' AND from_unixtime(u.access) >= NOW() - INTERVAL 1 YEAR;

 DECLARE CONTINUE HANDLER FOR NOT FOUND SET v_finished1 = TRUE;

OPEN myemail_cursor;

get_myemail: LOOP
 FETCH FROM myemail_cursor INTO v_email';
 IF v_finished1  THEN 
   CLOSE myemail_cursor;
   SET v_finished1 = FALSE;
   LEAVE get_myemail;
 END IF;

 -- build email list
 SET email_list = CONCAT(v_email," ",email_list);

 END LOOP get_myemail;

CLOSE myemail_cursor;

 END LOOP get_email;

 CLOSE email_cursor;

 END LOOP get_mydb;

  END

尝试#2。我得到这个错误:表'v\u db.users'不存在第一个游标只选择表用户存在的dbs,所以我不明白为什么我得到这个错误。

BEGIN

 DECLARE v_finished, v_finished1 BOOLEAN DEFAULT FALSE;
 DECLARE v_email varchar(4000) DEFAULT "";
 DECLARE v_db varchar(400) DEFAULT "";

 DECLARE mydb_cursor CURSOR FOR 
SELECT TABLE_SCHEMA AS 'database' FROM information_schema.TABLES WHERE TABLE_TYPE = 'BASE TABLE' and table_name='users';

 DECLARE CONTINUE HANDLER FOR NOT FOUND SET v_finished = TRUE; 
open mydb_cursor;

get_mydb: LOOP
FETCH FROM mydb_cursor INTO v_db;
 IF v_finished THEN CLOSE mydb_cursor;
 LEAVE get_mydb;
 END IF;

BLOCK2: BEGIN
 DECLARE myemail_cursor CURSOR FOR 
 SELECT DISTINCT u.mail FROM v_db.users u INNER JOIN v_db.users_roles ur ON u.uid=ur.uid INNER JOIN v_db.role r ON ur.rid=r.rid WHERE r.name = 'court administrator' AND from_unixtime(u.access) >= NOW() - INTERVAL 1 YEAR;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET v_finished1 = TRUE;

OPEN myemail_cursor;

get_myemail: LOOP
 FETCH FROM myemail_cursor INTO v_email;
 IF v_finished1 THEN 
CLOSE myemail_cursor;
   LEAVE get_myemail;
 END IF;

 SET email_list = CONCAT(v_email," ",email_list);
END LOOP get_myemail;

END BLOCK2;
END LOOP get_mydb;

END
r7xajy2e

r7xajy2e1#

23.2.1存储例程语法
...
调用例程时,将执行隐式的use db\u name(并在例程终止时撤消)。不允许在存储例程中使用语句。
...
尝试:

...
DECLARE mydb_cursor CURSOR FOR
  SELECT
    DISTINCT u.mail
  FROM
    v_db.users u
      INNER JOIN v_db.users_roles ur ON u.uid=ur.uid
      INNER JOIN v_db.role r ON ur.rid=r.rid
  WHERE
    r.name = 'court administrator' AND
    from_unixtime(u.access) >= NOW() - INTERVAL 1 YEAR;
...

记得:
13.6.3声明语法
...
只允许在begin中声明。。。结束复合语句,并且必须在其开始处,在任何其他语句之前。
...

相关问题