Condional looping in mysql - mysql

Inside MySQL I have written a Procedure where I want to run a loop like this:
While (Cond) Do
...(Body1)
...
If (Condition2)
continue ;
...(Body2)
...
end while ;
Under the while loop I want the full body to run in case where Condition2 is not met (ie Body1 and Body2).
Currently, when Condition 2 is met, it just executes Body1 and then continues(Check Cond in While and Continue looping.)
Can someone help with the proper syntax to perform the above?

BEGIN
WHILE cond1 DO
CALL body1;
IF (NOT cond2) THEN
CALL body2;
END IF;
END WHILE;
END;

Related

Using BULK COLLECT with LIMIT clause

I am a beginner....just want to know why this following script is giving error while compiling.your replies will be helpful
create or replace procedure fetch_employee
is
cursor test_departments_cur
is
select * from test_departments;
Type test_departments_aat is
table of test_departments_cur%ROWTYPE
INDEX BY PLS_INTEGER;
l_test_departments test_departments_aat;
Begin
open test_departments_cur;
loop
fetch test_departments_cur
bulk collect into l_test_departments limit 10;
exit when l_test_departments.count=0;
for i in 1..l_test_departments.count
loop
dbms_output.put_line(l_test_departments(i));
end loop;
dbms_output.put_line('=============================================');
end loop;
close test_departments_cur;
end fetch_employee;
Its showing error: Error(23,5): PLS-00306: wrong number or types of arguments in call to 'PUT_LINE'
in
dbms_output.put_line(l_test_departments(i));
Please answer
"l_test_departments" is an array of type "test_departments_aat" which is holding a record. As its type is "test_departments_cur%rowtype" , so to access a cursor's value ,you need to pass the column of the cursor as follows :
dbms_output.put_line(l_test_departments(i).<column_name>);
Hope this helps.

How to break recursive calls between procedures on MySQL?

I have 2 procedures: A and B.
A calls B and B calls A. I need to block calling if the procedure is already called from another one. How to check that?
MySQL user-defined variables are global to your session.
So you could set a variable to TRUE in procedure A and then check it in procedure B.
Procedure A:
BEGIN
IF NOT #called = 1 THEN
SET #called := 1;
CALL B();
SET #called := NULL;
END IF;
END
Procedure B:
BEGIN
IF NOT #called = 1 THEN
SET #called := 1;
CALL A();
SET #called := NULL;
END IF;
END
These variables are global to the session, that is each session gets its own version of the variable. So you don't have a problem if multiple user sessions are calling procedures.
But you do have a problem that a user-defined variable persists until the end of the current session. That's why I show setting the variable to NULL after the call to the other proc. However, if the proc is interrupted before setting the variable to NULL, you could get some wrong results the next time you try calling a proc.
as variant you may check some value from table IsExecFromA. And Check this in proc B. If IsExecFromA = true then not exec again proc A etc. u?
I have used the following method:
create procedure A()
begin
call B();
call A_Code();
end
create procedure A_Code()
begin
...code...
end
create procedure B()
begin
...code...
call A_Code();
end

Retrieve a value inside a stored procedure and use it inside that stored procedure

delimiter //
CREATE DEFINER=root#localhost PROCEDUREgetData(IN templateName VARCHAR(45),IN templateVersion VARCHAR(45),IN userId VARCHAR(45))
BEGIN
set #version = CONCAT("SELECT saveOEMsData_answersVersion FROMsaveOEMsData WHERE saveOEMsData_templateName = '",templateName,"' ANDsaveOEMsData_templateVersion = ",templateVersion," AND saveOEMsData_userId= ",userId);
PREPARE s1 from #version;
EXECUTE S1;
END // delimiter ;
I am retrieving saveOEMsData_answersVersion, but I have to use it in an IF loop, as in if the version == 1, then I would use a query, else I would use something else. But I am not able to use the version. Could someone help with this?? I am only able to print but not able to use the version for data manipulation. The procedure works fine but I am unable to proceed to next step which is the if condition.
The if condition would have something like the below mentioned.
IF(ver == 1) THEN SELECT "1";
END IF;
IF is allowed inside a Procedure, you can not use IF inside SQL, in SQL you can use CASE, but that doesn't have the same abilities.
Build a second procedure to handle the IF/Then for the results of getData

MySql Trigger - String Comparison in IF Statment

I'm running mysql 5.0 and have created a trigger. Roughly as follows:
SET #temp =(SELECT STATEMENT);
IF(#temp = 'true') THEN
-code block 1-
ELSE
-code block 2-
END IF;
When I run my select statement alone it returns 'true' however in the code above 'code block 2' is being executed. Any idea why?
If your select statement is returning a boolean value then this should work:
SET #temp =(SELECT STATEMENT);
IF(#temp = true) THEN
-code block 1-
ELSE
-code block 2-
END IF;
It looks like #temp contains way more than 'true'. Without any additional information, this seems it should be re-written so you aren't searching through strings. However, this may be what you are looking for (if 'true' is in the return string):
IF (INSTR(#temp, 'true') > 0) THEN
-code block 1-
ELSE
-code block 2-
END IF

MySQL equivalent session variable for Oracle

In MySQL, I can create an access a session variable by using a single #.
Example initialization:
set #myVar = true;
Some trigger containing this code:
if (#myVar is not true) then
execute something
What is the equivalent in Oracle 10g?
SQL> EXEC DBMS_SESSION.SET_CONTEXT('CLIENTCONTEXT', 'myvar', 'myvalue');
PL/SQL procedure successfully completed
SQL> SELECT SYS_CONTEXT('CLIENTCONTEXT', 'myvar') FROM dual;
SYS_CONTEXT('CLIENTCONTEXT','M
--------------------------------------------------------------------------------
myvalue
A package global variable would probably do the same trick.
CREATE OR REPLACE PACKAGE foo as
myVar BOOLEAN;
END foo;
CREATE OR REPLACE PACKAGE BODY foo AS
BEGIN
MyVar := true;
END foo;
BEGIN
If foo.myVar THEN
dbms_output.put_line ('MyVar is True');
end if;
END;
An advantage of using the package over SYS_CONTEXT is that you get some encapsulation.
Why not just use bind variables? In SQL Plus:
variable SOME_NUMBER number
exec :SOME_NUMBER := 10
PL/SQL procedure successfully completed
if :SOME_NUMBER = 10 then
do something;
end if;
/
Works for any kind of Oracle datatype.