![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
hi, I am trying to insert a record in a employee table 1 based on whether a record already exists in table 1. If a record for that employee already exists in table 1, I need to insert the record into table 2 else into table 1 itself. I am trying to do it through a pl/sql db procedure. So the code I have for insert statement is as follows: insert when (empno = 1234) * * * * then into table 2 (empname,empno) * * * * values ( * * * * AAAA, * * * * 1234 * * * * ) * * * * else * * * * into table 1(empname,empno) * * * * values ( * * * * AAAA, * * * * 1234 * * * * ) * * * * select * from table 1 where empno = 1234; The insert statement works when there are no records in table 1 for that employee but fails when there are no records. Can anyone help me as what do I need to add/change in this query for it to work in both the conditions. Any help would be appreciated! |
#3
| |||
| |||
|
#4
| |||
| |||
|
|
hi, I am trying to insert a record in a employee table 1 based on whether a record already exists in table 1. If a record for that employee already exists in table 1, I need to insert the record into table 2 else into table 1 itself. I am trying to do it through a pl/sql db procedure. So the code I have for insert statement is as follows: insert when (empno = 1234) * * * * then into table 2 (empname,empno) * * * * values ( * * * * AAAA, * * * * 1234 * * * * ) * * * * else * * * * into table 1(empname,empno) * * * * values ( * * * * AAAA, * * * * 1234 * * * * ) * * * * select * from table 1 where empno = 1234; The insert statement works when there are no records in table 1 for that employee but fails when there are no records. Can anyone help me as what do I need to add/change in this query for it to work in both the conditions. Any help would be appreciated! |
#5
| |||
| |||
|
|
On Sep 7, 7:44*am, Sandy80 <svarshneym... (AT) gmail (DOT) com> wrote: I am trying to insert a record in a employee table 1 based on whether a record already exists in table 1. If a record for that employee already exists in table 1, I need to insert the record into table 2 else into table 1 itself. I am trying to do it through a pl/sql db procedure. So the code I have for insert statement is as follows: insert when (empno = 1234) * * * * then into table 2 (empname,empno) * * * * values ( * * * * AAAA, * * * * 1234 * * * * ) * * * * else * * * * into table 1(empname,empno) * * * * values ( * * * * AAAA, * * * * 1234 * * * * ) * * * * select * from table 1 where empno = 1234; The insert statement works when there are no records in table 1 for that employee but fails when there are no records. Can anyone help me as what do I need to add/change in this query for it to work in both the conditions. Any help would be appreciated! The second insert needs to be done in an exception block since you raise a *NO_DATA_FOUND error: SQL> create or replace procedure cond_insrt(p_empno IN varchar2) * 2 *is * 3 * * * * *v_empname emp.empname%type; * 4 *begin * 5 * * * * *select empname into v_empname from table_1 whereempno = p_empno; * 6 * 7 * * * * *if v_empname is not null then * 8 * * * * * * * * *insert into table_2 (empname, empno) * 9 * * * * * * * * *values (v_empname, p_empno); *10 * * * * *end if; *11 *exception *12 * * * * *when no_data_found then *13 * * * * * * * * *insert into table_1 (empname, empno) *14 * * * * * * * * *values ('AAAA', p_empno); *15 * * * * *when others then *16 * * * * * * *dbms_output.put_line('Displaying the error stack:'); *17 * * * * * * *dbms_output.put(dbms_utility.format_error_stack); *18 dbms_output.put_line(dbms_utility.format_error_bac ktrace); *19 *end; *20 */ Procedure created. |
![]() |
| Thread Tools | |
| Display Modes | |
| |