Advertisement
Guest User

Untitled

a guest
Nov 25th, 2015
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 1.65 KB | None | 0 0
  1. CREATE DEFINER=`myroot`@`%` PROCEDURE `SP_OrganizaEnvioPesquisa`()
  2. BEGIN
  3.     DECLARE no_more_rows BOOLEAN DEFAULT FALSE;
  4.     DECLARE registrantsIds INT DEFAULT 0;
  5.    
  6.     DECLARE cur CURSOR FOR
  7.     SELECT RegistrantId FROM db_didyoubuy.Questionario GROUP BY RegistrantId;
  8.    
  9.     DECLARE CONTINUE handler FOR NOT FOUND SET no_more_rows := TRUE;
  10.    
  11.     OPEN cur;
  12.    
  13.     questoes : LOOP
  14.    
  15.     FETCH cur into registrantsIds;
  16.         IF no_more_rows THEN
  17.             leave questoes;
  18.         CLOSE cur;
  19.     END IF;
  20.        
  21.     INSERT IGNORE INTO db_didyoubuy.Grupo (GrupoId, RegistrantIdReferencia) SELECT UUID(), registrantsIds;
  22.     COMMIT;
  23.    
  24.     INSERT IGNORE INTO db_didyoubuy.QuestionarioENV (IdQuestionario, NomeDestinatario, EmailDestinatario, TelefoneDestinatario, GrupoId, DataVisita,
  25.     DataEnvio, DataResposta, RegistrantId, HeadingId, HeadingName, CompanyId, CompanyName, TipoVisita, TotalAdPoints, AWSMessageId)  
  26.     SELECT
  27.          Id AS IdQuestionario,
  28.          NomeDestinatario,
  29.          EmailDestinatario,
  30.          TelefoneDestinatario,
  31.          (select GrupoId from db_didyoubuy.Grupo where RegistrantIdReferencia = registrantsIds) as Grupo,
  32.          DataVisita,
  33.          now() AS DataEnvio,
  34.          now() AS DataResposta,      
  35.          RegistrantId,
  36.          HeadingId,
  37.          HeadingName,
  38.          CompanyId,
  39.          CompanyName,
  40.          TipoVisita,
  41.          TotalAdPoints,
  42.          UUID() AS AWSMessageId
  43.      FROM
  44.      db_didyoubuy.Questionario
  45.      WHERE RegistrantId = registrantsIds ORDER BY TotalAdPoints DESC LIMIT 5;  
  46.     COMMIT;
  47.    
  48.     END LOOP questoes;    
  49.  
  50.     CLOSE cur;    
  51.    
  52. END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement