· 9 years ago · Oct 22, 2016, 05:38 PM
1-- --------------------------------------------------------------------------------
2-- Routine DDL
3-- Note: comments before and after the routine body will not be stored by the server
4-- --------------------------------------------------------------------------------
5DELIMITER $$
6
7CREATE DEFINER=`root`@`localhost` PROCEDURE `proc_teste`()
8BEGIN
9 -- declarando variaveis
10 DECLARE fim INT DEFAULT false;
11 DECLARE var_id_dado_coletado INT;
12 DECLARE var_valor_identificador varchar(45);
13 DECLARE var_pergunta varchar(100);
14 DECLARE var_resposta varchar(100);
15 DECLARE var_des_resposta varchar(100);
16 DECLARE tmp_valor_identificador varchar(45);
17
18
19
20 DECLARE tmp_nome varchar(100);
21 DECLARE tmp_cpf varchar(100);
22 DECLARE tmp_nome_beneficiado varchar(100);
23 DECLARE tmp_cpf_beneficiado varchar(100);
24 DECLARE tmp_estado varchar(100);
25 DECLARE tmp_municipio varchar(100);
26 DECLARE tmp_bairro varchar(100);
27 DECLARE tmp_logradouro varchar(100);
28 DECLARE tmp_endereco varchar(100);
29 DECLARE tmp_numero varchar(100);
30 DECLARE tmp_complemento varchar(100);
31 DECLARE tmp_cep varchar(100);
32 DECLARE tmp_idade boolean;
33 DECLARE tmp_carcaca boolean;
34 DECLARE tmp_borracha boolean;
35
36
37 -- criando o cursor
38 DECLARE curDados CURSOR FOR
39 SELECT
40 dc.id_dado_coletado,
41 dc.valor_identificador AS uc,
42 pep.des AS pergunta,
43 p.resposta_livre,
44 pepi.des AS resposta
45 FROM
46 erp_dado_coletado dc
47 INNER JOIN
48 erp_dado_coletado_item dci ON (dci.id_dado_coletado = dc.id_dado_coletado
49 AND dci.stat <> 0)
50 INNER JOIN
51 erp_pesquisa p ON (p.id_pesquisa = dci.id_registro
52 AND tabela_registro = 'erp_pesquisa'
53 AND p.stat <> 0)
54 INNER JOIN
55 com_projeto_elemento_pesquisa pep ON (pep.id_projeto_elemento_pesquisa = p.id_projeto_elemento_pesquisa)
56 LEFT JOIN
57 com_projeto_elemento_pesquisa_item pepi ON (pepi.id_projeto_elemento_pesquisa_item = p.id_projeto_elemento_pesquisa_item)
58 LEFT JOIN
59 com_projeto_elemento pe ON (pe.id_projeto_elemento = dc.id_projeto_elemento)
60 where dc.valor_identificador in (
61 select
62 d.valor_identificador
63
64 from
65 erp_dado_coletado d
66 inner join erp_dado_coletado_item i on d.id_dado_coletado = i.id_dado_coletado and i.stat <> 0 and i.tabela_registro = 'erp_pesquisa'
67 inner join com_projeto_elemento e on e.id_projeto_elemento = d.id_projeto_elemento and e.stat <> 0
68 inner join erp_pesquisa p on p.id_pesquisa = i.id_registro and p.stat <> 0
69 where
70 d.dt_inicial >= '2016-08-01 00:00:00' and d.dt_inicial <= '2016-08-09 23:59:59' and d.status_qualidade_pesquisa = 1
71 and e.id_projeto = 187 -- and e.id_projeto_elemento = 25
72 -- and (p.id_projeto_elemento_pesquisa_item = 230 and p.id_projeto_elemento_pesquisa_item = 237 and p.id_projeto_elemento_pesquisa_item = 238)
73 ) and dc.stat <> 0 order by dc.valor_identificador;
74
75 DECLARE CONTINUE handler FOR NOT found SET fim = TRUE;
76
77 -- criando tabela temporaria
78 DROP TEMPORARY TABLE IF EXISTS tmp_teste;
79 CREATE TEMPORARY TABLE tmp_teste (
80 valor_identificador varchar(45),
81 nome_titular varchar(100),
82 cpf_titular varchar(100),
83 nome_beneficiado varchar(100),
84 cpf_beneficiado varchar(100),
85 estado varchar(100),
86 municipio varchar(100),
87 bairro varchar(100),
88 logradouro varchar(100),
89 endereco varchar(100),
90 numero varchar(100),
91 complemento varchar(100),
92 cep varchar(100)
93 )ENGINE=MyISAM DEFAULT CHARSET=latin1;
94
95 -- zerando variaveis
96 SET tmp_valor_identificador = '';
97 SET tmp_idade = false;
98 SET tmp_carcaca = false;
99 SET tmp_borracha = false;
100
101 OPEN curDados;
102
103 -- loop no cursor e gravando na tabela temporaria
104 READ_LOOP:LOOP
105 FETCH curDados
106 INTO
107 var_id_dado_coletado,
108 var_valor_identificador,
109 var_pergunta,
110 var_resposta,
111 var_des_resposta;
112
113
114 IF (tmp_valor_identificador = '') then
115 set tmp_valor_identificador = var_valor_identificador;
116 end IF;
117 -- qual é pergunta se for endereco se for alguma dessas eu guardo em variaveis
118 if(tmp_valor_identificador = var_valor_identificador) then
119 if((var_pergunta = 'QUAL A IDADE DA GELADEIRA?') && (var_des_resposta = 'MAIOR QUE 5 ANOS'))then
120 set tmp_idade = true;
121 end if;
122 if((var_pergunta = 'COMO ESTA A CARCACA DA GELADEIRA ?') && (var_des_resposta = 'RUIM'))then
123 set tmp_carcaca = true;
124 end if;
125 if((var_pergunta = 'COMO ESTA A BORRACHA DA GELADEIRA?') && (var_des_resposta = 'RUIM'))then
126 set tmp_borracha = true;
127 end if;
128 if(var_pergunta = 'NOME TITULAR')then
129 set tmp_nome = var_resposta;
130 end if;
131 if(var_pergunta = 'CPF TITULAR')then
132 set tmp_cpf = var_resposta;
133 end if;
134 if(var_pergunta = 'NOME BENEFICIADO')then
135 set tmp_nome_beneficiado = var_resposta;
136 end if;
137 if(var_pergunta = 'CPF BENEFICIADO')then
138 set tmp_cpf_beneficiado = var_resposta;
139 end if;
140 if(var_pergunta = 'ESTADO')then
141 set tmp_estado = var_des_resposta;
142 end if;
143 if(var_pergunta = 'MUNICIPIO')then
144 set tmp_municipio = var_des_resposta;
145 end if;
146 if(var_pergunta = 'BAIRRO')then
147 set tmp_bairro = var_des_resposta;
148 end if;
149 if(var_pergunta = 'LOGRADOURO')then
150 set tmp_logradouro = var_des_resposta;
151 end if;
152 if(var_pergunta = 'ENDERECO')then
153 set tmp_endereco = var_resposta;
154 end if;
155 if(var_pergunta = 'NUMERO')then
156 set tmp_numero = var_resposta;
157 end if;
158 if(var_pergunta = 'COMPLEMENTO')then
159 set tmp_complemento = var_resposta;
160 end if;
161 if(var_pergunta = 'CEP')then
162 set tmp_cep = var_resposta;
163 end if;
164
165 set tmp_valor_identificador = var_valor_identificador;
166 ELSE
167 if(tmp_idade = true and tmp_carcaca = true and tmp_borracha = true) then
168 insert into tmp_teste (valor_identificador,nome_titular,cpf_titular,nome_beneficiado,cpf_beneficiado,estado,municipio,bairro,logradouro,endereco,numero,complemento,cep) values (tmp_valor_identificador,tmp_nome,tmp_cpf,tmp_nome_beneficiado,tmp_cpf_beneficiado,tmp_estado,tmp_municipio,tmp_bairro,tmp_logradouro,tmp_endereco,tmp_numero,tmp_complemento,tmp_cep);
169 end if;
170 set tmp_nome = '';
171 set tmp_cpf = '';
172 set tmp_nome_beneficiado = '';
173 set tmp_cpf_beneficiado = '';
174 set tmp_estado = '';
175 set tmp_municipio = '';
176 set tmp_bairro = '';
177 set tmp_logradouro = '';
178 set tmp_endereco = '';
179 set tmp_numero = '';
180 set tmp_complemento = '';
181 set tmp_cep = '';
182 set tmp_idade = false;
183 set tmp_carcaca = false;
184 set tmp_borracha = false;
185
186 if((var_pergunta = 'QUAL A IDADE DA GELADEIRA?') && (var_des_resposta = 'MAIOR QUE 5 ANOS'))then
187 set tmp_idade = true;
188 end if;
189 if((var_pergunta = 'COMO ESTA A CARCACA DA GELADEIRA ?') && (var_des_resposta = 'RUIM'))then
190 set tmp_carcaca = true;
191 end if;
192 if((var_pergunta = 'COMO ESTA A BORRACHA DA GELADEIRA?') && (var_des_resposta = 'RUIM'))then
193 set tmp_borracha = true;
194 end if;
195 if(var_pergunta = 'NOME TITULAR')then
196 set tmp_nome = var_des_resposta;
197 end if;
198 if(var_pergunta = 'CPF TITULAR')then
199 set tmp_cpf = var_des_resposta;
200 end if;
201 if(var_pergunta = 'NOME BENEFICIADO')then
202 set tmp_nome_beneficiado = var_resposta;
203 end if;
204 if(var_pergunta = 'CPF BENEFICIADO')then
205 set tmp_cpf_beneficiado = var_resposta;
206 end if;
207 if(var_pergunta = 'ESTADO')then
208 set tmp_estado = var_resposta;
209 end if;
210 if(var_pergunta = 'MUNICIPIO')then
211 set tmp_municipio = var_resposta;
212 end if;
213 if(var_pergunta = 'BAIRRO')then
214 set tmp_bairro = var_resposta;
215 end if;
216 if(var_pergunta = 'LOGRADOURO')then
217 set tmp_logradouro = var_resposta;
218 end if;
219 if(var_pergunta = 'ENDERECO')then
220 set tmp_endereco = var_des_resposta;
221 end if;
222 if(var_pergunta = 'NUMERO')then
223 set tmp_numero = var_resposta;
224 end if;
225 if(var_pergunta = 'COMPLEMENTO')then
226 set tmp_complemento = var_resposta;
227 end if;
228 if(var_pergunta = 'CEP')then
229 set tmp_cep = var_resposta;
230 end if;
231
232 set tmp_valor_identificador = var_valor_identificador;
233 end if;
234
235
236 IF fim THEN
237 LEAVE read_loop;
238 end IF;
239 end LOOP;
240
241 close curDados;
242
243 -- select na tabela temporaria
244 SELECT * FROM tmp_teste order by 1;
245 -- deletando tabela temporaria
246 DROP TEMPORARY TABLE IF EXISTS tmp_teste;
247
248END