| View previous topic :: View next topic |
| Author |
Message |
hellohatim Beginner
Joined: 12 Apr 2007 Posts: 28 Topics: 7
|
Posted: Mon Dec 22, 2008 9:56 am Post subject: INSERT with NESTED SELECT, for Pagination Logic |
|
|
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 |
|
 |
kolusu Site Admin

Joined: 26 Nov 2002 Posts: 12394 Topics: 75 Location: San Jose
|
Posted: Mon Dec 22, 2008 11:00 am Post subject: Re: INSERT with NESTED SELECT, for Pagination Logic |
|
|
| 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 |
|
 |
hellohatim Beginner
Joined: 12 Apr 2007 Posts: 28 Topics: 7
|
Posted: Mon Dec 22, 2008 11:10 am Post subject: |
|
|
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 |
|
 |
Nic Clouston Advanced
Joined: 01 Feb 2007 Posts: 1075 Topics: 7 Location: At Home
|
Posted: Mon Dec 22, 2008 1:27 pm Post subject: |
|
|
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 |
|
 |
hellohatim Beginner
Joined: 12 Apr 2007 Posts: 28 Topics: 7
|
Posted: Mon Dec 22, 2008 8:47 pm Post subject: |
|
|
Thanks Nic! _________________ -Hatim M P |
|
| Back to top |
|
 |
Dibakar Advanced

Joined: 02 Dec 2002 Posts: 702 Topics: 64 Location: USA
|
Posted: Mon Dec 29, 2008 5:29 pm Post subject: |
|
|
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 |
|
 |
Nic Clouston Advanced
Joined: 01 Feb 2007 Posts: 1075 Topics: 7 Location: At Home
|
Posted: Tue Dec 30, 2008 4:06 am Post subject: |
|
|
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 |
|
 |
kolusu Site Admin

Joined: 26 Nov 2002 Posts: 12394 Topics: 75 Location: San Jose
|
Posted: Tue Dec 30, 2008 2:31 pm Post subject: |
|
|
Dibakar,
Insert with fetch first n rows only is NOT a valid syntax on Z/os DB2 yet
Kolusu |
|
| Back to top |
|
 |
Dibakar Advanced

Joined: 02 Dec 2002 Posts: 702 Topics: 64 Location: USA
|
Posted: Wed Dec 31, 2008 1:17 am Post subject: |
|
|
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 |
|
 |
kolusu Site Admin

Joined: 26 Nov 2002 Posts: 12394 Topics: 75 Location: San Jose
|
Posted: Tue Jan 06, 2009 8:27 pm Post subject: |
|
|
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 |
|
 |
hellohatim Beginner
Joined: 12 Apr 2007 Posts: 28 Topics: 7
|
Posted: Sun Jan 11, 2009 2:47 am Post subject: |
|
|
Hi Kolusu!
Thanks a lot , I will try this tomorrow and let you know the updates soon.
Thanks & Regards, _________________ -Hatim M P |
|
| Back to top |
|
 |
|
|
|