In oracle I would like to check if a record exists before insert then if exist update the record.
This site was really helpful. Is there a better method to do what I am trying to do? Why did the second method not work?
https://www.geeksforgeeks.org/how-to-check-if-a-row-already-exists-in-plsql/
DECLARE
v_count NUMBER;
BEGIN
SELECT COUNT(*)
INTO v_count
FROM employees
WHERE employee_id = 100;
IF v_count > 0 THEN
DBMS_OUTPUT.PUT_LINE('Row exists in employees table for employee_id = 100');
ELSE
DBMS_OUTPUT.PUT_LINE('Row does not exist in employees table for employee_id = 100');
END IF;
END;
DECLARE
v_exists BOOLEAN := FALSE;
BEGIN
SELECT 'TRUE'
INTO v_exists
FROM dual
WHERE EXISTS (
SELECT 1
FROM employees
WHERE employee_id = 101
);
IF v_exists THEN
DBMS_OUTPUT.PUT_LINE('Row exists in employees table for employee_id = 101');
ELSE
DBMS_OUTPUT.PUT_LINE('Row does not exist in employees table for employee_id = 101');
END IF;
END;
Initial setup
CREATE TABLE people(
id int primary key,
name varchar(55),
status int,
phone varchar(55)
);
INSERT INTO people (id, name, status, phone)
WITH names AS (
SELECT 1 id, 'jOHN' name, 1 status, '214-444-1234' phone FROM dual UNION ALL
SELECT 2, 'John', 1, '214-444-1234' FROM dual UNION ALL
SELECT 3, 'Mary', 0, '555-111-1234' FROM dual UNION ALL
SELECT 4, 'Mary', 0, '555-111-1234' FROM dual UNION ALL
SELECT 5, 'Jeff', 0, '214-222-1234' FROM dual UNION ALL
SELECT 6, 'Jeff', 0, '214-222-1234' FROM dual UNION ALL
SELECT 7, 'Bill', 1, '817-333-1234' FROM dual UNION ALL
SELECT 8, 'Bill', 1, '817-333-1234' FROM dual UNION ALL
SELECT 9, 'Bob' , 1, '214-555-1234' FROM dual UNION ALL
SELECT 10, 'Bob' , 1, '214-555-1234' FROM dual UNION ALL
SELECT 11, 'Ruth', 1, 'Mrs' FROM dual UNION ALL
SELECT 12, 'Isabelle', 2, 'Miss' FROM dual UNION ALL
SELECT 13, 'Justin', 3, 'Master' FROM dual UNION ALL
SELECT 14, 'Lisa', 4, 'Dr' FROM dual
)
SELECT * FROM names;
This worked on my database.
DECLARE
v_count NUMBER;
BEGIN
-- Check if a row exists based on specific conditions
SELECT COUNT(*)
INTO v_count
FROM people
WHERE id = 1;
IF v_count > 0 THEN
-- Row exists
-- Perform necessary actions
UPDATE people SET name = 'jack' WHERE id = 1;
ELSE
-- Row does not exist
-- Perform necessary actions
INSERT INTO people VALUES
(1, 'Josh', 1, '214-444-1235');
END IF;
END;
Surprisingly did not work on dbfiddle.
ORA-06550: line 21, column 3:
PLS-00103: Encountered the symbol "end-of-file" when expecting one of the following:
; <an identifier> <a double-quoted delimited-identifier>
The symbol ";" was substituted for "end-of-file" to continue.
I was unable to make the second method work on my database or dbfiddle.
DECLARE
v_exists BOOLEAN := FALSE;
BEGIN
SELECT 'TRUE'
INTO v_exists
FROM dual
WHERE EXISTS (
SELECT 1
FROM people
WHERE id = 1
);
IF v_exists THEN
UPDATE people SET name = 'jack' WHERE id = 1;
ELSE
INSERT INTO people VALUES
(1, 'Josh', 1, '214-444-1235');
END IF;
END;
This was my database error message.
[Error] Execution (71: 8): ORA-06550: line 5, column 8:
PLS-00382: expression is of wrong type
This was dbfiddle error message.
ORA-06550: line 19, column 3:
PLS-00103: Encountered the symbol "end-of-file" when expecting one of the following:
; <an identifier> <a double-quoted delimited-identifier>
The symbol ";" was substituted for "end-of-file" to continue.
https://dbfiddle.uk/b7z78r_u