473,387 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

DB2 9.5 global variables

Interesting! I was going to ask if such a thing existed, but I was pretty
much convinced they did not so I didn't ask.

Looks like with version 9.5 DB2 supports global variables:

"Global variables improve data sharing between SQL statements.
Version 9.5 introduces the concept of global variables, which are named
memory variables that you can access and modify through SQL statements.
Global variables enable you to share data between different SQL statements
running in the same session (or connection) without the need for application
logic to support this data transfer."

The reason I had wanted something like this is for applications where a
generic userid is used for an application to connect to a database, but a
specific userid is used by the user when using the application. This way I
think we can do something like this:

CREATE VARIABLE global.userid VARCHAR(20) DEFAULT NULL^

CREATE TABLE maint_log (
userid VARCHAR(20)
, field_name VARCHAR(40) NOT NULL
, old_value VARCHAR(255)
, new_value VARCHAR(255)
)^

CREATE TRIGGER TEST.NAME_LOG_TR
AFTER UPDATE OF name, city, state
ON TEST.TABLE1
REFERENCING OLD AS old NEW AS new
FOR EACH ROW
MODE DB2SQL
BEGIN ATOMIC
IF NOT old.name = new.name THEN
INSERT INTO maint_log (userid, field_name, old_value, new_value)
VALUES (global.userid, 'NAME', old.name, new.name);
END IF;
IF NOT old.city = new.city THEN
INSERT INTO maint_log (userid, field_name, old_value, new_value)
VALUES (global.userid, 'CITY', old.city, new.city);
END IF;
IF NOT old.state = new.state THEN
INSERT INTO maint_log (userid, field_name, old_value, new_value)
VALUES (global.userid, 'STATE', old.state, new.state);
END IF;
END^

And then in an application:

CONNECT ...
SET global.userid = 'JAUser';

UPDATE test.table1
SET name = 'Frank Swarbrick'
, state = 'CA'
WHERE name = 'Francis J Swarbrick';

And then
SELECT * FROM maint_log;
gives:
USERID FIELD_NAME OLD_VALUE
NEW_VALUE
-------------------- ----------------------------------------
--------------------- -------------------
JAUser NAME Francis J
Swarbrick Frank Swarbrick
JAUser STATE CO
CA

This would allow the application to connect to the database and immediately
set the 'global.userid' variable. Then later it could do updates to tables
that have triggers attached and the trigger could retrieve the global.userid
variable and insert it in to the maintenance log table.

This seems to work, and seems to be to be a decent idea. Any thoughts?

One thing about the trigger is, could there possibly be a way to make the IF
and INSERT statements more generic so that I don't have to add a new one for
each column that I want to do logging for?

Anyway, cool new feature in DB2!

Frank

Feb 23 '08 #1
6 2543
You may want to look up "trusted context". Another cool feature in Db2
9.5 which may be more applicable to your problem.

Cheers
Serge

PS: Glad you like global vars :-)
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Feb 23 '08 #2
>>On 2/22/2008 at 6:25 PM, in message
<47******************@efirstbank.com>,
Frank Swarbrick<Fr*************@efirstbank.comwrote:
Interesting! I was going to ask if such a thing existed, but I was
pretty
much convinced they did not so I didn't ask.

Looks like with version 9.5 DB2 supports global variables:

"Global variables improve data sharing between SQL statements.
Version 9.5 introduces the concept of global variables, which are named
memory variables that you can access and modify through SQL statements.
Global variables enable you to share data between different SQL
statements
running in the same session (or connection) without the need for
application
logic to support this data transfer."

The reason I had wanted something like this is for applications where a
generic userid is used for an application to connect to a database, but
a
specific userid is used by the user when using the application. This
way I
think we can do something like this:

CREATE VARIABLE global.userid VARCHAR(20) DEFAULT NULL^

CREATE TABLE maint_log (
userid VARCHAR(20)
, field_name VARCHAR(40) NOT NULL
, old_value VARCHAR(255)
, new_value VARCHAR(255)
)^

CREATE TRIGGER TEST.NAME_LOG_TR
AFTER UPDATE OF name, city, state
ON TEST.TABLE1
REFERENCING OLD AS old NEW AS new
FOR EACH ROW
MODE DB2SQL
That's wierd. It cut off the rest of my message and put it in some
unreadable attachment.

