Portal | Manuals | References | Downloads | Info | Programs | JCLs | Master the Mainframes
IBM Mainframe Computers Forums Index
 
Register
 
IBM Mainframe Computers Forums Index Mainframe: Search IBM Mainframe Forum: FAQ Memberlist Usergroups Profile Log in to check your private messages Log in
 

 

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

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> DB2
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    Post subject: How can we retrive first row in a table using SQL query
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: 1439
Location: Bangalore,India

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

Narasimha,

Discussed earlier. Follow the link -

http://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: 276
Location: Bang,iflex

PostPosted: Wed Nov 07, 2007 2:42 pm    Post subject:
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    Post subject:
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    Post subject:
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    Post subject:
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: 1187
Location: Bangalore,India

PostPosted: Wed Nov 07, 2007 3:33 pm    Post subject:
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: 1439
Location: Bangalore,India

PostPosted: Wed Nov 07, 2007 3:45 pm    Post subject:
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    Post subject:
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: 276
Location: Bang,iflex

PostPosted: Wed Nov 07, 2007 4:14 pm    Post subject:
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: 1105
Location: The Netherlands at Hole 19

PostPosted: Wed Nov 07, 2007 4:23 pm    Post subject:
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: 1105
Location: The Netherlands at Hole 19

PostPosted: Wed Nov 07, 2007 4:25 pm    Post subject:
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: 276
Location: Bang,iflex

PostPosted: Wed Nov 07, 2007 4:49 pm    Post subject:
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

Senior Member


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

PostPosted: Wed Nov 07, 2007 6:39 pm    Post subject:
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

Global Moderator


Joined: 14 Mar 2007
Posts: 10203
Location: italy

PostPosted: Wed Nov 07, 2007 6:55 pm    Post subject: Reply to: How can we retrive first row in a table using SQL
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: 1105
Location: The Netherlands at Hole 19

PostPosted: Wed Nov 07, 2007 7:26 pm    Post subject: Re: How can we retrive first row in a table using SQL query
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    Post subject:
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: 276
Location: Bang,iflex

PostPosted: Tue Nov 13, 2007 8:55 am    Post subject:
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

Site Director


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

PostPosted: Tue Nov 13, 2007 8:36 pm    Post subject:
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

Senior Member


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

PostPosted: Thu Nov 29, 2007 9:46 pm    Post subject:
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    IBMMAINFRAMES.com Support Forums -> DB2 All times are GMT + 6 Hours
Page 1 of 1

 

Search our Forum:

Similar Topics
Topic Author Forum Replies Posted
This topic is locked: you cannot edit posts or make replies. Db2 SQL query to convert rows into co... subratarec DB2 5 Thu Nov 17, 2016 4:51 pm
No new posts Two Selects in a single select query Rohit Umarjikar DB2 1 Fri Nov 04, 2016 8:46 pm
No new posts Problem in Running Query via JCL vickey_dw DB2 3 Tue Oct 18, 2016 11:11 pm
No new posts Need Rexx Program to Load in to DB2 T... Hareesh Verma CLIST & REXX 22 Tue Sep 20, 2016 2:59 pm
No new posts SQL Query optimization. arunsoods DB2 12 Sun Sep 04, 2016 6:27 am


Facebook
Back to Top
 
Mainframe Wiki | Forum Rules | Bookmarks | Subscriptions | FAQ | Tutorials | Contact Us