Delete First Rows in DB2 (Mainframe z/OS)

Discuss other programming languages besides AutoHotkey
User avatar
jethrow
Posts: 188
Joined: 30 Sep 2013, 19:52
Location: Iowa

Delete First Rows in DB2 (Mainframe z/OS)

Post by jethrow » 12 Mar 2014, 16:35

Anyone happen to know how to delete the first n rows from a table in Mainframe DB2? By first, I mean the way you can utilize fetch first n rows in a select statement. The order doesn't actually matter - I'm just trying to limit the result set so I can generically create a commit limit for deleting. I'd rather not find the key & try to create a condition to return a given n rows because I'd like this to be applied to several tables, and not become too complex. The following works of distributed DB2, which is quite frustrating:

Code: Select all

delete from
(select * from <TABLE> fetch first 2 rows only)
Executing this in Mainframe DB2 gives the folloiwng:
Error: SQL0104N An unexpected token "(" was found following "(" ...

User avatar
joedf
Posts: 8981
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

Re: Delete First Rows in DB2 (Mainframe z/OS)

Post by joedf » 14 Mar 2014, 19:53

Code: Select all

DELETE FROM table_name
WHERE some_column=some_value;
????
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]

User avatar
jethrow
Posts: 188
Joined: 30 Sep 2013, 19:52
Location: Iowa

Re: Delete First Rows in DB2 (Mainframe z/OS)

Post by jethrow » 17 Mar 2014, 09:48

Thanks for the input, but that will not satisfy my request. This is an automated process in production, so I cannot just execute a delete statement as such - since there could be millions+ of deletes - which could bog down the server. That's why I'm attempting to create a commit threshold.

The following works:

Code: Select all

delete from <TABLE_NAME>
where (<KEY>) in (
        select <KEY> from <TABLE_NAME>
        where <CONDITION>
        FETCH first <N> rows only     )
The problem with this, however, is there is a limit not only on the IN-statement, but there's also a Lock limit. Again, my goal is to create a generic SQL statement to consolidate code for a purge process on multiple tables.

User avatar
joedf
Posts: 8981
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

Re: Delete First Rows in DB2 (Mainframe z/OS)

Post by joedf » 17 Mar 2014, 18:50

hmmm im no expert in SQL, but have you tried StackOverflow? they answer quickly, and im sure youll find your answer there... ;)
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]

User avatar
jethrow
Posts: 188
Joined: 30 Sep 2013, 19:52
Location: Iowa

Re: Delete First Rows in DB2 (Mainframe z/OS)

Post by jethrow » 12 Mar 2015, 17:04

So, I was just provided this SQL today, though I'm not completely sure it's fool-proof:

Code: Select all

DELETE FROM <TABLE_NAME>
WHERE rid(<TABLE_NAME>) <= (SELECT max(rid) FROM (
						        SELECT rid(<TABLE_NAME>) AS rid
						        FROM <TABLE_NAME>
						        ORDER BY rid(<TABLE_NAME>)
						        FETCH FIRST <N> ROWS ONLY) i)

Post Reply

Return to “Other Programming Languages”