Oracle: adding numbering to a table...


 
Thread Tools Search this Thread
Top Forums Programming Oracle: adding numbering to a table...
# 1  
Old 03-05-2009
Oracle: adding numbering to a table...

As a disclaimer, I am not a database person. I have some basic knowledge, but my area is in other fields. Please treat me like I am stupid when it comes to this question.

An Oracle script has been dumped on me, which I have been able to work out an understanding of, but I need to make a change to it. The script takes data from several different tables and puts it all together into a temporary table where various changes are made before it reports what it has. I need to add a simple loop which goes over each row and increments a value until a certain condition is met. This much I can handle, but there is no way to track my place in the table. I want to add a column that simply indexes the table 1,2,3,...,n. I have tried to Google this, but either I could not find it, or I could not understand it. I think it may have been the latter.

The table creation takes the following basic form (there are a lot of values, so copying it all in would be over-kill):
Code:
CREATE TABLE name AS
SELECT a,b,c
FROM x,y,z
WHERE n;

How do I modify this to add an indexing column?
# 2  
Old 03-05-2009
You mean like access has?
1. create a sequence - make sure it has a public synonym so other users can access it
see HELP CREATE SEQUENCE
see help on create synonym
see help on grant to allow others to access the sequence

2.
Code:
select sequencename.nextval from dual;

lets you get the next value from the sequence.

You are gonna have to give us a better example than your select statement, so we can give you a working example of putting the sequence number into your table.

If you are having performance problems consider adding an index to your table.
# 3  
Old 03-05-2009
Sorry, I do not think I follow. It is not a performance issue --- this is only a temporary table that gets thrown away later. After this table is built, I will be adding a section which loops over it, something like:

count number;
count = 1;
select max(index) into max_rows from temp;
while count <= max_rows
loop
update temp set x=y where index=count;
count = count + 1
endloop;

I want to add column of sequential, unique numbers to the table (either during or after creation) that I can use as, basically, my place-holder as I update each row in the table. The select I showed was exactly how the table is built (albeit the original has create table temp as select a.colum1, b.colum2, b.column10 from table1 a, table2 b where a.column1='blah' and b.column2='blah', etc).
# 4  
Old 03-05-2009
- SCA name?
Anyway the sequence is what you want. That is what "numbering' in Oracle is called.

Code:
create sequence mysequence
  minvalue 1
  start with 1
  increment by 1; 
CREATE TABLE name AS
SELECT a,b,c, (select mysequence.nextval from dual)
FROM x,y,z
where n;
drop sequence mysequence;

Dropping and creating tables and sequences just to run a report is not consdiered best practice, ie. running DDL during routine production

But since you already are doing it, why not just keep on with the trend....

You now have a last field which is a number 1... n.
# 5  
Old 03-10-2009
Thanks for the help there. It took a little messing around to get it to work (apparently sequences cannot be used in a select within a select), but eventually I managed to sort things out. It seems awfully complicated for what seems a simple enough operation, but I learned quite a few things out of this.
# 6  
Old 03-10-2009
You can also include rownum in the select. For example:
Code:
SELECT rownum, filed1, field2 FROM table WHERE field3='X';

ROWNUM is the number of the row - it is added by Oracle to every query.
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. UNIX and Linux Applications

Help in copying table structure to another table with constraints in Oracle

hi, i need to copy one table with data into another table, right now am using create table table1 as select * from table2 i want the constraints of table1 to be copied to table2 also , can anyone give me some solution to copy the constraints also, now am using oracle 10.2.0.3.0... (1 Reply)
Discussion started by: senkerth
1 Replies

2. Shell Programming and Scripting

Adding a field to a file using a conversion table

Hello everyone, Here is what i am trying to accomplish. I have a transaction log that I want to to add a field. The fields in the transaction log are tab delimited FYI. My goal is to add a column specifying the category/type to each item purchased. I have created a two column "conversion table"... (2 Replies)
Discussion started by: SpencerClark
2 Replies

3. Shell Programming and Scripting

Check the record count in table (table in oracle)

I have requirement: 1) Check the record count in table (table in oracle) 2) If records exists generate the file for existing records and wait for some time then Go to sleep mode and Again check the record count after 10 min.......... (Loop this process if record count >0). 3) Generate touch... (1 Reply)
Discussion started by: kamineni
1 Replies

4. Shell Programming and Scripting

Check the record count in table (table in oracle)

I have requirement: 1) Check the record count in table (table in oracle) 2) If records exists generate the file for existing records and wait for some time (Go to sleep mode) and Again check the record count after 10 min.......... (Loop this process if record count >0). 3) Generate touch... (1 Reply)
Discussion started by: kamineni
1 Replies

5. Shell Programming and Scripting

Adding character to spaces in a table

Hi All, I am trying to get the spaces in the below table to be fill up with a character " - ". For eg, coordinates 0202 is a space but i would want to fill up with " - ". Can anybody help ? Input: 04 D H ... (15 Replies)
Discussion started by: Raynon
15 Replies

6. Shell Programming and Scripting

Track changes to Oracle table

Hi, I'm trying to write a program with unix shell scripting to track the changes of a particular table in the Oracle database. I've try reading up on sql TRIGGER function, but I'm not sure if its feasible here. Any idea what kind of syntax should I use to track the changes in the table? Can... (0 Replies)
Discussion started by: mervinboyz
0 Replies

7. IP Networking

Adding an extra route to the ip routing table

In my college dorm, there is a file sharing network in the entire building. Problem is, there is only a manual for windows with the settings on how to connect... :mad: They say that you have to give the following command in cmd in windows: route add 172.16.71.0 mask 255.255.255.0... (2 Replies)
Discussion started by: Japie89
2 Replies

8. HP-UX

upload oracle table

am on HP-Unix and want to upload table on windows oracle from HP-Unix Thanx Swapnil (2 Replies)
Discussion started by: swapnil286
2 Replies
Login or Register to Ask a Question