MySQL equivalent session variable for Oracle - mysql

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.

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.

How to return a String value from a Stored Procedure in MySQL?

I want to return a value from Stored Procedure which I can use in my Spring Boot server in JPA repository.
Here's my Stored Procedure,I want to know how do I return String from the Procedure.
DELIMITER //
CREATE PROCEDURE GetAllProducts(out query varchar(20))
BEGIN
SET query=SELECT title FROM course where description="just a course";
RETURN #query;
END //
DELIMITER ;
I am selecting 1 value from the query table to return it.
Can someone Help?
Thanks!
If you want to use your SP as datasource (like you execute SELECT, not CALL, in external program) you must select to output stream, not to variable:
CREATE PROCEDURE GetAllProducts()
SELECT title
FROM course
WHERE description="just a course";
Got the Solution,Its working now!
Just tweaked the Procedure a little bit:
DELIMITER //
CREATE PROCEDURE GetAllProdcuts(OUT output VARCHAR(50))
BEGIN
SELECT title into output
FROM course
WHERE description="just a course";
END //

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

Getting stored procedure's return value with iBatis.NET

How can I retrieve the return value of a stored procedure using iBatis.NET? The below code successfully calls the stored procedure, but the QueryForObject<int> call returns 0.
SqlMap
<procedure id="MyProc" parameterMap="MyProcParameters" resultClass="int">
MyProc
</procedure>
<parameterMap id="MyProcParameters">
<parameter property="num"/>
</parameterMap>
C# code
public int RunMyProc( string num )
{
return QueryForObject < int > ( "MyProc", new Hashtable { { "num", num } } );
}
Stored Procedure
create procedure MyProc
#num nvarchar(512)
as
begin
return convert(int, #num)
end
FYI, I'm using iBatis 1.6.1.0, .NET 3.5, and SQL Server 2008.
It's not pretty, but this works:
SqlMap
<statement id="MyProc" parameterClass="string" resultClass="int">
declare #num int
exec #num = MyProc #value#
select #num
</statement>
C# code
public int RunMyProc( string num )
{
return QueryForObject < int > ( "MyProc", num );
}
You might want to check out the following article
http://www.barebonescoder.com/2010/04/ibatis-net-stored-procedures-return-values/ on how to retrieve return values.
I've used it in QueryForObject and Insert scenarios where the last statement is a return statement in the stored procedure.
Pay particular attention to the class attribute on the "parameterMap" element. It's a lot prettier than the answer above and I believe it's more inline with the way IBatis.Net was intended to be used.
Stored procedures don't have a return value like functions.
So, I don't think that will work. Try using output parameters instead.
I'm not sure about your application logic, but your procedure would be better like this:
create procedure MyProc
#num nvarchar(512)
as
begin
DECLARE #ReturnValue int
BEGIN TRY
SET #ReturnValue=convert(int, #num)
END TRY
BEGIN CATCH
SET #ReturnValue=0 --procedures can not return null, so set some default here
END CATCH
return #ReturnValue
end