IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

How can we retrive first row in a table using SQL query


IBM Mainframe Forums -> DB2
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
Laxminarsimharao

New User


Joined: 08 May 2007
Posts: 26
Location: hyderabad

PostPosted: Wed Nov 07, 2007 2:09 pm
Reply with quote

How can we retrive first row in a table using SQL query ?
i.e. top row in a table.
Back to top
View user's profile Send private message
murmohk1

Senior Member


Joined: 29 Jun 2006
Posts: 1436
Location: Bangalore,India

PostPosted: Wed Nov 07, 2007 2:32 pm
Reply with quote

Narasimha,

Discussed earlier. Follow the link -

ibmmainframes.com/viewtopic.php?t=7394&highlight=fetch
Back to top
View user's profile Send private message
vasanthkumarhb

Active User


Joined: 06 Sep 2007
Posts: 275
Location: Bang,iflex

PostPosted: Wed Nov 07, 2007 2:42 pm
Reply with quote

Hi,

Please try this Query below and let me know



Code:
 SELECT * FROM TABLE FETCH FIRST 1 ROWS ONLY;




Regard's
Vasanth......... icon_smile.gif

"Never Give Up Until U Succeed"
Back to top
View user's profile Send private message
Laxminarsimharao

New User


Joined: 08 May 2007
Posts: 26
Location: hyderabad

PostPosted: Wed Nov 07, 2007 2:44 pm
Reply with quote

Thanks it's working.
Back to top
View user's profile Send private message
venkatm99

New User


Joined: 01 Jun 2006
Posts: 23
Location: Sinagpore

PostPosted: Wed Nov 07, 2007 3:11 pm
Reply with quote

HI

Code:
SELECT * FROM TABLE FETCH FIRST 1 ROWS ONLY;


Can the above query works in the program as singleton select ??
or
Cursor is needed?

cheers
venkat
Back to top
View user's profile Send private message
Laxminarsimharao

New User


Joined: 08 May 2007
Posts: 26
Location: hyderabad

PostPosted: Wed Nov 07, 2007 3:13 pm
Reply with quote

Cursor is required for that one.
Back to top
View user's profile Send private message
guptae

Moderator


Joined: 14 Oct 2005
Posts: 1208
Location: Bangalore,India

PostPosted: Wed Nov 07, 2007 3:33 pm
Reply with quote

Hi Laxminarsimharao,

Would you please tell me why u r require CURSOR when u r fetching single row only.

Please go throgh the manual

Venkat,

No cursor is not required becoz that query always resultant a single row.
Back to top
View user's profile Send private message
murmohk1

Senior Member


Joined: 29 Jun 2006
Posts: 1436
Location: Bangalore,India

PostPosted: Wed Nov 07, 2007 3:45 pm
Reply with quote

In addition to Ekta's post -

You can always DECLARE cursor to make the program bit complex (you know show off).
Back to top
View user's profile Send private message
revel

Active User


Joined: 05 Apr 2005
Posts: 135
Location: Bangalore/Chennai-INDIA

PostPosted: Wed Nov 07, 2007 4:06 pm
Reply with quote

Hi Murali,

What did you mean?

Could you please elaborate...
Back to top
View user's profile Send private message
vasanthkumarhb

Active User


Joined: 06 Sep 2007
Posts: 275
Location: Bang,iflex

PostPosted: Wed Nov 07, 2007 4:14 pm
Reply with quote

Murali,


Then, why cursor is needed could you pls clear your answer?

__________________
Regard's
Vasanth........ icon_smile.gif

"Never Give Up Until U Succeed".
Back to top
View user's profile Send private message
Bitneuker

CICS Moderator


Joined: 07 Nov 2005
Posts: 1104
Location: The Netherlands at Hole 19

PostPosted: Wed Nov 07, 2007 4:23 pm
Reply with quote

murmohk1 wrote:
In addition to Ekta's post -

You can always DECLARE cursor to make the program bit complex (you know show off).


We should not post like this; it confuses icon_cry.gif
Back to top
View user's profile Send private message
Bitneuker

CICS Moderator


Joined: 07 Nov 2005
Posts: 1104
Location: The Netherlands at Hole 19

PostPosted: Wed Nov 07, 2007 4:25 pm
Reply with quote

