How to break recursive calls between procedures on MySQL? - 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

Related

How to automatic return value of "PI" from function without assign in operation part of function body

My code is under , i will be gratefull for any suggestion
(* //const
//pi=3.1415926;
//uses
//mathh.inc; *)
var
r,pole_kola,obwod_kola: real;
function Pi: valreal;
begin
Pi:=3.1415926;
end;
procedure dane();
begin
read(r);
end;
procedure obliczenia();
begin
pole_kola:= {pi}Pi*r*r;
obwod_kola:= 2*{pi}Pi*r;
end;
procedure wyniki();
begin
writeln('pole koła: ',pole_kola:4:8);
writeln('obwód koła: ',obwod_kola:4:8);
end;
begin
writeln('podaj promien r: ');
dane();
obliczenia();
wyniki();
end.
How i can use function Pi :
https://www.freepascal.org/docs-html/rtl/system/pi.html
to return automatic value of "PI" from function without assign in operation part of function body if i try modify function get back
Function result does not seem to be set
function Pi() :valreal;
begin
end;
begin
WriteLn('pi = ', Pi():1:20);
end.
Compiling main.pas
main.pas(1,10) Warning: Function result does not seem to be set
Linking a.out
8 lines compiled, 0.1 sec
1 warning(s) issued
pi = 0.00000000000000000000
in program
 ./main
podaj promien r:
6
pole koła: 0.00000000
obwód koła: 0.00000000

In the task, I wanted to automatically use a ready value for PI (3.14…) without using my function. My function didn’t returned a value because I didn’t assigned one. Like we see here:
function Pi() :valreal;
begin
//here is nothing but must be returned a value
end;
begin
WriteLn('pi = ', Pi():1:20);
end.
Going by #derpirscher’s comment, the function written by hand always needs to return something. So I commented part of my syntax, and used the built-in function named PI. (Pascal includes that function.)
(* function pi: valreal; // If I define my own function, it must return a value
begin
pi:=3.1415926; // So in the body of the function, we must assign value
end; *)
We see that here
procedure obliczenia();
begin
pole_kola:= {pi}Pi*r*r; // using build in function
obwod_kola:= 2*{pi}Pi*r; // as above
end;
If we need to use the value of PI in our task/homework, we can use predefinied built-in functions because it is easier; it is good practice to use less syntax in our code.
Must remember: If we define a function i.e., named Pi ourselves, it has to return a value.
Under the comment, the entire syntax of my code with corrections:
var
r,pole_kola,obwod_kola: real;
(* function pi: valreal; // If I define my own function, it must return a value
begin
pi:=3.1415926; // So in the body of the function, we must assign value
end; *)
procedure dane();
begin
read(r);
end;
procedure obliczenia();
begin
pole_kola:= {pi}Pi*r*r; // using build in function
obwod_kola:= 2*{pi}Pi*r; // as above
end;
procedure wyniki();
begin
writeln('pole koła: ',pole_kola:4:8);
writeln('obwód koła: ',obwod_kola:4:8);
end;
begin
writeln('podaj promien r: ');
dane();
obliczenia();
wyniki();
end.

MySQL Stored Function Syntax Error

I've spent hours and I can't understand why this is highlighted red and I don't know what my error is.
CREATE FUNCTION Student_Section_Count(StudentID INT)
Returns INT
Begin
DECLARE section_Registred INT;
SET section_Registred= (Select COUNT(DISTINCT Section.ID) as 'Students Registration Count'
FROM Section
Inner Join
Registration on registration.StudentID=Section.ID
Where registration.StudentID=StudentID);
Return Section_Registred;
END$$
Delimiter;
It highlights END and Delimiter, as well as INT from Return INT
By default semi-colon ; is the delimiter in mysql. So when using a mysql client, the function definition that is passed to server is only up to the first semi-colon i.e. DECLARE section_Registred INT;. Rest of the definition of function is not passed to server. This produces an error. To incorporate semi-colon in function definition you need to change your delimiter from semi-colon to some thing else. So before defining the function write the below sql to change the delimiter:
DELIMITER $$.
After the above sql write sql for your function. Append the new delimiter $$ to END at the end of definition of your function. Now the whole definition of function is passed to server up to the new delimiter. This will fix your error. Change the delimiter back to default after your function definition ends as below:
DELIMITER ;.
Also remember to choose a delimiter that is not present anywhere inside your function definition.
Well, I guess I figured it out on my own.
DELIMITER $$
CREATE FUNCTION Student_Section_Count(StudentID INT)
Returns INT
Begin
DECLARE section_Registred INT;
SET section_Registred= (Select COUNT(DISTINCT Section.ID) as 'Students Registration Count'
FROM Section
Inner Join
Registration on registration.StudentID=Section.ID
Where registration.StudentID=StudentID);
Return Section_Registred;
END$$

how to get out value of one procedure into another procedure in mysql

I have two procedure namely "createOrder" And "createAndress"
And in "createOrder" procedure i have 4 IN parameter and 1 out parameter.
And in "createAndress" procedure i have 3 IN parameter and 1 out parameter.
And i am calling "createAndress" inside the "createOrder" procedure like below
CALL create_address (in_userprofile_id, in_pin, true, in_address_id);
but how to get out value of "createAndress" into "createOrder" procedure ?
Do this inside your CreateOrder stored proc
-- Declare the variable to receive the output value of the procedure.
DECLARE #OutParameterForCreateAddress INT;
-- Execute the procedure specifying in_userprofile_id, in_pin, true,in_address_id for the input parameter
-- and saving the output value in the variable #OutParameterForCreateAddress
EXECUTE create_address in_userprofile_id, in_pin, true, in_address_id, #YourOutParameterNameinCreatedAddress = #OutParameterForCreateAddress OUTPUT;
-- Display the value returned by the procedure.
Print #OutParameterForCreateAddress

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 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.