if v_test_id IS NOT NULL AND exists (select test_id from physicians where test_id = v_test_id) then
select 'phys_id' = -1;
elseif
if v_addl_language1 = '' THEN SELECT v_addl_language1 = NULL;
if v_addl_language2 = '' THEN SELECT v_addl_language2 = NULL;
if v_addl_language3 = '' THEN SELECT v_addl_language3 = NULL;
The error is occuring at if v_addl_language1...
I've tooled with adding parentheticals around the parameter of the if statements but that doesn't seem to fix it. Any ideas?
You miss END IF; statement for every IF
The sintaxe is IF expression THEN commands; ELSEIF anotherExpression THEN commands; ELSE commands; END IF;
So your code at the elseif part should be:
ELSEIF someexpression THEN
if v_addl_language1 = '' THEN SELECT v_addl_language1 = NULL; end if;
if v_addl_language2 = '' THEN SELECT v_addl_language2 = NULL; end if;
if v_addl_language3 = '' THEN SELECT v_addl_language3 = NULL; end if;
END IF;
Related
Though my procedure code has proper "if"s (that is "if"s that are opened and closed properly), mysql workbench 8.0 is throwing an error that all "if"s are not closed properly. I can't find any other mistake too. Please help me out.
If I am adding extra "if"s in the end, the errors are gone. But it is not correct since those "end if"s do not have any if so that they can be closed.Though it is not showing any error, while applying the procedure with the additional "end if"s, it is not getting applied instead throws some other error.
IF(Ser ='search') THEN
IF(Value = 0) THEN
IF EXISTS(SELECT Role FROM U WHERE Name = name) THEN
IF(O = 0) THEN
IF(F1 <> NULL AND F1V <> NULL AND
F2 = NULL AND F2V = NULL
) THEN
IF EXISTS (SELECT * FROM Com ) THEN
SET V=0;
END IF;
ELSE IF (F1<> NULL AND F1V <> NULL AND
F2 <> NULL AND F2V <> NULL
) THEN
IF EXISTS ( SELECT * FROM Com ) THEN
SET V=1;
END IF;
END IF;
ELSE IF(O = 1) THEN
SET A=0;
END IF;
END IF;
END IF;
END IF;
I changed the else if condition to if and it started working.
While trying to update a trigger, MySQL tells me the query ";" failed. How is ";" even a query in MySQL's view is beyond me.
The exact message is:
The following query has failed: ";" MySQL said: #1065 - Query was empty
Here's the new trigger (AFTER INSERT):
BEGIN
DECLARE vIdPlacet VARCHAR(40);
DECLARE vTypeTravaux VARCHAR(32);
DECLARE vEssence VARCHAR(3) DEFAULT '-';
DECLARE vClasseHau VARCHAR(5) DEFAULT '-';
DECLARE vNoMesurag int;
DECLARE new_id_parcelle INT UNSIGNED DEFAULT 0;
DECLARE new_no_microplacette INT UNSIGNED DEFAULT 0;
IF NEW.deleted = 0 THEN
SELECT id_parcelle, no_microplacette
INTO new_id_parcelle, new_no_microplacette
FROM microplacette
WHERE id_microplacette = NEW.id_microplacette;
SELECT travaux, no_mesurag, id__placet
INTO vTypeTravaux, vNoMesurag, vIdPlacet
FROM secteur
LEFT JOIN parcelle ON secteur.id_secteur = parcelle.id_secteur
WHERE id_parcelle = new_id_parcelle;
IF vTypeTravaux = 'inventaire' THEN
SELECT abbreviation INTO vEssence FROM essences WHERE _id = NEW.id_essence;
IF NEW.hauteur_15 = 1 THEN
SET vClasseHau = '15CM+';
END IF;
IF (SELECT COUNT(*) FROM imported_pres_ess WHERE id__placet = vIdPlacet AND
caracteris = '-' AND
classe_hau = vClasseHau AND
essence = vEssence AND
no_mesurag = vNoMesurag AND
no_micro_p = new_no_microplacette) = 0 THEN
INSERT INTO imported_pres_ess (id__placet, caracteris, classe_hau, essence, no_mesurag, no_micro_p)
VALUES (vIdPlacet, '-', vClasseHau, vEssence, vNoMesurag, new_no_microplacette);
END IF;
IF (SELECT COUNT(*) FROM imported_semi_gau WHERE id__placet = vIdPlacet AND
classe_hau = vClasseHau AND
essence = vEssence AND
no_mesurag = vNoMesurag AND
no_micro_p = new_no_microplacette) = 0 THEN
INSERT INTO imported_semi_gau (id__placet, classe_hau, essence, no_mesurag, no_micro_p)
VALUES (vIdPlacet, vClasseHau, vEssence, vNoMesurag, new_no_microplacette);
END IF;
IF NEW.diametre > 0 THEN
SET vClasseHau = 'D2_D8';
ELSE
SET vClasseHau = '-';
END IF;
IF (SELECT COUNT(*) FROM imported_pres_ess WHERE id__placet = vIdPlacet AND
caracteris = '-' AND
classe_hau = vClasseHau AND
essence = vEssence AND
no_mesurag = vNoMesurag AND
no_micro_p = new_no_microplacette) = 0 THEN
INSERT INTO imported_pres_ess (id__placet, caracteris, classe_hau, essence, no_mesurag, no_micro_p)
VALUES (vIdPlacet, '-', vClasseHau, vEssence, vNoMesurag, new_no_microplacette);
END IF;
IF (SELECT COUNT(*) FROM imported_semi_gau WHERE id__placet = vIdPlacet AND
classe_hau = vClasseHau AND
essence = vEssence AND
no_mesurag = vNoMesurag AND
no_micro_p = new_no_microplacette) = 0 THEN
INSERT INTO imported_semi_gau (id__placet, classe_hau, essence, no_mesurag, no_micro_p)
VALUES (vIdPlacet, vClasseHau, vEssence, vNoMesurag, new_no_microplacette);
END IF;
END IF;
END IF;
END
I tried creating the procedure you show, but I don't get any error.
The error about "empty statement" happens when you try to execute a query through the API but the query string is empty.
I can duplicate the error in the mysql client this way:
mysql> set #s = '';
mysql> prepare stmt from #s;
ERROR 1065 (42000): Query was empty
So I suggest you look not at the stored procedure, but whatever code you're executing this from, and check that every time you try to execute a query, that you submit a non-empty string.
It turns out, the trigger I was updating got deleted in the meantime, so I was updating a trigger that didn't exist anymore.
I found out after refreshing the page (the trigger was gone from the trigger list).
I simply recreated the trigger anew and it worked.
I've created a procedure in my DB of a veterinary clinic that shows what is: the first date, the first time available with a veterinarian authorized to perform the service requested (there mustn't be other appointments booked at the same time). However when I call this procedure the cursor stops because the value of eof is 1, but it didn't really see all the records: the cursor leaves the loop before the end of rows! I can not understand why.
here is the code:
DELIMITER //
DROP PROCEDURE IF EXISTS first_booking_available //
CREATE PROCEDURE first_booking_available(IN Perf VARCHAR(30), IN DateP DATE)
BEGIN
DECLARE IdV, eof INTEGER;
DECLARE Dat DATE;
DECLARE i, TimeB, End TIME;
DECLARE vet CURSOR FOR E.Id_vet, W.Date, S.Start_time, S.End_time
FROM Enabled E, Work W, WorkShift S
WHERE E.Code_performance = Perf AND W.Id_vet = E.Id_vet AND S.Code = W.Code_workshift AND W.Date >= DateP ORDER BY W.Date, S.Start_time;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET eof = 1;
SELECT Duration INTO #Duration FROM Perfomance WHERE Code = Perf;
SET #Start_time = NULL;
OPEN vet;
read_loop: LOOP
FETCH vet INTO IdV, Dat, TimeB, End;
IF (eof = 1 OR #Start_time IS NOT NULL) THEN
LEAVE read_loop;
END IF;
SET i = TimeB;
WHILE i < End AND #Start_time IS NULL DO
SET #P = NULL;
SET #F = NULL;
SELECT ADDTIME(ADDTIME(B.Start_time, P.Duration), '00:01:00') INTO #P
FROM Booking B, Performance P
WHERE P.Code = B.Code_performance AND B.Date = Dat AND B.Id_vet = IdV AND B.Start_time = i;
SELECT ADDTIME(ADDTIME(B.Start_time, P.Duration), '00:01:00') INTO #F
FROM Booking B, Performance P
WHERE P.Code = B.Code_performance AND B.Date = Dat AND B.Id_vet = IdV AND B.Start_time BETWEEN i AND ADDTIME(i, #Duration) ORDER BY ADDTIME(B.Start_time, P.Duration) DESC LIMIT 1;
IF #P IS NOT NULL THEN
SET i = #P;
END IF;
IF #F IS NOT NULL THEN
SET i = #F;
END IF;
IF ADDTIME(i, #duration) > End THEN
SET i = End;
END IF;
IF ((#P IS NULL) AND (#F IS NULL) AND (i != End)) THEN
SET #Start_Time = i;
END IF;
END WHILE;
IF #Start_Time IS NOT NULL THEN
SET #Id_vet = IdV;
SET #Date = Dat;
END IF;
END LOOP;
CLOSE vet;
IF #Start_Time IS NULL THEN
SIGNAL sqlstate '99994'
SET MESSAGE_TEXT = 'there is no availability !';
END IF;
END //
DELIMITER ;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET eof = 1;
is mean your query has no result. It is not apply just cursor, another select query too.
SELECT ADDTIME(ADDTIME(B.Start_time, P.Duration), '00:01:00') INTO #P
FROM Booking B, Performance P
WHERE P.Code = B.Code_performance AND B.Date = Dat AND B.Id_vet = IdV AND B.Start_time = i;
SELECT ADDTIME(ADDTIME(B.Start_time, P.Duration), '00:01:00') INTO #F
FROM Booking B, Performance P
WHERE P.Code = B.Code_performance AND B.Date = Dat AND B.Id_vet = IdV AND B.Start_time BETWEEN i AND ADDTIME(i, #Duration) ORDER BY ADDTIME(B.Start_time, P.Duration) DESC LIMIT 1;
this query has no result. HANDLER catch them. and leave cursor.
I wrote a Stored Procedure, that uses a variable with the sql query to make it dynamic depending on a set of parameters, my problem is when I try the LIKE clause with % wildcards, the query is not filtering, I tested with a regular query and it works.
CREATE DEFINER=`root`#`localhost` PROCEDURE `seleccionar_exp_repGral`(in p_fecha date, in p_fecha2 date, in p_compania int, in p_institucion int, in p_sector int,
in p_clasificacion int, in p_analista int, in p_decision int, in p_fuenteFinanciamiento int, in p_tipo int, in p_objeto varchar(3500))
BEGIN
set #qrystr = "select Expe.exp, Expe.mes, Expe.anio, Expe.fecha_recibido, cia.nombre_com, inst.nombre_inst, Expe.monto_egreso,
Expe.anio_egreso, Expe.monto_ingreso, Expe.anio_ingreso, an.nombre_ana, deci.nombre_dec, Expe.fecha_sesion, sec.nombre_sec,
clas.nombre_cla, finan.nombre_fin, tipo.nombre_tipo from expediente Expe
left outer join compania cia on (Expe.compania = cia.idcompania)
left outer join institucion inst on (Expe.institucion = inst.idinstitucion)
left outer join analista an on (Expe.analista = an.idanalista)
left outer join decision deci on (Expe.decision = deci.iddecision)
left outer join sector sec on (Expe.sector = idsector)
left outer join clasificacion clas on (Expe.clasificacion = idclasificacion)
left outer join financiamiento finan on (Expe.financiamiento = finan.idfinanciamiento)
left outer join tipo_expediente tipo on (Expe.tipo = tipo.idtipo_expediente)";
-- variables for filter
set #pcia = p_compania;
set #pinst = p_institucion;
set #psector = p_sector;
set #pclasificacion = p_clasificacion;
set #panalista = p_analista;
set #pdecision = p_decision;
set #pfuenteFinanciamiento = p_fuenteFinanciamiento;
set #ptipo = p_tipo;
set #pObjeto = p_objeto;
-- concatenate conditions for filter
set #qrystr = CONCAT(#qrystr,'where Expe.fecha_recibido >=\'',p_fecha,'\' and Expe.fecha_recibido <=\'',p_fecha2,
'\'');
-- Filters
IF #pcia > 0 THEN
set #qrystr = CONCAT(#qrystr,' and Expe.compania = ',#pcia);
end if;
IF #pinst > 0 THEN
set #qrystr = CONCAT(#qrystr,' and Expe.institucion = ',#pinst);
end if;
IF #psector > 0 THEN
set #qrystr = CONCAT(#qrystr,' and Expe.sector = ',#psector);
end if;
IF #pclasificacion > 0 THEN
set #qrystr = CONCAT(#qrystr,' and Expe.clasificacion = ',#pclasificacion);
end if;
IF #panalista > 0 THEN
set #qrystr = CONCAT(#qrystr,' and Expe.analista = ',#panalista);
end if;
IF #pdecision > 0 THEN
set #qrystr = CONCAT(#qrystr,' and Expe.decision = ',#pdecision);
end if;
IF #pfuenteFinanciamiento > 0 THEN
set #qrystr = CONCAT(#qrystr,' and Expe.financiamiento = ',#pfuenteFinanciamiento);
end if;
IF #ptipo > 0 THEN
set #qrystr = CONCAT(#qrystr,' and Expe.tipo = ',#ptipo);
end if;
IF #pObjeto <> "" THEN
set #qrtystr = CONCAT(#qrystr," and Expe.objeto LIKE '",#pObjeto,"'");
end if;
-- preparamos el ultimo statement del query
set #qrystr = CONCAT(#qrystr,' order by Expe.idexpediente asc');
-- preparamos datos finales
PREPARE stmt from #qrystr;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END$$
From that long SP I have this part:
IF #pObjeto <> "" THEN
set #qrtystr = CONCAT(#qrystr," and Expe.objeto LIKE '",#pObjeto,"'");
end if;
This line I added the wildcard % before the #pObjeto and after that and it's not working, I even added wildcard to the variable, and my query still shows all registers without the text filtering.
I wonder if it has to do with string formatting or something.
I appreciate any help.
drop function if exists rty_check_member_info_status;
DELIMITER $$
--
-- Functions
CREATE DEFINER=root#localhost FUNCTION rty_check_member_info_status(memb_id int,field_name_1 varchar(100),field_name_2 varchar(100),login_member_amount int(11),login_status char(1)) RETURNS char(1) CHARSET latin1
begin
declare fn_field_name_1 varchar(100) ;
declare fn_field_name_2 varchar(100) ;
declare fn_amount_for_profile_visible int(11);
declare fn_return char(1) default 'N';
declare test_field varchar(100);
select field_name_1,field_name_2,amount_for_profile_visible into
fn_field_name_1,fn_field_name_2,fn_amount_for_profile_visible
from member_account_settings inner join tbl_members on member_account_settings.member_auto_id = tbl_members.member_id
where tbl_members.member_id = memb_id ;
if fn_field_name_1 = 'H' Then
set fn_return = 'N' ;
else
if fn_field_name_2 = 'Y' Then
if fn_amount_for_profile_visible = '0' Then
set fn_return = 'Y' ;
else
if login_status = 1 Then
if fn_amount_for_profile_visible > login_member_amount Then
set fn_return = 'N' ;
else
set fn_return = 'Y' ;
end if;
else
set fn_return = 'N';
end if ;
end if;
else
set fn_return = 'Y';
end if ;
end if ;
return fn_return ;
end$$
DELIMITER ;
You're two choices are pretty much generated SQL(normally a bad idea because it's harder to write, debug and document) and using a case statement to select the column based on the name matching a string(which is normally a pretty good solution).
Here's an example of the second, since it's the solution I'd definitely recommend.
SET #test_field1 = "last_name_display_status" ;
SET #test_field2 = "last_name_display_for_other_partcpnt" ;
SELECT
CASE #test_field1
-- List columns here that you might want to return:
WHEN 'last_name_display_status' THEN last_name_display_status
WHEN 'last_name_display_for_other_partcpnt' THEN last_name_display_for_other_partcpnt
WHEN 'create_date' THEN create_date
-- Return a value for an invalid name here:
ELSE NULL
END AS test_field1,
CASE #test_field2
-- List columns here that you might want to return:
WHEN 'last_name_display_status' THEN last_name_display_status
WHEN 'last_name_display_for_other_partcpnt' THEN last_name_display_for_other_partcpnt
WHEN 'create_date' THEN create_date
-- Return a value for an invalid name here:
ELSE NULL
END AS test_field2,
-- Rest of select unaffected by this change
amount_for_profile_visible
INTO
fn_field_name_1,
fn_field_name_2,
fn_amount_for_profile_visible
FROM member_account_settings
INNER JOIN tbl_members
ON member_account_settings.member_auto_id = tbl_members.member_id
WHERE
tbl_members.member_id = memb_id
;
And for the sake of completeness a copy of the first solution I proposed(the generated SQL):
-- Need to use #vars, since named vars aren't in scope for the generated SQL:
SET #output1 = '';
SET #output2 = '';
SET #output3 = '';
SET #input1 = memb_id;
-- We also need to store our generated SQL to a variable
SET #query = 'SELECT ' + #test_field1 + ',' + #test_field2 + ', amount_for_profile_visible INTO #output1, #output2, #output3 FROM member_account_settings INNER JOIN tbl_members ON member_account_settings.member_auto_id = tbl_members.member_id WHERE tbl_members.member_id = ?';
-- To execute the code we have to convert it to a prepared statement
-- named stmt here, because it's what most people use in this instance
PREPARE stmt FROM #query;
-- Execute the statement using our input variable
EXECUTE stmt USING #input1;
-- Delete the prepared statement now we've run it.
DEALLOCATE PREPARE stmt;
-- Store our #vars back into the named vars.
SET fn_field_name_1 = #output1;
SET fn_field_name_2 = #output2;
SET fn_amount_for_profile_visible = #output3;