1
I’d like to ask a question, or just show me the way.
There is a table called Questionrio from it I take the records in a precedent and organize the data in it.
Here is my code:
CREATE DEFINER=`myroot`@`%` PROCEDURE `SP_OrganizaEnvioPesquisa`()
BEGIN
DECLARE no_more_rows BOOLEAN DEFAULT FALSE;
DECLARE registrantsIds INT DEFAULT 0;
DECLARE cur CURSOR FOR
SELECT RegistrantId FROM db_didyoubuy.Questionario GROUP BY RegistrantId;
DECLARE CONTINUE handler FOR NOT FOUND SET no_more_rows := TRUE;
OPEN cur;
questoes : LOOP
FETCH cur into registrantsIds;
IF no_more_rows THEN
leave questoes;
CLOSE cur;
END IF;
INSERT IGNORE INTO db_didyoubuy.Grupo (GrupoId, RegistrantIdReferencia) SELECT UUID(), registrantsIds;
COMMIT;
INSERT IGNORE INTO db_didyoubuy.QuestionarioENV (IdQuestionario, NomeDestinatario, EmailDestinatario, TelefoneDestinatario, GrupoId, DataVisita,
DataEnvio, DataResposta, RegistrantId, HeadingId, HeadingName, CompanyId, CompanyName, TipoVisita, TotalAdPoints, AWSMessageId)
SELECT
Id AS IdQuestionario,
NomeDestinatario,
EmailDestinatario,
TelefoneDestinatario,
(select GrupoId from db_didyoubuy.Grupo where RegistrantIdReferencia = registrantsIds) as Grupo,
DataVisita,
now() AS DataEnvio,
now() AS DataResposta,
RegistrantId,
HeadingId,
HeadingName,
CompanyId,
CompanyName,
TipoVisita,
TotalAdPoints,
UUID() AS AWSMessageId
FROM
db_didyoubuy.Questionario
WHERE RegistrantId = registrantsIds ORDER BY TotalAdPoints DESC LIMIT 5;
COMMIT;
END LOOP questoes;
CLOSE cur;
END;
After you run the trial and send a show warnings, return me the following.
I need to insert one for many. In the case of a GUID (UUID) from the Group table for Questionarioenv.
What I do first is a search of all my users (Registrantid) in the table Questionario, and for each of them I insert in the table Group then that same Guid inserted in the table Group I relate to the same user(Registrantid).
The funny thing is that when I Gero the first Inserts in my table Questionarioenv it inserts normal, the second time I need to run the same precedent, it returns me the error.
"Error Code: 1452. Cannot add or update a Child Row: a Foreign key Constraint fails";
My base on sql is pretty basic, I would like a light on how I could fix this precedent or how I could rewrite it.
Thanks in advance.
Can you write a headline that summarizes your problem? The error message is already in the description and doesn’t really explain what your question is about.
– brasofilo
There seems to be an error in the first
INSERT IGNORE
, because the value that causes the question error is the result of the sub-query that looks for the same entered value, try to include aSHOW WARNING
– Pedro Sanção
Sanction if I put something like auto increment would solve? I need to insert one for many, the guid that I create in the group I need to relate it in the Insert below. I don’t know much bank, you have some idea how I could do this?
– Marco
Put your code here in your question, the links to Pastebin are temporary, in some time your question will not have code! To format just add 4 blank spaces before each line!
– Jorge Campos
Jorge the Pastebin links have the option to put as expires Never. Thanks for the touch.
– Marco