Search code examples
sqloracle-databaseplsqlsql-merge

Merge two tables in PL/SQL block


I have next 3 tables:

  • Fibonacci(id, value) first 1000 fibonacci numbers.
  • Prime(id, value) first 2000 prime numbers.
  • FiboPrime(value, prime), initially the table is empty.

I have to populate the FiboPrime table, with the prime numbers from Fibonacci table (prime=1 => the number is prime, else the number is not prime.)

I have to use MERGE statement and it is attempt:

DECLARE

    BEGING
        MERGE INTO FiboPrime fp
         USING fibonacci f, prime p
         ON (f.value=p.value)
        WHEN MATCHED THEN
         UPDATE SET
          fp.value=f.value,
          fp.prime=1;
    WHEN NOT MATCHED THEN
     INSERT VALUES(f.value,0);
END;

I get the next error: Econtered the symbol "INTO" when expecting one of the following: := .( @ ; not null range character. Who can help me to solve it?


Solution

  • DECLARE

    BEGING

    1. You don't need the keyword DECLARE, since you don't have anything to declare.

    2. The keyword is BEGIN and NOT BEGING.

    3. Modify the USING clause to a SELECT query. For example:

    USING (select t1.col1,t2.col2 from t1,t2)

    1. I don't see there is any reason to do it in PL/SQL. Execute the MERGE as SQL. No need of wrapping it with BEGIN-END block.