CREATE TRIGGER TEST.NAME_LOG_TR
AFTER UPDATE OF
NAME
, CITY
, STATE
ON TEST.TABLE1
REFERENCING OLD AS old NEW AS new
FOR EACH ROW
MODE DB2SQL
BEGIN ATOMIC
IF NOT old.name = new.name THEN
INSERT INTO maint_log (userid, field_name, old_value, new_value)
VALUES (global.userid, 'NAME', old.name, new.name);
END IF;
IF NOT old.city = new.city THEN
INSERT INTO maint_log (userid, field_name, old_value, new_value)
VALUES (global.userid, 'CITY', old.city, new.city);
END IF;
IF NOT old.state = new.state THEN
INSERT INTO maint_log (userid, field_name, old_value, new_value)
VALUES (global.userid, 'STATE', old.state, new.state);
END IF;
END

SET global.userid = 'JAUser';

insert into test.table1
values ('Francis J Swarbrick', 'Lakewood', 'CO');

update test.table1
set name = 'Frank Swarbrick'
where name = 'Francis J Swarbrick';

select * from maint_log;

I wonder if there's a way to make a more generic IF statement so that I
don't have to add a new one for each column...?

Frank

Feb 25 '08 #3
>>On 2/22/2008 at 8:20 PM, in message
<62*************@mid.individual.net>,
Serge Rielau<sr*****@ca.ibm.comwrote:
You may want to look up "trusted context". Another cool feature in Db2
9.5 which may be more applicable to your problem.
I have looked at that a bit, but I don't think it will work for me.
Firstly, it appears that it cannot be used from an 'embedded SQL'
application. Is this true?

Secondly, does the 'alternate user ID' have to be defined to DB2 as an
actual user? I don't think that would work out well for us, with a few
thousand possible users.

Thanks,
Frank

Feb 25 '08 #4


n 2/25/2008 at 9:53 AM, in message <47******************@efirstbank.com>,
Frank Swarbrick<Fr*************@efirstbank.comwrote:
>>>On 2/22/2008 at 6:25 PM, in message
<47******************@efirstbank.com>,
Frank Swarbrick<Fr*************@efirstbank.comwrote:
>Interesting! I was going to ask if such a thing existed, but I was
pretty
much convinced they did not so I didn't ask.

Looks like with version 9.5 DB2 supports global variables:

"Global variables improve data sharing between SQL statements.
Version 9.5 introduces the concept of global variables, which are named
memory variables that you can access and modify through SQL statements.
Global variables enable you to share data between different SQL
statements
running in the same session (or connection) without the need for
application
logic to support this data transfer."

The reason I had wanted something like this is for applications where a
generic userid is used for an application to connect to a database, but
a
specific userid is used by the user when using the application. This
way I
think we can do something like this:

CREATE VARIABLE global.userid VARCHAR(20) DEFAULT NULL^

CREATE TABLE maint_log (
userid VARCHAR(20)
, field_name VARCHAR(40) NOT NULL
, old_value VARCHAR(255)
, new_value VARCHAR(255)
)^

CREATE TRIGGER TEST.NAME_LOG_TR
AFTER UPDATE OF name, city, state
ON TEST.TABLE1
REFERENCING OLD AS old NEW AS new
FOR EACH ROW
MODE DB2SQL

That's wierd. It cut off the rest of my message and put it in some
unreadable attachment.

CREATE TRIGGER TEST.NAME_LOG_TR
AFTER UPDATE OF
NAME
, CITY
, STATE
ON TEST.TABLE1
REFERENCING OLD AS old NEW AS new
FOR EACH ROW
MODE DB2SQL
It did it again! *!#@&#@ Let's try something else...

