| updating Nth row and adding a column (merged 2) [message #562958] |
Tue, 07 August 2012 05:55  |
 |
nischalinn
Messages: 84 Registered: May 2012 Location: nepal
|
Member |
|
|
I had created a table which have 100s' of entries in it.
create table reg_user
(
USERNAME VARCHAR2(50),
PASSWORD VARCHAR2(20)
)
***
insert all
into reg_user (USERNAME,PASSWORD) values ('A','aa')
into reg_user (USERNAME,PASSWORD) values ('B','vv')
into reg_user (USERNAME,PASSWORD) values ('C','ff')
into reg_user (USERNAME,PASSWORD) values ('D','ee')
--hundreds of ROWS inserted
select * from dual;
1. In this table, how to update the Nth row, how can I do it
2. Now I need to add USERID in this table,which will get value from 1 to max no. of rows. I do not want to drop the table
and again re create it adding USERID or update each row manually.
Is there any other way to add USERID and have IDs from 1 to max IDs.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| Re: updating Nth row and adding a column [message #562973 is a reply to message #562971] |
Tue, 07 August 2012 06:30   |
cookiemonster
Messages: 9284 Registered: September 2008 Location: Rainy Manchester
|
Senior Member |
|
|
1) That does not update the 3rd row. That updates a random row. Rows in a table have no inherent order. You need a column to order by. You don't appear to have one.
2) You get the userid values by running the update littlefoot suggested. There is no single command that will add the column and populate it.
|
|
|
|
|
|
| incrementally increasing userid value via looping [message #563232 is a reply to message #562958] |
Fri, 10 August 2012 05:15   |
 |
nischalinn
Messages: 84 Registered: May 2012 Location: nepal
|
Member |
|
|
I've a table
create table t1
(
USERNAME VARCHAR2(50),
PASSWORD VARCHAR2(20)
)
***
insert all
into t1(USERNAME,PASSWORD) values ('A','aa')
into t1(USERNAME,PASSWORD) values ('B1','vv')
into t1(USERNAME,PASSWORD) values ('C1','iiii')
into t1(USERNAME,PASSWORD) values ('D1','ee')
--other rows
select * from dual;
after creating the row I have to add userid column to the table which will be unique for all the rows.
I've inserted the first entry for the USERID column i.e 1.
Is there any way in oralce such that I select the max userid and then loops until the last row,
incrementally adding userid values to each consecutive rows.
-
Attachment: tabl.PNG
(Size: 4.52KB, Downloaded 11 times)
|
|
|
|
|
|
|
|
|
|
|
|
| Re: incrementally increasing userid value via looping [message #563245 is a reply to message #563241] |
Fri, 10 August 2012 06:33   |
cookiemonster
Messages: 9284 Registered: September 2008 Location: Rainy Manchester
|
Senior Member |
|
|
nischalinn wrote on Fri, 10 August 2012 12:25How can I use a for loop and update each individually for doing this??
That's about the most basic PL/SQL you can write. If you don't know how to code a for loop I suggest you spend some time reading the documentation. It has examples.
Try it. If you get stuck post what you tried here and we'll show you where you've gone wrong.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| Re: incrementally increasing userid value via looping [message #563367 is a reply to message #563365] |
Mon, 13 August 2012 04:33   |
John Watson
Messages: 3179 Registered: January 2010 Location: Global Village
|
Senior Member |
|
|
I can;t stand this any longer.orcl> update scott.emp set sal=rownum;
14 rows updated.
orcl> select ename,sal from emp;
ENAME SAL
---------- ----------
SMITH 1
ALLEN 2
WARD 3
JONES 4
MARTIN 5
BLAKE 6
CLARK 7
SCOTT 8
KING 9
TURNER 10
ADAMS 11
JAMES 12
FORD 13
MILLER 14
14 rows selected.
orcl>
|
|
|
|
|
|
| Re: incrementally increasing userid value via looping [message #563370 is a reply to message #563368] |
Mon, 13 August 2012 04:58   |
 |
nischalinn
Messages: 84 Registered: May 2012 Location: nepal
|
Member |
|
|
Guys thank all of you for the reply. Don't go so rude...
I can do it with the solution you provided but I just want to learn to do the same thing using loop. that's all.
cookiemonster gave me the hint to do the same, and I tried but did not succeed. Is it a sin to try another approach for a problem???
Quote:Hint - FOR UPDATE and WHERE CURRENT OF clauses will help
@John Watson, you are a senior member and you might have many rights as well sound knowledge of SQL but never forget everything is complex before it becomes easy...
Thank You.
[Updated on: Mon, 13 August 2012 04:59] Report message to a moderator
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|