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 to get auto incremented row number in a db2 query

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> DB2
View previous topic :: :: View next topic  
Author Message
mfguy2008

New User


Joined: 02 May 2008
Posts: 3
Location: Columbus

PostPosted: Fri May 02, 2008 8:29 pm    Post subject: How to get auto incremented row number in a db2 query
Reply with quote

Hello ,
Iam new to this forum.

I have a situation in one of my query.

The query looks like this
select * from employee

The result should be like
001 shaun
002 ron
003 mary

Here column shown as '001 002' are not part of my table.But I want the auto incremented number for each record in the table.

Can I get like this?

Please let me know if there is any solution for this issue.

Thanks
Back to top
View user's profile Send private message

Anuj Dhawan

Senior Member


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

PostPosted: Fri May 02, 2008 8:58 pm    Post subject:
Reply with quote

Hi,

I think it's not doable by a 'query' only.

If you are running the query through Cobol, then you can keep your own counter. If you are doing ad hoc queries, what does the a row number generated by the select do for you ? BTW, without ORDER BY, query shown is not 'that' good.

To help You further, please check this link, a similiar topic:

http://www.ibmmainframes.com/viewtopic.php?t=26832&highlight=sequence


P.S.:
Quote:
Iam new to this forum.
Well every one was new at times, not to worry, you are welcome in the world for Forums... icon_smile.gif
Back to top
View user's profile Send private message
CICS Guy

Senior Member


Joined: 18 Jul 2007
Posts: 2150
Location: At my coffee table

PostPosted: Fri May 02, 2008 9:04 pm    Post subject:
Reply with quote

Those that you are seeing are (probably) a convenience of FileAid?
Or are you refering to Row ID values?
Or are you refering to ROW_NUMBER expression?
Back to top
View user's profile Send private message
mfguy2008

New User


Joined: 02 May 2008
Posts: 3
Location: Columbus

PostPosted: Fri May 02, 2008 9:10 pm    Post subject:
Reply with quote

I want to generate a comma delimited row in a db2 table.
The first entry information should be the auto incremented number like

001,kapil,cisco,......
002,ravi,IBM,....

How to get the number like 001 and 002 using a db2 query?

Please help me on this
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: Fri May 02, 2008 9:15 pm    Post subject:
Reply with quote

Hello,

Quote:
I want to generate a comma delimited row in a db2 table.
Is this really true? I suspect that you want to read a db2 table and generate some comma delimited output.

An easy way to get what you want is to declare a cursor and as you fetch each row, increment a counter and STRING the counter plus any/all of the table fields you want delimited by commas into an output area.
Back to top
View user's profile Send private message
mfguy2008

New User


Joined: 02 May 2008
Posts: 3
Location: Columbus

PostPosted: Fri May 02, 2008 10:25 pm    Post subject:
Reply with quote

dick scherrer wrote:
Hello,

Quote:
I want to generate a comma delimited row in a db2 table.
Is this really true? I suspect that you want to read a db2 table and generate some comma delimited output.

An easy way to get what you want is to declare a cursor and as you fetch each row, increment a counter and STRING the counter plus any/all of the table fields you want delimited by commas into an output area.


can you explain how to do this with a query?
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: Fri May 02, 2008 10:42 pm    Post subject:
Reply with quote

Hello,

Quote:
can you explain how to do this with a query?
This would not be a single query that could be run via SPUFI. This would be embedded in a bit of code. You would declare the cursor, open the cursor, and then fetch rows individually, creating the csv data you want for output.
Back to top
View user's profile Send private message
Anuj Dhawan

Senior Member


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

PostPosted: Sat May 03, 2008 12:12 am    Post subject:
Reply with quote

Hi,

Your mission is little programatic, as suggested
Quote:
This would not be a single query that could be run via SPUFI.

alternatevly you could 'unload' the table & can write a sort-card in a single JOB for that. Might be there is way around but I've never used or come across.
Back to top
View user's profile Send private message
the_gautam

Active User


Joined: 05 Jun 2005
Posts: 165
Location: Bangalore

PostPosted: Sat May 03, 2008 6:40 am    Post subject:
Reply with quote

i also got a similar requirement where i had to increment the SN column of the table by 1 for each insert in the table. i.e. first the query had to read the last value in the SN column and then increment it by 1 for the insertion of the new row.
hope this may help you.

INSERT INTO MYTABLE SELECT SUBSTR ( MAX ( SN ), 1, 1 ) || CHAR ( INT ( SUBSTR ( MAX ( SN ), 2, 4 ) ) + 1 ) FROM MYTABLE;

here my column SN was like :
Code:

SN
-----
C1000
C1001
C1002
C1003
Back to top
View user's profile Send private message
Anuj Dhawan

Senior Member


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

PostPosted: Sat May 03, 2008 3:15 pm    Post subject:
Reply with quote

Hi,

In Your case "SN" column was 'part' of the table, here OP wants to add sequence number "in front of" the reords fetched via
Code:
select * from tablename

using which He gets something like
Code:
shaun
ron
mary

while he wants
Code:
001 shaun
002 ron
003 mary

by a single query.

There is no such "SN" (Serial Number, I think) cloumn there in His table.
Back to top
View user's profile Send private message
the_gautam

Active User


Joined: 05 Jun 2005
Posts: 165
Location: Bangalore

PostPosted: Sat May 03, 2008 8:48 pm    Post subject:
Reply with quote

OK, i think i was not able to understand his requirement clearly... icon_question.gif
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
No new posts Need DB2 query to fetch previous row ! Chandan1993 DB2 10 Sat Jun 03, 2017 10:43 am
No new posts SMTP the current generation number of... Jyothi Kulunde JCL & VSAM 4 Thu May 04, 2017 4:08 pm
No new posts Edit large number of datasets (QSAM) zh_lad TSO/ISPF 3 Tue Apr 04, 2017 6:08 pm
No new posts how to mask the phone number kumarinfy DB2 4 Mon Apr 03, 2017 5:23 pm
No new posts JCL to Set Return code based on DB2 S... vinu78 DB2 17 Mon Mar 13, 2017 9:47 pm


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