..CREATE TRIGGER TEST.NAME_LOG_TR
.. AFTER UPDATE OF
.. NAME
.. , CITY
.. , STATE
.. ON TEST.TABLE1
.. REFERENCING OLD AS old NEW AS new
.. FOR EACH ROW
.. MODE DB2SQL
..BEGIN ATOMIC
.. IF NOT old.name = new.name THEN
.. INSERT INTO maint_log (userid, field_name, old_value, new_value)
.. VALUES (global.userid, 'NAME', old.name, new.name);
.. END IF;
.. IF NOT old.city = new.city THEN
.. INSERT INTO maint_log (userid, field_name, old_value, new_value)
.. VALUES (global.userid, 'CITY', old.city, new.city);
.. END IF;
.. IF NOT old.state = new.state THEN
.. INSERT INTO maint_log (userid, field_name, old_value, new_value)
.. VALUES (global.userid, 'STATE', old.state, new.state);
.. END IF;
..END@
Feb 25 '08 #5
>>On 2/25/2008 at 12:05 PM, in message
<47******************@efirstbank.com>,
Frank Swarbrick<Fr*************@efirstbank.comwrote:
>
n 2/25/2008 at 9:53 AM, in message <47******************@efirstbank.com>,
Frank Swarbrick<Fr*************@efirstbank.comwrote:
>>>>On 2/22/2008 at 6:25 PM, in message
<47******************@efirstbank.com>,
Frank Swarbrick<Fr*************@efirstbank.comwrote:
>>Interesting! I was going to ask if such a thing existed, but I was
pretty
much convinced they did not so I didn't ask.

Looks like with version 9.5 DB2 supports global variables:

"Global variables improve data sharing between SQL statements.
Version 9.5 introduces the concept of global variables, which are named
memory variables that you can access and modify through SQL statements.
Global variables enable you to share data between different SQL
statements
running in the same session (or connection) without the need for
application
logic to support this data transfer."

The reason I had wanted something like this is for applications where a
generic userid is used for an application to connect to a database, but
>>a
specific userid is used by the user when using the application. This
way I
think we can do something like this:

CREATE VARIABLE global.userid VARCHAR(20) DEFAULT NULL^

CREATE TABLE maint_log (
userid VARCHAR(20)
, field_name VARCHAR(40) NOT NULL
, old_value VARCHAR(255)
, new_value VARCHAR(255)
)^

CREATE TRIGGER TEST.NAME_LOG_TR
AFTER UPDATE OF name, city, state
ON TEST.TABLE1
REFERENCING OLD AS old NEW AS new
FOR EACH ROW
MODE DB2SQL

That's wierd. It cut off the rest of my message and put it in some
unreadable attachment.

CREATE TRIGGER TEST.NAME_LOG_TR
AFTER UPDATE OF
NAME
, CITY
, STATE
ON TEST.TABLE1
REFERENCING OLD AS old NEW AS new
FOR EACH ROW
MODE DB2SQL

It did it again! *!#@&#@ Let's try something else...

.CREATE TRIGGER TEST.NAME_LOG_TR
. AFTER UPDATE OF
. NAME
. , CITY
. , STATE
. ON TEST.TABLE1
. REFERENCING OLD AS old NEW AS new
. FOR EACH ROW
. MODE DB2SQL
.BEGIN ATOMIC
. IF NOT old.name = new.name THEN
. INSERT INTO maint_log (userid, field_name, old_value, new_value)
. VALUES (global.userid, 'NAME', old.name, new.name);
. END IF;
. IF NOT old.city = new.city THEN
. INSERT INTO maint_log (userid, field_name, old_value, new_value)
. VALUES (global.userid, 'CITY', old.city, new.city);
. END IF;
. IF NOT old.state = new.state THEN
. INSERT INTO maint_log (userid, field_name, old_value, new_value)
. VALUES (global.userid, 'STATE', old.state, new.state);
. END IF;
.END@
That's better.

Anyway, I am interested in a way to make the IF etc statements more generic
so as to not require a new IF statement for each column. Any ideas?

Frank

Feb 25 '08 #6
Frank Swarbrick wrote:
>>>On 2/25/2008 at 12:05 PM, in message
<47******************@efirstbank.com>,
Frank Swarbrick<Fr*************@efirstbank.comwrote:
>n 2/25/2008 at 9:53 AM, in message <47******************@efirstbank.com>,
Frank Swarbrick<Fr*************@efirstbank.comwrote:
>>>>>On 2/22/2008 at 6:25 PM, in message
<47******************@efirstbank.com>,
Frank Swarbrick<Fr*************@efirstbank.comwrote:
Interesting! I was going to ask if such a thing existed, but I was
pretty
much convinced they did not so I didn't ask.

