Home » Developer & Programmer » Forms » how to insert and update with single statement...? (6i)
how to insert and update with single statement...? [message #546123] Mon, 05 March 2012 07:04 Go to next message
kartheek
Messages: 17
Registered: February 2012
Location: chennai
Junior Member
hi all ,

I need a sql statemnet . for the below scenario...

I would like to insert a value if that value is not existing in the table (example for a column which contains date only new dates should be inserted and if the date already exists in the column then it needs to get updated ) can any one help me out in this please..?
thanks in advance..

example of scenario...

date s1 s2 s3


in the above if the date is new..it should get inserted with the appropriate slot no.(s1,s2,s3)
if the date already exists it needs to update the no.in slot no.
Re: how to insert and update with single statement...? [message #546135 is a reply to message #546123] Mon, 05 March 2012 07:39 Go to previous messageGo to next message
Littlefoot
Messages: 21813
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
MERGE statement might be what you need.

As you use Forms 6i, I'm not sure that it'll work in this Forms version. Try it. If it doesn't work, create a stored procedure (which would utilize MERGE) and call that procedure from a form. But, if your database version doesn't support MERGE, huh, you'll need to code the whole thing manually.
Re: how to insert and update with single statement...? [message #546220 is a reply to message #546135] Mon, 05 March 2012 15:47 Go to previous messageGo to next message
owais_baba
Messages: 289
Registered: March 2008
Location: MUSCAT
Senior Member
I think here is your solution i have tested already


create table A(s1 date);



begin  
    insert into a(s1)
             (select a.s1 from a a where s1=:a.s1);
    message('Record Already Exist');
    message('Record Already Exist');
               raise form_trigger_failure;
              exception when no_data_found then
       null;
--  next_item;
   end;  


hope it will work for u

Cool

[Updated on: Mon, 05 March 2012 15:50]

Report message to a moderator

Re: how to insert and update with single statement...? [message #546225 is a reply to message #546220] Mon, 05 March 2012 15:57 Go to previous messageGo to next message
owais_baba
Messages: 289
Registered: March 2008
Location: MUSCAT
Senior Member
if i mis-understood then let me know
Re: how to insert and update with single statement...? [message #546230 is a reply to message #546225] Mon, 05 March 2012 16:38 Go to previous messageGo to next message
owais_baba
Messages: 289
Registered: March 2008
Location: MUSCAT
Senior Member
i think littlefoot is right "MERGE" could work
Re: how to insert and update with single statement...? [message #546238 is a reply to message #546123] Mon, 05 March 2012 21:55 Go to previous messageGo to next message
mvmkandan
Messages: 68
Registered: May 2010
Location: Trivendrum
Member
MERGE STATEMENT is used for Update of the condition is matched or insert.



MERGE <hint> INTO <table_name>
USING <table_view_or_query>
ON (<condition>)
WHEN MATCHED THEN <update_clause>
DELETE <where_clause>
WHEN NOT MATCHED THEN <insert_clause>
[LOG ERRORS <log_errors_clause> <reject limit <integer | unlimited>]; 



CREATE TABLE employee (
employee_id NUMBER(5),
first_name  VARCHAR2(20),
last_name   VARCHAR2(20),
dept_no     NUMBER(2),
salary      NUMBER(10));

INSERT INTO employee VALUES (1, 'Dan', 'Morgan', 10, 100000);
INSERT INTO employee VALUES (2, 'Helen', 'Lofstrom', 20, 100000);
INSERT INTO employee VALUES (3, 'Akiko', 'Toyota', 20, 50000);
INSERT INTO employee VALUES (4, 'Jackie', 'Stough', 20, 40000);
INSERT INTO employee VALUES (5, 'Richard', 'Foote', 20, 70000);
INSERT INTO employee VALUES (6, 'Joe', 'Johnson', 20, 30000);
INSERT INTO employee VALUES (7, 'Clark', 'Urling', 20, 90000);

CREATE TABLE bonuses (
employee_id NUMBER, bonus NUMBER DEFAULT 100);

INSERT INTO bonuses (employee_id) VALUES (1);
INSERT INTO bonuses (employee_id) VALUES (2);
INSERT INTO bonuses (employee_id) VALUES (4);
INSERT INTO bonuses (employee_id) VALUES (6);
INSERT INTO bonuses (employee_id) VALUES (7);
COMMIT;

SELECT * FROM employee;

SELECT * FROM bonuses;

MERGE INTO bonuses b
USING (
  SELECT employee_id, salary, dept_no
  FROM employee
  WHERE dept_no =20) e
ON (b.employee_id = e.employee_id)
WHEN MATCHED THEN
  UPDATE SET b.bonus = e.salary * 0.1
  DELETE WHERE (e.salary < 40000)
WHEN NOT MATCHED THEN
  INSERT (b.employee_id, b.bonus)
  VALUES (e.employee_id, e.salary * 0.05)
  WHERE (e.salary > 40000);

SELECT * FROM bonuses; 



Veera

[Updated on: Mon, 05 March 2012 21:56]

Report message to a moderator

Re: how to insert and update with single statement...? [message #546240 is a reply to message #546238] Mon, 05 March 2012 22:17 Go to previous messageGo to next message
sandeepgujje
Messages: 28
Registered: January 2012
Location: India
Junior Member
DECLARE
V_Count NUMBER(2);
BEGIN
 SELECT COUNT(*)
 INTO V_COUNT
 FROM TABLE_NAME
 WHERE TO_CHAR(SDATE, 'DD-MON-YYYY') = TO_CHAR(:BLOCK_NAME.DATE, 'DD-MON-YYYY);
 
 IF SQL%NOTFOUND THEN
    -- WRITE AN INSERT --
 ELSE
    -- WRITE AN UPDATE --
 END IF;
END;


Hope this one will give you an idea..

Re: how to insert and update with single statement...? [message #546247 is a reply to message #546240] Mon, 05 March 2012 23:18 Go to previous messageGo to next message
kartheek
Messages: 17
Registered: February 2012
Location: chennai
Junior Member
Thanks for all your response....

rather than using sql%notfound we can use the count variable as follows...

declare
v_c number;
begin
select count(*) into v_c from week_info where week_date=:day1;
if v_c=0 then
message('inserting a new date.....');
insert into week_info values (:day1,:day1_slot1,:day1_slot2,:day1_slot3);
else
message('updating the new data....as the date already exists...');
update week_info set s1=:day1_slot1,s2=:day1_slot2,s3=:day1_slot3 where week_date=:day1;
end if;
end;
Re: how to insert and update with single statement...? [message #546287 is a reply to message #546247] Tue, 06 March 2012 01:57 Go to previous message
Littlefoot
Messages: 21813
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
Generally speaking: if something can be done in SQL, you should do it - rather than using PL/SQL. Generally speaking, SQL will be faster than PL/SQL. Generally speaking, it will take more time to write the same (complex) logic in SQL than in PL/SQL, but - you'll benefit from it in a long term.
Previous Topic: updating a record in a cursor issue
Next Topic: Data Block ignoring the order by clause
Goto Forum:
  


Current Time: Sat Jul 06 00:17:03 CDT 2024