Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. CREATE TABLE `participantes` (
  2. `id` INT(11) NOT NULL AUTO_INCREMENT,
  3. `nome` VARCHAR(50) NULL DEFAULT ‘0’ COLLATE ‘utf8_bin’,
  4. `id_sorteio` INT(11) NULL DEFAULT ‘0’,
  5. PRIMARY KEY (`id`)
  6. )
  7. COLLATE=’utf8_bin’
  8. ENGINE=InnoDB;
  9.  
  10. insert into participantes (nome) values
  11. (‘Bill Gates’), (‘Amancio Ortega’),
  12. (‘Warren Buffett’), (‘Carlos Slim Helu’),
  13. (‘Jeff Bezos’), (‘Mark Zuckerberg’),
  14. (‘Larry Ellison’), (‘Michael Bloomberg’),
  15. (‘Charles Koch’), (‘David Koch’);
  16.  
  17. select * from participantes order by rand();
  18.  
  19. CREATE PROCEDURE `FazerSorteio`()
  20. LANGUAGE SQL
  21. NOT DETERMINISTIC
  22. CONTAINS SQL
  23. SQL SECURITY DEFINER
  24. COMMENT ”
  25. BEGIN
  26.  
  27. DECLARE Pid, Posicao, Restante int;
  28. DECLARE existe_mais_linhas int DEFAULT 0;
  29. DECLARE Sorteio CURSOR FOR select id from participantes order by rand();
  30. DECLARE CONTINUE HANDLER FOR NOT FOUND SET existe_mais_linhas=1;
  31.  
  32. set Posicao = 0;
  33.  
  34. OPEN Sorteio;
  35. meu_loop: LOOP
  36.  
  37. fetch Sorteio into Pid;
  38.  
  39. if existe_mais_linhas = 1 then
  40. LEAVE meu_loop;
  41. END IF;
  42.  
  43. update participantes set id_sorteio = Posicao where id = Pid;
  44.  
  45. set Posicao = Pid;
  46.  
  47. END LOOP;
  48.  
  49. call FazerSorteio;
  50.  
  51. select
  52. p.id,
  53. p.nome,
  54. p.id_sorteio, pp.nome
  55. from participantes p
  56. inner join participantes pp on pp.id_sorteio = p.id
  57. order by p.id, pp.id_sorteio;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement