If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > How to delete Third row in a table.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-02-07, 03:17
vini_srcna vini_srcna is offline
Registered User
 
Join Date: May 2006
Posts: 82
How to delete Third row in a table.

Hi folks,

I have been posted with a tough question, HOW TO DELTE THIRD ROW in a table. Ofcourse this is not related to my work or project. Its just a challenging question. I need a SQL query to delete 3 rd row of a table if there is one. I know the data may be stored randomly and deleting the single row with out any keys in the where predicate may sound strange.

First if we run the select * from table name, we will see the rows.
If we run the delete query to delete 3rd row and again run the select * FROM table name, we should not see the 3rd row. I have no information other than the tablename. Any suggestions would be helpful. Thanks in advance
Reply With Quote
  #2 (permalink)  
Old 03-02-07, 04:15
grofaty grofaty is offline
Registered User
 
Join Date: Jan 2003
Posts: 1,570
Hi,

Table db2admin.tab:
Code:
ID  Name
1   AAA
2   BBB
3   CCC
4   DDD
SQL (delete third row from table):
Code:
DELETE FROM DB2ADMIN.TAB WHERE ID = 
(SELECT ID FROM 
    (
    SELECT ROW_NUMBER () OVER () AS NUMBER, ID FROM DB2ADMIN.TAB
    )
 AS TEMP WHERE NUMBER = 3
)
First of all you need to know this is dangerous operation, because you don't know which data will be deleted. It can help if order by statement is used in select, but you never know what will be deleted. It should be used primary key in delete where condition where ever possible.

Hope this helps,
Grofaty

Last edited by grofaty; 03-02-07 at 04:25.
Reply With Quote
  #3 (permalink)  
Old 03-02-07, 06:43
vini_srcna vini_srcna is offline
Registered User
 
Join Date: May 2006
Posts: 82
Hi Grofaty,

Thank you very much. I agree with your suggestion. We are no where using this query however this was a challengin question to me.

I forgot to inform that i need this query for OS/390 or Z/OS. I believe ROW_NUMBER () OVER () are the function on LUW. I did tried this on Z/OS and it didnt work for me. Would there be any other way. Thank you.
Please help

Thanks,
Vinay
Reply With Quote
  #4 (permalink)  
Old 03-02-07, 07:48
grofaty grofaty is offline
Registered User
 
Join Date: Jan 2003
Posts: 1,570
Hi,
I don't have db2 for os/390 or z/OS to test. But if I remember correctly this LUW functions should work in db2 v8 for zOS. Not sure...

Ok, try this out, don't know it is working on zOS:
Code:
DELETE FROM DB2ADMIN.TAB WHERE ID = (
SELECT ID FROM (
SELECT ID FROM DB2ADMIN.TAB FETCH FIRST 3 ROWS ONLY
) AS TEMP
EXCEPT
SELECT ID FROM (
SELECT ID FROM DB2ADMIN.TAB FETCH FIRST 2 ROWS ONLY
) AS TEMP2
)
Hope this helps,
Grofaty
Reply With Quote
  #5 (permalink)  
Old 03-02-07, 08:18
vini_srcna vini_srcna is offline
Registered User
 
Join Date: May 2006
Posts: 82
Hi, Thanks again.

It didnt work for me. It says error at FETCH. I believe we cannot use FETCH clause in sub queries.(Not sure though).
Reply With Quote
  #6 (permalink)  
Old 03-02-07, 08:36
urquel urquel is offline
Registered User
 
Join Date: Aug 2004
Posts: 330
Are you trying to delete the row with the 3rd lowest value of the ID column?
Reply With Quote
  #7 (permalink)  
Old 03-02-07, 09:06
Peter.Vanroose Peter.Vanroose is offline
Registered User
 
Join Date: Sep 2004
Location: Belgium
Posts: 1,079
I would suggest the following:
Code:
DECLARE c CURSOR FOR
   SELECT 1 FROM tbl ORDER BY ... 
   OPTIMIZE FOR 3 ROWS
   FOR UPDATE ;
OPEN c ;
FETCH c ;
FETCH c ;
FETCH c ;
DELETE FROM tbl WHERE CURRENT OF c ;
__________________
--_Peter Vanroose,
__IBM Certified Database Administrator, DB2 9 for z/OS
__IBM Certified Application Developer
__ABIS Training and Consulting
__http://www.abis.be/

