MVSFORUMS.com Forum Index MVSFORUMS.com
A Community of and for MVS Professionals
 
 FAQFAQ   SearchSearch   Quick Manuals   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

INSERT with NESTED SELECT, for Pagination Logic

 
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database
View previous topic :: View next topic  
Author Message
hellohatim
Beginner


Joined: 12 Apr 2007
Posts: 28
Topics: 7

PostPosted: Mon Dec 22, 2008 9:56 am    Post subject: INSERT with NESTED SELECT, for Pagination Logic Reply with quote

Hi All,

I am working on DB2 (ver 8 I guess!) z/OS Stored Procedures and had a requirement to insert data from one table to another. I want to select the last ten rows of the query and insert it in the table. I tried giving a nested SELECT query inside INSERT SQL statement but it does not accept "FETCH FIRST 10 ROWS ONLY". As of now I have to fetch all the 10 rows in Working Storage variables and then insert them seperately into the table.

The insert statement I tried was something like this...

Code:
INSERT INTO SESSION.TEMP (A, B, C, D)
SELECT A, B, C, D
FROM   P72.PARTNER_MERCHANT
WHERE  ...


This does not work if I add "ORDER BY A DESC FETCH FIRST 10 ROWS ONLY" to the nested SELECT Query. The SELECT query with FETCH FIRST 10 works well standalone. Believe there is no such thing as ROW_NUMBER() as well to help me achieve this.

- Is there a way to achieve the desired result in a single SQL query?

- In larger context, this piece of code is for pagination logic for retrieving search results to be displayed on the web page. Currently we are populating a global temporary table with 10 records each (which is the size of the page). A cursor to this table with SELECT * is openened and the same is then passed on as a result set (to Java front end). Every time the user moves to the next page, the query is re-executed and then table is dropped and refilled with the new contents. Is there a better way to achieve such pagination logic (either on the SP or Java side)? I read about scrollable cursors, but as of now I am not aware about its exact working. The search results can run into thousands of rows ... efficiency & speed is also a factor we would need to consider.

Thanks & Regards,
Hatim
_________________
-Hatim M P
Back to top
View user's profile Send private message MSN Messenger
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12394
Topics: 75
Location: San Jose

PostPosted: Mon Dec 22, 2008 11:00 am    Post subject: Re: INSERT with NESTED SELECT, for Pagination Logic Reply with quote

hellohatim wrote:
This does not work if I add "ORDER BY A DESC FETCH FIRST 10 ROWS ONLY" to the nested SELECT Query. ,
Hatim


Hatim,

AFAIK this should work fine

Code:

       SELECT A, B, C, D
         FROM   P72.PARTNER_MERCHANT
        WHERE  ...
        ORDER BY A DESC           
        FETCH FIRST 10 ROWS ONLY 
Back to top
View user's profile Send private message Send e-mail Visit poster's website
hellohatim
Beginner


Joined: 12 Apr 2007
Posts: 28
Topics: 7

PostPosted: Mon Dec 22, 2008 11:10 am    Post subject: Reply with quote

Hi Kolusu,

Yes, the select query (as mentioned by you above) works fine on its own, but when I use it in the INSERT SQL statement, it does not.

As I said the requirement is to selectively insert rows from one table to another. Currently I have to do this one by one, involves multiple DB2 call, 10 for FETCH and 10 for INSERT.

Please do guide me if possible on my second question as well!

Thanks!
Hatim
_________________
-Hatim M P
Back to top
View user's profile Send private message MSN Messenger
Nic Clouston
Advanced


Joined: 01 Feb 2007
Posts: 1075
Topics: 7
Location: At Home

PostPosted: Mon Dec 22, 2008 1:27 pm    Post subject: Reply with quote

INSERT is a completely different statement from SELECT - it is not a part of SELECT so of course you have to do it seperately.
_________________
Utility and Program control cards are NOT, repeat NOT, JCL.
Back to top
View user's profile Send private message
hellohatim
Beginner


Joined: 12 Apr 2007
Posts: 28
Topics: 7

PostPosted: Mon Dec 22, 2008 8:47 pm    Post subject: Reply with quote

Thanks Nic!
_________________
-Hatim M P
Back to top
View user's profile Send private message MSN Messenger
Dibakar
Advanced


Joined: 02 Dec 2002
Posts: 702
Topics: 64
Location: USA

PostPosted: Mon Dec 29, 2008 5:29 pm    Post subject: Reply with quote

Hatim what error are you getting? I tried the following code (without the where clause) and it worked -

Code:

insert into zschools
  (select * from schools order by zip fetch first 2 rows only)


Nic,

I didn't understand what you mean when you say -

Quote:
INSERT is a completely different statement from SELECT


Diba
Back to top
View user's profile Send private message Send e-mail
Nic Clouston
Advanced


Joined: 01 Feb 2007
Posts: 1075
Topics: 7
Location: At Home

PostPosted: Tue Dec 30, 2008 4:06 am    Post subject: Reply with quote

Diba

Do not worry - I got a little confused about the original code. I do not think it was the festivities, just plain confusion.

Still although you CAN, as you show, use a SELECT clause to get the data for insertion the SELECT statement is an SQL keyword on the same level as INSERT and is not a sub-keyword of INSERT even though it looks like it is.

I hope you understand what I am saying!
_________________
Utility and Program control cards are NOT, repeat NOT, JCL.
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12394
Topics: 75
Location: San Jose

PostPosted: Tue Dec 30, 2008 2:31 pm    Post subject: Reply with quote

Dibakar,

Insert with fetch first n rows only is NOT a valid syntax on Z/os DB2 yet

Kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Dibakar
Advanced


Joined: 02 Dec 2002
Posts: 702
Topics: 64
Location: USA

PostPosted: Wed Dec 31, 2008 1:17 am    Post subject: Reply with quote

Kolusu,

You are right. I don't have access to DB2 on Z/OS so I ran the query on Windows version. I should have clarified it first.

Diba.
Back to top
View user's profile Send private message Send e-mail
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12394
Topics: 75
Location: San Jose

PostPosted: Tue Jan 06, 2009 8:27 pm    Post subject: Reply with quote

hellohatim,

I just stumbled upon a cheat for this . Try this and let me know if it works. Assume that the WHERE condition on table P72.PARTNER_MERCHANT has fetched 1000 rows we would insert them all in to the session.temp, however since you only want to see the first 10 rows we use the fetch first 10 rows only on final table after insert. This would eliminate the 2 step process of inserting and then performing a select. This logic would help you to perform the page logic also. This works on Db2 V8


Code:

SELECT * FROM FINAL TABLE                     
        (INSERT INTO SESSION.TEMP (A, B, C, D)
                    SELECT A, B, C, D IMESTAMP
                      FROM P72.PARTNER_MERCHANT
                     WHERE .....)
 ORDER BY A DESC                               
 FETCH FIRST 10 ROWS ONLY                       
  ;                               


Kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
hellohatim
Beginner


Joined: 12 Apr 2007
Posts: 28
Topics: 7

PostPosted: Sun Jan 11, 2009 2:47 am    Post subject: Reply with quote

Hi Kolusu!

Thanks a lot Very Happy , I will try this tomorrow and let you know the updates soon.

Thanks & Regards,
_________________
-Hatim M P
Back to top
View user's profile Send private message MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


MVSFORUMS
Powered by phpBB © 2001, 2005 phpBB Group