If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > Oracle > how to update the colum in the same table after insert one row?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-04-11, 01:39
ssathish84 ssathish84 is offline
Registered User
 
Join Date: Dec 2011
Posts: 2
how to update the colum in the same table after insert one row?

hi,
i have a table called student(studentId,name,studentNo).
the studentNo is to be updated with studentId+'some text' after inserting the
row.
if i pass value like student(1,jack)
then the row should be updated like
studentId- 1
studentName- jack
studentNo- SNO1

thanks in advance.
Reply With Quote
  #2 (permalink)  
Old 12-04-11, 02:57
Littlefoot Littlefoot is offline
Lost Boy
 
Join Date: Jan 2004
Location: Croatia, Europe
Posts: 3,629
A database trigger can do that for you.

Code:
SQL> create table student (studentid number, stu_name varchar2(20), sno varchar2(20));

Table created.

SQL>
SQL> create or replace trigger trg_stu
  2    before insert on student
  3    for each row
  4  begin
  5    :new.sno := 'sno' || to_char(:new.studentid);
  6  end;
  7  /

Trigger created.

SQL> insert into student (studentid, stu_name) values (1, 'Jack');

1 row created.

SQL> insert into student (studentid, stu_name) values (230, 'Jill');

1 row created.

SQL> select * from student;

 STUDENTID STU_NAME             SNO
---------- -------------------- --------------------
         1 Jack                 sno1
       230 Jill                 sno230

SQL>
Reply With Quote
  #3 (permalink)  
Old 12-06-11, 01:14
Mr.Vivek Mr.Vivek is offline
Registered User
 
Join Date: Dec 2011
Posts: 15
Arrow Update Columns

create table student (studentid number, stu_name varchar2(20), sno varchar2(20));

If that table all ready have lots of records then
U have to update that column like


UPDATE student
SET SNO = 'Sno'||studentid;
Commit;
Reply With Quote
  #4 (permalink)  
Old 12-07-11, 09:05
ssathish84 ssathish84 is offline
Registered User
 
Join Date: Dec 2011
Posts: 2
how to retrieve the value generated by the trigger?

thank you mr.vivek
it works fine.

i am using hibernate to persist the student (studentid, stu_name,sno) object.while i am saving the student object the sno column is updated through the trigger.after saving the student object i can't get the student object with sno value which is updated by the trigger.how can i get that!pls reply.
Reply With Quote
  #5 (permalink)  
Old 12-07-11, 15:22
Littlefoot Littlefoot is offline
Lost Boy
 
Join Date: Jan 2004
Location: Croatia, Europe
Posts: 3,629
Quote:
after saving the student object i can't get the student object with sno value which is updated by the trigger
You can't? Why not?
Reply With Quote
  #6 (permalink)  
Old 12-07-11, 16:20
beilstwh beilstwh is offline
Lead Application Develope
 
Join Date: Jun 2004
Location: Liverpool, NY USA
Posts: 2,222
You tried

select sno from student;
__________________
Bill
You do not need a parachute to skydive. You only need a parachute to skydive twice.
Reply With Quote
  #7 (permalink)  
Old 12-07-11, 16:53
flyboy flyboy is offline
Registered User
 
Join Date: Mar 2007
Posts: 546
@Littlefoot: I would say the reason is "hibernate". I do not know that tool, but it seems to be "just another interface over database sql to disable most its features".

To the solution: Either issue INSERT statement with RETURNING clause (this is the place where the value of SNO would be returned) or use that formula (it is not that difficult, just a string concatenation) before issuing INSERT and fill SNO column directly with it.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On