Last edited by Peter.Vanroose; 03-02-07 at 09:13.
Reply With Quote
  #8 (permalink)  
Old 03-02-07, 09:10
Peter.Vanroose Peter.Vanroose is offline
Registered User
 
Join Date: Sep 2004
Location: Belgium
Posts: 1,079
Quote:
Originally Posted by grofaty
I don't have db2 for os/390 or z/OS to test. But if I remember correctly this LUW functions should work in db2 v8 for zOS.
No, these functions were not added to DB2 v8 for z/OS.
Hopefully they are in DB2 9 (to be out very soon ...)
__________________
--_Peter Vanroose,
__IBM Certified Database Administrator, DB2 9 for z/OS
__IBM Certified Application Developer
__ABIS Training and Consulting
__http://www.abis.be/

Last edited by Peter.Vanroose; 03-02-07 at 17:45.
Reply With Quote
  #9 (permalink)  
Old 03-02-07, 09:47
sathyaram_s sathyaram_s is offline
Super Moderator
 
Join Date: Aug 2001
Location: UK
Posts: 4,534
Peter, Yes, ROW_NUMBER, RANK etc are planned for V9.

Sathyaram

Quote:
Originally Posted by Peter.Vanroose
No, these functions were not been added to DB2 v8 for z/OS.
Hopefully they are in DB2 9 (to be out very soon ...)
__________________
Visit the new-look IDUG Website , register to gain access to the excellent content.
Reply With Quote
  #10 (permalink)  
Old 03-02-07, 17:57
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
Quote:
Originally Posted by vini_srcna
Hi folks,

I have been posted with a tough question, HOW TO DELTE THIRD ROW in a table. Ofcourse this is not related to my work or project. Its just a challenging question. I need a SQL query to delete 3 rd row of a table if there is one. I know the data may be stored randomly and deleting the single row with out any keys in the where predicate may sound strange.

First if we run the select * from table name, we will see the rows.
If we run the delete query to delete 3rd row and again run the select * FROM table name, we should not see the 3rd row. I have no information other than the tablename. Any suggestions would be helpful. Thanks in advance
Your question doesn't make any sense. Tables are comprised of a set of columns and contain a set of rows. Sets are - per definition - not ordered. Therefore, no one knows what the "3rd" row will be unless you specify an explicit ORDER BY in your query. Even the same query could return the rows in different order when executed twice - and that would be absolutely correct.

So you can delete any arbitrary row of your table. Just make sure there are more than 2 rows. You can always find an ORDER BY which happens to place this row in the 3rd position.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #11 (permalink)  
Old 03-05-07, 03:18
vini_srcna vini_srcna is offline
Registered User
 
Join Date: May 2006
Posts: 82
I agree stolze. This is a stupid question. However i just tried to find a stupid solution -. I agree we never know which row is deleted and am sure no one uses this kind of logic.
Reply With Quote
  #12 (permalink)  
Old 03-05-07, 04:41
grofaty grofaty is offline
Registered User
 
Join Date: Jan 2003
Posts: 1,570
Quote:
Originally Posted by vini_srcna
This is a stupid question. However i just tried to find a stupid solution -
Hi,
I think stolze has the best solution to solve the problem: "Don't do it."
Grofaty
Reply With Quote
  #13 (permalink)  
Old 03-05-07, 05:20
vini_srcna vini_srcna is offline
Registered User
 
Join Date: May 2006
Posts: 82
I was expecting your response Grofaty..-..!
Reply With Quote
  #14 (permalink)  
Old 03-05-07, 06:05
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
it's not a stupid question

it's a homework assignment
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #15 (permalink)  
Old 03-05-07, 06:18
grofaty grofaty is offline
Registered User
 
Join Date: Jan 2003
Posts: 1,570
Quote:
Originally Posted by vini_srcna
I was expecting your response Grofaty
There is no such thing as stupid question, there are just stupid people, who are afraid of asking.

One more advice... Don't migrate to db2 v9. This is the best solution to protect yourself against complex problems. You know: "Simple systems, simple problems. Complex systems complex problems."
Grofaty
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On