The requirement has been answered and works. Read the answers.
Back to top
View user's profile Send private message
vasanthkumarhb

Active User


Joined: 06 Sep 2007
Posts: 275
Location: Bang,iflex

PostPosted: Wed Nov 07, 2007 4:49 pm
Reply with quote

George,

Link provided by you are helpfull, thank you for the links.

__________________
Regard's
Vasanth.......... icon_smile.gif


"Never Give Up Untill U Succeed"
Back to top
View user's profile Send private message
Anuj Dhawan

Superior Member


Joined: 22 Apr 2006
Posts: 6250
Location: Mumbai, India

PostPosted: Wed Nov 07, 2007 6:39 pm
Reply with quote

vasanthkumarhb wrote:
Code:
 SELECT * FROM TABLE FETCH FIRST 1 ROWS ONLY;


Without ORDER BY clause this query is bit garbage, it'll select just any row..how do you decide what is & first- what is last-line ?
Back to top
View user's profile Send private message
enrico-sorichetti

Superior Member


Joined: 14 Mar 2007
Posts: 10873
Location: italy

PostPosted: Wed Nov 07, 2007 6:55 pm
Reply with quote

Quote:
Without ORDER BY clause this query is bit garbage, it'll select just any row..how do you decide what is & first- what is last-line ?


that' something that makes me wonder on the real quality of application developed by some sites

SO MANY DATA SETS WITH RECORD POSITION DEPENDENCY

.. I need to extract the first record
.. I need the last record
..
.. copy everything except the 75th record
.. add more of this kind

it makes me think that' s mostly homework, or as an alternative
the worst designed applications in ... choose a big enough domain
Back to top
View user's profile Send private message
Bitneuker

CICS Moderator


Joined: 07 Nov 2005
Posts: 1104
Location: The Netherlands at Hole 19

PostPosted: Wed Nov 07, 2007 7:26 pm
Reply with quote

Laxminarsimharao wrote:
How can we retrive first row in a table using SQL query ?
i.e. top row in a table.


is his requirement.......



Quote:
Posted: Wed Nov 07, 2007 2:14 pm Post subject:

--------------------------------------------------------------------------------

Thanks it's working.


is his reaction after implementing the solution.

I wonder why the topic is still discussed icon_eek.gif
Back to top
View user's profile Send private message
stodolas

Active Member


Joined: 13 Jun 2007
Posts: 632
Location: Wisconsin

PostPosted: Wed Nov 07, 2007 11:27 pm
Reply with quote

Because the solution will provide inconsistent results unless an order by is specified.
Back to top
View user's profile Send private message
vasanthkumarhb

Active User


Joined: 06 Sep 2007
Posts: 275
Location: Bang,iflex

PostPosted: Tue Nov 13, 2007 8:55 am
Reply with quote

Hi Anuj,



Try this query, it works good for you too, the query syntax is correct pls check it and let me know.

Regard's
Vasanth......... icon_smile.gif
Back to top
View user's profile Send private message
dick scherrer

Moderator Emeritus


Joined: 23 Nov 2006
Posts: 19244
Location: Inside the Matrix

PostPosted: Tue Nov 13, 2007 8:36 pm
Reply with quote

Hello,

Quote:
Try this query, it works good for you too, the query syntax is correct pls check it and let me know
I see no query to try. . .

Keep in mind there is no such thing as a "first row". . . The posted query will return a row, but there is nothing "first" about it. . . It is just a row, like all of the others, unless some order is specified.
Back to top
View user's profile Send private message
Anuj Dhawan

Superior Member


Joined: 22 Apr 2006
Posts: 6250
Location: Mumbai, India

PostPosted: Thu Nov 29, 2007 9:46 pm
Reply with quote

Hi Vasanth,

Ah...sorry to join the party bit late..well I hope you got enough suggestion, one more from my side... icon_smile.gif

Try the same query you are suggesting..with ORDER BY clause & without it..do you see some difference in the results?
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> DB2

 


Similar Topics
Topic Forum Replies
No new posts Compare 2 files and retrive records f... DFSORT/ICETOOL 0
No new posts Load new table with Old unload - DB2 DB2 6
No new posts Pulling a fixed number of records fro... DB2 2
No new posts RC query -Time column CA Products 3
No new posts Multiple table unload using INZUTILB DB2 2
Search our Forums:

Back to Top