View previous topic :: View next topic
|
Author |
Message |
ANISH THOMAS
New User
Joined: 23 Jan 2008 Posts: 2 Location: pune
|
|
|
|
hi! all,
i want to insert a value in a column depending on the previous value of that same column.
let me clear it with a example.
i have a employee table( columns are emp-id, name, salary). now i want to add a new row to that table. so i want that the emp-id column to be inserted automatically with different values whenever i insert a new row.
so how will i write a querry which will fetch the last emp-id n based on that it will generate a new emp-id(like incrementing the last emp-id value).
i hope i am clear. |
|
Back to top |
|
|
stodolas
Active Member
Joined: 13 Jun 2007 Posts: 632 Location: Wisconsin
|
|
|
|
Code: |
SELECT MAX(emp_id)+1 FROM emp_table; |
|
|
Back to top |
|
|
ravi17s Warnings : 1 New User
Joined: 15 Aug 2003 Posts: 57
|
|
|
|
If your are in V8 I suggest you to go for Identity column.
More easy to maintain.....
In your ex the EMP_ID column must be defined as an Identity column. |
|
Back to top |
|
|
abhishekmdwivedi
New User
Joined: 22 Aug 2006 Posts: 95 Location: india
|
|
|
|
Hi ,
This might be helpful,
Create table emp1(
emp_no integer not null
generated always as identity
(start with 1
,increment by 1
, no maxvalue
,no cycle
,order)
,name ..... |
|
Back to top |
|
|
|