Question regarding global variable.
Posted in 2005
Topics: Stored Procedures & SPL, Data Types & Schema Design, Platform-Specific Issues
Hi,
I am new to Informix. I would like to get some help on how to use global
variable.
I use IBM Informix Dynamic Server Version 10.00.UC3X9 on a Linux server.
I have a function. Inside the function, I would like set the value of a
global variable when entering and reset it back to null right before
exiting. Something like below:
CREATE FUNCTION SP1(appName varchar(128)) RETURNS BOOLEAN AS retval;
DEFINE retval BOOLEAN;
DEFINE GLOBAL gvar varchar(1024) DEFAULT "";
LET retval = 't';
LET gvar = "SP in progress for " || appName;
...
LET gvar = "":
RETURN retval;
END FUNCTION;
The function could be called by multiple instances of application. I
wonder whether there is a way I can protect the global variable to only
be set by one instance at a time. For example, App1 calls SP1, set gvar
to be "SP in progress for App1". When App2 calls SP1, App2 has to wait
until App1 exit the function (gvar is set back to ""). I don't want to
throw exception immediately for App2 either.
Your help is highly appreciated.
Feng
sending to informix-list
Feng Huang (huangcf) wrote:
> Hi,
>
> I am new to Informix. I would like to get some help on how to use global
> variable.
>
> I use IBM Informix Dynamic Server Version 10.00.UC3X9 on a Linux server.
> I have a function. Inside the function, I would like set the value of a
> global variable when entering and reset it back to null right before
> exiting. Something like below:
>
> CREATE FUNCTION SP1(appName varchar(128)) RETURNS BOOLEAN AS retval;>
> DEFINE retval BOOLEAN;
> DEFINE GLOBAL gvar varchar(1024) DEFAULT "";
>
> LET retval = 't';
> LET gvar = "SP in progress for " || appName;
>
> ...
>
> LET gvar = "":
>
> RETURN retval;
>
> END FUNCTION;
>
> The function could be called by multiple instances of application. I
> wonder whether there is a way I can protect the global variable to only
> be set by one instance at a time. For example, App1 calls SP1, set gvar
> to be "SP in progress for App1". When App2 calls SP1, App2 has to wait
> until App1 exit the function (gvar is set back to ""). I don't want to
> throw exception immediately for App2 either.
In terms of concurrency, what you've got is fine - the 'GLOBAL' variable
is global to the session only, and not visible to any other session.
In terms of your specification, the zero length non-null VARCHAR is
different from a NULL - use LET gvar = NULL to meet your specification.
--
Jonathan Leffler #include <disclaimer.h>
Email: jleffler@earthlink.net, jleffler@us.ibm.com
Guardian of DBD::Informix v2005.02 -- http://dbi.perl.org/