Looks like with version 9.5 DB2 supports global variables:

"Global variables improve data sharing between SQL statements.
Version 9.5 introduces the concept of global variables, which are named
memory variables that you can access and modify through SQL statements.
Global variables enable you to share data between different SQL
statements
running in the same session (or connection) without the need for
application
logic to support this data transfer."

The reason I had wanted something like this is for applications where a
generic userid is used for an application to connect to a database, but
>>>a
specific userid is used by the user when using the application. This
way I
think we can do something like this:

CREATE VARIABLE global.userid VARCHAR(20) DEFAULT NULL^

CREATE TABLE maint_log (
userid VARCHAR(20)
, field_name VARCHAR(40) NOT NULL
, old_value VARCHAR(255)
, new_value VARCHAR(255)
)^

CREATE TRIGGER TEST.NAME_LOG_TR
AFTER UPDATE OF name, city, state
ON TEST.TABLE1
REFERENCING OLD AS old NEW AS new
FOR EACH ROW
MODE DB2SQL
That's wierd. It cut off the rest of my message and put it in some
unreadable attachment.

CREATE TRIGGER TEST.NAME_LOG_TR
AFTER UPDATE OF
NAME
, CITY
, STATE
ON TEST.TABLE1
REFERENCING OLD AS old NEW AS new
FOR EACH ROW
MODE DB2SQL
It did it again! *!#@&#@ Let's try something else...

.CREATE TRIGGER TEST.NAME_LOG_TR
. AFTER UPDATE OF
. NAME
. , CITY
. , STATE
. ON TEST.TABLE1
. REFERENCING OLD AS old NEW AS new
. FOR EACH ROW
. MODE DB2SQL
.BEGIN ATOMIC
. IF NOT old.name = new.name THEN
. INSERT INTO maint_log (userid, field_name, old_value, new_value)
. VALUES (global.userid, 'NAME', old.name, new.name);
. END IF;
. IF NOT old.city = new.city THEN
. INSERT INTO maint_log (userid, field_name, old_value, new_value)
. VALUES (global.userid, 'CITY', old.city, new.city);
. END IF;
. IF NOT old.state = new.state THEN
. INSERT INTO maint_log (userid, field_name, old_value, new_value)
. VALUES (global.userid, 'STATE', old.state, new.state);
. END IF;
.END@

That's better.

Anyway, I am interested in a way to make the IF etc statements more generic
so as to not require a new IF statement for each column. Any ideas?

Frank
INSERT INTO .. SELECT ..FROM (VALUES ('NAME', old.name, new.name),
(...), ...) AS X(col, oldval, newval) WHERE oldval <newval

Cheers
Serge
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Feb 25 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Matt | last post by:
Greetings, What are people's thoughts on global variables in C++? Why are we taught not to use them in programming? Is it true that if you are running two copies of the C program one copy can...
4
by: Andrew V. Romero | last post by:
I have been working on a function which makes it easier for me to pull variables from the URL. So far I have: <script language="JavaScript"> var variablesInUrl; var vArray = new Array(); ...
12
by: David WOO | last post by:
Hi, I am a newbie on C++, I need to define some global variables which should be accessible to most classes. In the mean time, I don't won't the global variables be modified freely at most of...
2
by: Bryan Parkoff | last post by:
….I would like to know which is the best optimization to use global variable or global struct. I always tell C/C++ Compiler to turn on optimization. ….I use underscore between first name and...
17
by: MLH | last post by:
A97 Topic: If there is a way to preserve the values assigned to global variables when an untrapped runtime error occurs? I don't think there is, but I thought I'd ask. During development, I'm...
33
by: MLH | last post by:
I've read some posts indicating that having tons of GV's in an Access app is a bad idea. Personally, I love GVs and I use them (possibly abuse them) all the time for everything imaginable - have...
9
by: CDMAPoster | last post by:
About a year ago there was a thread about the use of global variables in A97: http://groups.google.com/group/comp.databases.ms-access/browse_frm/thread/fedc837a5aeb6157 Best Practices by Kang...
5
by: Sandman | last post by:
I dont think I understand them. I've read the section on scope in the manual inside out. I'm running PHP 5.2.0 Here is the code I'm working on: //include_me.php <?php $MYVAR = array(); global...
1
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have...
112
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.