MySQL游标UPDATE LOOP

时间:2017-06-08 17:01:07

标签: mysql

我通常会尝试自己解决问题,但我对此感到难过。

我在一个SugarCRM帐户中有潜在客户,我想为每个活跃用户重新分配一个特定的号码。每次我尝试运行时,我都会收到“ERROR 1064(42000):......在第4行”,我不知道出了什么问题。

这是我到目前为止所做的:

您好,我通常会尝试自己解决问题,但我很难过。

我在一个SugarCRM帐户中有潜在客户,我想为每个活跃用户重新分配一个特定的号码。每次我尝试运行时,我都会收到“ERROR 1064(42000):......在第4行”,我不知道出了什么问题。

    DELIMITER $$
    DROP PROCEDURE IF EXISTS  assign_leads $$
    CREATE PROCEDURE assign_leads(num_rows INT)
    BEGIN
    SET num_rows = num_rows;
    DECLARE i VARCHAR(255);
    DECLARE exit_loop BOOLEAN;         
    DECLARE employee_cursor CURSOR FOR
        SELECT users.id
        FROM users
        WHERE (((users.title)="South Carolina Qualifier") AND ((users.status)="Active"));

    DECLARE CONTINUE HANDLER FOR NOT FOUND SET exit_loop = TRUE;

    OPEN employee_cursor;
    employee_loop: LOOP

    FETCH  employee_cursor INTO i;

        SET @sql_text1 = concat('UPDATE leads SET assigned_user_id = ',@i,' WHERE ((assigned_user_id IS NULL OR assigned_user_id = '1' OR assigned_user_id =  '') AND do_not_call = '0' AND deleted = '0' AND status = 'New') LIMIT ',@num_rows,' 1;')
        PREPARE stmt1 FROM @sql_text1;
        EXECUTE stmt1;
        DEALLOCATE PREPARE stmt1;


         IF exit_loop THEN
            CLOSE employee_cursor;
              LEAVE employee_loop;
         END IF;
  END LOOP employee_loop;
    END $$
    DELIMITER ;

2 个答案:

答案 0 :(得分:0)

一些注意事项:

  • SET必须位于DECLARE
  • 之后
...
    CREATE PROCEDURE assign_leads(num_rows INT)
    BEGIN
    -- SET num_rows = num_rows;
    DECLARE i VARCHAR(255);
    DECLARE exit_loop BOOLEAN;         
    DECLARE employee_cursor CURSOR FOR
        SELECT users.id
        FROM users
        WHERE (((users.title)="South Carolina Qualifier") AND ((users.status)="Active"));

    DECLARE CONTINUE HANDLER FOR NOT FOUND SET exit_loop = TRUE;
    SET num_rows = num_rows;
    OPEN employee_cursor;
...

此分配(SET)没有多大意义,它将num_rows参数的值分配给相同的num_rows参数。

  • 必须转义@sql_text1变量中的某些字符,并在语句末尾添加;
...
/*
SET @sql_text1 = concat('
  UPDATE leads SET assigned_user_id = ',@i,'
   WHERE (
          (assigned_user_id IS NULL OR assigned_user_id = '1' OR assigned_user_id =  '') AND
          do_not_call = '0' AND deleted = '0' AND status = 'New'
         )
   LIMIT ',@num_rows,' 1;
')
*/

SET @sql_text1 = concat('
  UPDATE leads SET assigned_user_id = ',@i,'
   WHERE (
          (assigned_user_id IS NULL OR assigned_user_id = \'1\' OR assigned_user_id =  \'\') AND
          do_not_call = \'0\' AND deleted = \'0\' AND status = \'New\'
         )
   LIMIT ',@num_rows,' 1;
');
...

答案 1 :(得分:0)

这是查询...

      DELIMITER $$
        DROP PROCEDURE IF EXISTS  assign_leads $$

  CREATE PROCEDURE assign_leads(num_rows INT)
      BEGIN
      DECLARE i VARCHAR(255);
      DECLARE exit_loop BOOLEAN;         
      DECLARE employee_cursor CURSOR FOR
          SELECT users.id
          FROM users
          WHERE (((users.title)="South Carolina Qualifier") AND((users.status)="Active"));

      DECLARE CONTINUE HANDLER FOR NOT FOUND SET exit_loop = TRUE;
      OPEN employee_cursor;

  employee_loop: LOOP
  FETCH  employee_cursor INTO i;
  SET @sql_text1 = concat('
  UPDATE leads SET assigned_user_id = ',@i,' 
  WHERE (
  (assigned_user_id IS NULL OR assigned_user_id = \'1\' OR assigned_user_id =  \'\') AND

do_not_call = \'0 \'AND deleted = \'0 \'AND status = \'New \'       )LIMIT',@ num_rows,'1;'       );       PREPARE stmt1 FROM @sql_text1;       执行stmt1;       DEALLOCATE PREPARE stmt1;

  IF exit_loop THEN
  CLOSE employee_cursor;
  LEAVE employee_loop;
  END IF;
  END LOOP employee_loop;
   END $$
    DELIMITER ;enter code here