473,396 Members | 2,010 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,396 software developers and data experts.

Bug (?) in SQL: Global temporary table gets truncated on failed insert.

Hello.

During some experimentation with DB2 decimal numbers representations, I
have discovered very unexpected behaviour of the global temporary
table. Consider this example.
Such I had executed the example: # db2 -td@ -f FILENAME.

When an single Insert into table fails, the table gets empty. Could
anybody shed some light on this ?

----( code )----------------------
DECLARE GLOBAL TEMPORARY TABLE #r
(
res decimal(31) not null
)
ON COMMIT PRESERVE ROWS
NOT LOGGED
@
insert into session.#r values( 1 ) @
insert into session.#r values( 2 ) @
insert into session.#r values( 3 ) @

select * from session.#r
-- here is expected result: 3 rows with consecutive values : 1,2,3
@
BEGIN ATOMIC
declare m_fact decimal(15) default 1;

FOR X AS
select
m_fact * 9999999999999999999999999999999. +
..0000000000000000000000000000001 as val
from sysibm.sysdummy1
DO
-- next line FIRES THE ERROR: SQL0802N
insert into session.#r values( X.val );
END FOR;
END
@
select * from session.#r
-- HERE the table "session.#r" is empty: 0 rows returned. WHY ?
@

Feb 16 '06 #1
7 2282
Sorry, I forgot to mention my system:
IBM DB2, Enterprise Server Edition, 8.1.6 @ MS Windows Server 2003 @
Intel x32.

Feb 16 '06 #2
kanda wrote:
Sorry, I forgot to mention my system:
IBM DB2, Enterprise Server Edition, 8.1.6 @ MS Windows Server 2003 @
Intel x32.

This is working as designed for NOT LOGGED temporary tables.
http://publib.boulder.ibm.com/infoce.../v8//index.jsp
"When a rollback operation terminates a unit of work or a savepoint in
P, and that unit of work or savepoint includes a modification to
SESSION.T, then if NOT LOGGED was specified, the rollback includes the
operation DELETE from SESSION.T, else the changes to T are undone."

If an UPDATE/DELETE or INSERT operation against a temp table fails in
mid flight DB2 has to roll back the SQL statement to comply with the
"Atomic" in ACID.
Since the change was not logged there is no way to comply with
Atomicity. So three choices remain:
1) drop the table (mark the table as unusable as doen for regular tables
when NOT LOGGED operations fail)
2) go to the only well defined state: emtpy
3) leave the table in whatever state it is with an unknown amount of the
changes done.

Option 2) is deemed the most appropriate.

Cheers
Serge
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Feb 16 '06 #3
Thank you very much for the skilled answer. The question is taken off,
but I eager to make some philosophic comments.
This is working as designed for NOT LOGGED temporary tables.
"When a rollback operation terminates a unit of work or a savepoint in
P, and that unit of work or savepoint includes a modification to
SESSION.T, then if NOT LOGGED was specified, the rollback includes the
operation DELETE from SESSION.T, else the changes to T are undone."
I realize now. IMHO, the biggest trouble with IBM (from the user's
point of view) it makes things much more arduous then they could be. In
this particular case it's DB2 documentation, which could be less
intricate. The other glaring case is the way for exporting data from
DB2 to a text file.

Historically, the most successful attempt of IBM to make the simple
thing hard was "Job Control Language" for the early IBM mainfraims
(like IBM/360). People worked with JCL are wincing at the mention of :(
Since the change was not logged there is no way to comply with
Atomicity. So three choices remain:
1) drop the table (mark the table as unusable as doen for regular tables
when NOT LOGGED operations fail)
2) go to the only well defined state: emtpy
3) leave the table in whatever state it is with an unknown amount of the
changes done.

Option 2) is deemed the most appropriate.


This decision could be contested. You could consider next arguments
against (2), but for (3).

* Rule of Least Surprise: always do the least surprising thing. (This
is also widely known as the Principle of Least Astonishment.) -- Eric
S.Raymond.

The disappearance of the innocent rows is very surprising thing.

* Since it's user responsibility for declaring "NOT LOGGED", don't
deprive him the freedom to recover the error himself, according his
current needs. If DB2 would implement (3) then user could drop table,
delete rows himself, or work further, if applicable.

This is another good principle: "Supply mechanism, not policy."
--
Konstantin Andreev.

Feb 17 '06 #4
Hi

I had also coded couple of procedures and had not faced any problems .
think over whether "ATOMIC" is required here, eventhough this doesnot affect your code , i belive the "ATOMIC" purpose is not required in this procedural code.

try adding this line to see whether the insert is having any issue:

-- next line FIRES THE ERROR: SQL0802N
insert into session.#r values( X.val );
GET DIAGNOSTICS v_row_count = ROW_COUNT;

this GET DIAGNOSTICS will display the no of rows it had inserted
declare a variable v_row_count as INTEGER;

include this condition also

if v_row_count < 0
then capture this using exceptions
you will know how many data was inserted or what error occured while inserting the data.

thanks
Thirumaran
Feb 17 '06 #5
Hi

I forgot to mention one more thing , i just refereed my DB2 proc simillar codes.

my table declation is
create procedure (...)
LANGUAGE SQL

Begin
DECLARE v_row_count INTEGER;

DECLARE GLOBAL TEMPORARY TABLE session."#trans_t"(
columns .....
WITH REPLACE ON COMMIT PRESERVE ROWS NOT LOGGED;

INSERT INTO session."#trans_t"( columns....);
GET DIAGNOSTICS v_row_count = ROW_COUNT;

Select * from session."#trans_t";

RETURN 0;


END
@

i am not using ATOMIC as this does not require .
This works fine for me in
IBM DB2, Enterprise Server Edition, 8.1.6 @ MS Windows Server 2003


thanks
thirumaran
Feb 17 '06 #6
Konstantin Andreev wrote:
I realize now. IMHO, the biggest trouble with IBM (from the user's
point of view) it makes things much more arduous then they could be. In
this particular case it's DB2 documentation, which could be less
intricate. There is room for improvement in this paragraph, agred.
The other glaring case is the way for exporting data from
DB2 to a text file. You are a tease! Spit it out man, what about it? :-)
Historically, the most successful attempt of IBM to make the simple
thing hard was "Job Control Language" for the early IBM mainfraims
(like IBM/360). People worked with JCL are wincing at the mention of :( Is late birth on my behalf a valid excuse?
Since the change was not logged there is no way to comply with
Atomicity. So three choices remain:
1) drop the table (mark the table as unusable as doen for regular tables
when NOT LOGGED operations fail)
2) go to the only well defined state: emtpy
3) leave the table in whatever state it is with an unknown amount of the
changes done.

Option 2) is deemed the most appropriate.

This decision could be contested. You could consider next arguments
against (2), but for (3).

* Rule of Least Surprise: always do the least surprising thing. (This
is also widely known as the Principle of Least Astonishment.) -- Eric
S.Raymond.

The disappearance of the innocent rows is very surprising thing.
* Since it's user responsibility for declaring "NOT LOGGED", don't
deprive him the freedom to recover the error himself, according his
current needs. If DB2 would implement (3) then user could drop table,
delete rows himself, or work further, if applicable.

This is another good principle: "Supply mechanism, not policy."

Just like people will wear seatbelts and helmet if you just offer them.
This is a phylosophical debate indeed. It's like C vs Java.
Do you do error checks after each SQL statement?
Most(!) developers don't. Look at any procedure migrated from TSQL.
If DB2 deletes the table content on error you are bound to find out.
If it does not and their is not error test the app can silently fail,
wrong bank transactions, reservation, accounting....
For an industrial strength DBMS this is simply not acceptable.

There is a difference between what is consumable by the masses and what
is required for the high end.

Cheers
Serge
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Feb 17 '06 #7
Serge Rielau wrote:
The other glaring case is the way for exporting data from DB2 to a text file. You are a tease! Spit it out man, what about it? :-)


Hm... I've spent some time, trying to reproduce something about it now,
but failed. Sorry, It seems I was mistaken.

[ ... skip ... ]
2) go to the only well defined state: emtpy
3) leave the table in whatever state it is with an unknown amount of the changes done.
Option 2) is deemed the most appropriate.
You could consider next arguments against (2), but for (3).

* Rule of Least Surprise: always do the least surprising thing. ...
The disappearance of the innocent rows is very surprising thing.
* Supply mechanism, not policy. ... don't deprive the freedom to recover the error himself

Do you do error checks after each SQL statement?
Most(!) developers don't. Look at any procedure migrated from TSQL.
If DB2 deletes the table content on error you are bound to find out.
If it does not and their is not error test the app can silently fail,
wrong bank transactions, reservation, accounting...


Sure, this is very strong argue. The approach that ensures less errors
is preferable. Thank you for cognitive discussion.
--
Konstantin Andreev.

Mar 3 '06 #8

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

Similar topics

4
by: Corrine | last post by:
Hi, I am creating a global temporary table that is session-specific. I insert a BLOB into this table, and then select the BLOB from this table into a ResultSet. The ResultSet sees this BLOB...
5
by: Matt | last post by:
Hello I am running a SP from the SQL Server Agent, the job has one step that looks like this. exec q_spr_inlevextsystem This job fails with the following message Job 'AutoInlev' : Step...
6
by: Bob Sparks | last post by:
Can some one post an example, or where to find one, using DBINFO in a SQL procedure? I can't seem to find one anywhere. I don't see the linkage between the structure and the SQL language variables...
8
by: Thomasb | last post by:
With a background in MS SQL Server programming I'm used to temporary tables. Have just started to work with DB2 ver 7 on z/OS and stumbled into the concept of GLOBAL TEMPORARY TABLE. I have...
7
by: Paul Reddin | last post by:
Hi, Having just upgraded one of our servers to V8.2, the CONTINUE HANDLERs in Stored procedures seem to be behaving differently? i.e Instead of continuing at the next statement (as they did...
5
by: Adam W. Saxton | last post by:
We have a few existing stored procedures which create a Global Temporary Table (##), do some work on the table and then delete the table. The issue we have is that if our Server application is...
20
by: Bryan | last post by:
hello all... im trying to add a record to an sql db on ms sql server 2000, using vb.net. seems to be working.. except for one thing, one of the columns in the database is a bit datatype, and...
7
by: rzagars | last post by:
I have just started working on DB2 which the syntax is a lot different. I am trying to create a SQL stored procedure for generating reports. I want to use temporary database which in this example...
1
ssnaik84
by: ssnaik84 | last post by:
Hi Guys, Last year I got a chance to work with R&D team, which was working on DB scripts conversion.. Though there is migration tool available, it converts only tables and constraints.. Rest of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.