Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

query TO GENERATE AUTO INCREMENTED VALUES FROM A MAX NUMBER OF ANOTHER TABLE ...

3648470Jun 8 2018 — edited Jun 8 2018

i have a table emp1.

create table emp1 (empid number(5), ename varchar2(50),deptno number(5), designation varchar2(50),sal number(10),loc varchar2(50))

insert into emp1 values(1,458956,'john',10,'Clerk',1000,'London')

insert into emp1 values(2,212854,'Joseph',20,'CA',7000,'USA')

insert into emp1 values(3,100247,'Arjun',30,'Manager',10000,'India')

insert into emp1 values(4,326856,'Jim',40,''Senior Clerk',6000,'China')

Emp1:-

Seq_numberempidenamedeptnodesignationsalloc
101458956John10Clerk1000London
102212854Joseph20CA7000USA
103100247Arjun30Manager10000India
104326856Jim40Senior Clerk6000

China

Emp2:-

create table emp2 (empid number(5), ename varchar2(50),deptno number(5), designation varchar2(50),sal number(10).,loc varchar2(50))

insert into emp2 values(101,458956,'john',10,'Clerk',1000,'London')

insert into emp2 values(102,212854,'Joseph',20,'CA',7000,'USA')

insert into emp2 values(103,100247,'Arjun',30,'Manager',10000,'India')

insert into emp2 values(104,326856,'Jim',40,''Senior Clerk',6000,'China')

insert into emp2 values(105,203323,'Catherine',40,''Senior Clerk',6000,'China')

insert into emp2 values(106,152830','Mario',60,'BDM',12000,'Canada')

insert into emp2 values(107,200674,'George',10,'SSE',7000,'Japan')

Seq_number

empidenamedeptnodesignationsalloc
101458956John10Clerk1000London
102212854Joseph20CA7000Canada
103100247Arjun30Manager10000India
104326856Jim40Senior Clerk6000

China

203323Catherine50Developer8000USA
152830Mario60BDM12000Canada
200674George10SSE7000Japan

Now my requirement is to compare the emp1 and emp2 tables and generate the sequence number for table emp2 as sequence number is not assigned for the empids 203323, 152830,200674 and output the data in new table emp_output.

Note:- seq_number should be generated in considering the max value of emp2 and then seq_number should be generated as show in required output format.

emp_output table.

create table emp_output(seq_number number(5), empid number(5), ename varchar2(50), loc varchar2(50),status varchar2(50))

Required o/p;-

Seq_numberempidenameLocStatus
102212854JosephCanadaRecord Updated
105203323CatherineUSANew Record
106152830MarioCanadaNew Record
107200674GeorgeJapanNew Record

I tried the comparion of data using union all , the expected results are coming but am unable to generate the seq_number for emp2 table. Can anyone look into this.

Message was edited by: 3648470 removed the sequence from emp1 i.e create sequence seq_new start with 100 increment by 1;

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 6 2018
Added on Jun 8 2018
6 comments
897 views