Using Insert/Update with 2 Tables - Oracle Apex - sql

I'm still new to SQL and am having a hard time figuring out how to update and insert from one table into another table in Oracle Apex (uses SQL).
The two tables are named Temp and Table (example) and both have the same columns. Basically they are copies of each other but with different data. I would like to compare the ID field in Temp to the ID field in Table and if there is a row that matches that ID field in Temp, then to overwrite all the data on the row in Table with the data in the corresponding row in Temp.
Note: Table has 10 million rows of data, Temp has like 500.
if Temp.ID = Table.ID then
update
set
Table.ID = Temp.ID
Table.Address = Temp.Address
else
insert
(Table.ID,
Table.Address)
values
(Temp.ID,
Temp.Address
That is basically what I want done but not sure how to write it in SQL. Have seen a lot of different answers, but none really involving 2 tables and mostly for MySQL or SQL Server specific SQL that I am not sure also works with Oracle.
MERGE into TEST1
USING TEST2 on (TEST2.ID = TEST1.ID)
WHEN matched THEN UPDATE
SET TEST1.ID = TEST2.ID, TEST1.NAME = TEST2.NAME, TEST1.ADDRESS = TEST2.ADDRESS, TEST1.EMAIL = TEST2.EMAIL
WHEN not matched THEN INSERT (ID, NAME, ADDRESS, EMAIL) values (TEST2.ID, TEST2.NAME, TEST2.ADDRESS, TEST2.EMAIL);
I tried this but it is giving me the error:
ORA-38104: Columns referenced in the ON Clause cannot be updated: "TEST1"."ID"
UPDATE: Got it to work!
http://db-oriented.com/2013/09/20/the-merge-statement/
Was helpful with the answer to the error and I did not know that you could not have the IDs in the update clause, which makes total sense now. Thank you also to the respondent below for explaining the merge code to me. :)

Looks like a good candidate for MERGE. Have a look at the example.
Sample tables & data:
SQL> create table ttable (id number, address varchar2(20));
Table created.
SQL> create table temp (id number, address varchar2(20));
Table created.
SQL> insert into ttable
2 select 1, 'Address 1' from dual union all
3 select 2, 'Address 2' from dual union all
4 select 3, 'Address 3' from dual;
3 rows created.
SQL> insert into temp
2 select 1, 'New address 1' from dual union all
3 select 2, 'New address 2' from dual union all
4 select 4, 'New address 4' from dual;
3 rows created.
Merge & the result:
SQL> merge into ttable a
2 using temp e on (e.id = a.id)
3 when matched then update set a.address = e.address
4 when not matched then insert (id, address) values (e.id, e.address);
3 rows merged.
SQL> select * from ttable;
ID ADDRESS
---------- --------------------
1 New address 1
2 New address 2
3 Address 3
4 New address 4
SQL>

Related

How to unescape % in LIKE clause

I have my search patterns stored in database in patterns table. For example my table column name_pattern contains string 'Basic%'. I'd like to create dynamic search where search patterns will be fetched from name_pattern column.
So my SQL query should look something like:
SELECT *
FROM products
WHERE product_name LIKE name_pattern <-- somehow joined from patterns table
Seems that Oracle escapes % in my string but I want to take it unescapped in order my query to work like:
SELECT *
FROM products
WHERE product_name LIKE 'Basic%'
I found that my problem is with stable set of rows:
CREATE TABLE patterns(code CHAR(1),name_pattern VARCHAR2(20));
INSERT INTO patterns(code,name_pattern) VALUES('B','Basic%');
INSERT INTO patterns(code,name_pattern) VALUES('T','%thing');
CREATE TABLE products (id NUMBER,name VARCHAR2(20),code CHAR(1));
INSERT INTO products(id,name,found) VALUES(1,'Basic instinct',NULL);
INSERT INTO products(id,name,found) VALUES(2,'Basic thing',NULL);
INSERT INTO products(id,name,found) VALUES(3,'Super thing',NULL);
INSERT INTO products(id,name,found) VALUES(4,'Hyper instinct',NULL);
MERGE INTO products p USING
(
SELECT code,name_pattern FROM patterns
) s
ON (p.name LIKE s.name_pattern)
WHEN MATCHED THEN UPDATE SET p.code=s.code;
SELECT * FROM products;
If my search patterns were Basic% and Super% in patterns table then this MERGE will work, but if my search patterns are Basic% and %thing, the second product should be marked with both codes 'B' and 'T' and that causes the error:
ORA-30926: unable to get a stable set of rows in the source tables
So my problem is not in (un)escaping :-(, sorry
You don't have to (un)escape anything, I'd say.
SQL> with
2 patterns (name_pattern) as
3 (select 'Basic%' from dual union all
4 select '%foot%' from dual
5 ),
6 products (id, name) as
7 (select 1, 'Basic instinct' from dual union all
8 select 2, 'Visual Basic' from dual union all
9 select 3, 'Littlefoot' from dual union all
10 select 4, 'Happy feet' from dual
11 )
12 select b.id, b.name, a.name_pattern
13 from products b join patterns a on b.name like a.name_pattern;
ID NAME NAME_P
---------- -------------- ------
1 Basic instinct Basic%
3 Littlefoot %foot%
SQL>
Based on test case you provided: don't merge, update!
SQL> update products p set
2 p.found = 1
3 where exists (select null
4 from patterns o
5 where p.name like o.name_pattern
6 );
3 rows updated.
SQL> select * from products;
ID NAME FOUND
---------- -------------------- ----------
1 Basic instinct 1
2 Basic thing 1
3 Super thing 1
4 Hyper instinct 0
SQL>
After you changed your mind (again), it is still update. Though, you didn't explain which code you want to take when there's multiple match (for example, product 2 matches both "Basic%" and "%thing") so I took any of them, using the min function.
SQL> update products p set
2 p.code = (select min(o.code)
3 from patterns o
4 where p.name like o.name_pattern
5 );
4 rows updated.
SQL> select * from products;
ID NAME CODE
---------- -------------------- ----------
1 Basic instinct B
2 Basic thing B
3 Super thing T
4 Hyper instinct NULL
SQL>

Is it possible to insert multiple rows in a table based on a select returning more than one row

Using Oracle SQL, I am trying to insert into table A based on select from table B, but I am not sure how to achieve this, since the select is returning more than one row.
INSERT INTO A
VALUES
(
SELECT id FROM B WHERE status = 'APPROVED',
'Hardcoded-Value'
);
Table B:
id
status
1
APPROVED
2
DECLINED
3
APPROVED
Based on that insert, I want to achieve following:
Table A:
Column A
Column B
1
Hardcoded-Value
3
Hardcoded-Value
You can use a const in the select list
INSERT INTO A(colA, colB)
SELECT id, 'Hardcoded-Value'
FROM B
WHERE status = 'APPROVED'

Update values of one table from other table

For Example I have 2 tables
Table 1:
id number name
-----------------------
1 1684 abc
2 9666 pqr
3 1234 adf
Table 2:
id number name
-----------------------
1 9109 xyz
2 9564 pqr
How do i get output like this?
Table 1:
id number name
-----------------------
1 9109 xyz
2 9564 pqr
3 1234 adf
I want to merge table 2 in table.
Also if the column-"name" value is updated/changed from this merge, I need to update status column and i need to call a procedure of smtp mail, how can i handle this?
The status column has different status transitions(eg: x->y, a->b).
and if the record is not in table1 it should be inserted.
These all operations are inside a procedure used in batch job.
Please advice me with this.
Thank you in advance for help.
There are different ways to do this, say, using MERGE statement, but most simple is
Connected to Oracle Database 12c Enterprise Edition Release 12.1.0.2.0
SQL> create table t$1 ("id" integer, "number" integer, "name" varchar2(3));
Table created
SQL> create table t$2 ("id" integer, "number" integer, "name" varchar2(3));
Table created
SQL> insert into t$1 values (1, 1684, 'abc');
1 row inserted
SQL> insert into t$1 values (2, 9666, 'pqr');
1 row inserted
SQL> insert into t$1 values (3, 1234, 'adf');
1 row inserted
SQL> insert into t$2 values (1, 9109, 'xyz');
1 row inserted
SQL> insert into t$2 values (2, 9564, 'pqr');
1 row inserted
SQL> update t$1 set
2 ("number", "name") = (select "number", "name" from t$2 where t$2."id" = t$1."id")
3 where
4 "id" in (select "id" from t$2);
2 rows updated
SQL> select * from t$1;
id number name
-------- -------- --------
1 9109 xyz
2 9564 pqr
3 1234 adf
You can use MERGE to do it very simply:
MERGE INTO table1 dst
USING table2 src
ON ( src.id = dst.id )
WHEN MATCHED THEN
UPDATE SET number = src.number,
name = src.name;

Oracle -- Update the exact column referenced in the ON clause

I think this requirement is rarely encountered so I couldn't search for similar questions.
I have a table that needs to update the ID. For example ID 123 in table1 is actually supposed to be 456. I have a separate reference table built that stores the mapping (e.g. old 123 maps to new id 456).
I used the below query but apparently it returned error 38104, columns referenced in the ON clause cannot be updated.
MERGE INTO table1
USING ref_table ON (table1.ID = ref_table.ID_Old)
WHEN MATCHED THEN UPDATE SET table.ID = ref_table.ID_New;
Is there other way to achieve my purpose?
Thanks and much appreciated for your answer!
Use the ROWID pseudocolumn:
SQL Fiddle
Oracle 11g R2 Schema Setup:
CREATE TABLE TABLE1( ID ) AS
SELECT 1 FROM DUAL UNION ALL
SELECT 2 FROM DUAL UNION ALL
SELECT 3 FROM DUAL;
CREATE TABLE REF_TABLE( ID_OLD, ID_NEW ) AS
SELECT 1, 4 FROM DUAL UNION ALL
SELECT 2, 5 FROM DUAL;
MERGE INTO TABLE1 dst
USING ( SELECT t.ROWID AS rid,
r.id_new
FROM TABLE1 t
INNER JOIN REF_TABLE r
ON ( t.id = r.id_old ) ) src
ON ( dst.ROWID = src.RID )
WHEN MATCHED THEN
UPDATE SET id = src.id_new;
Query 1:
SELECT * FROM table1
Results:
| ID |
|----|
| 4 |
| 5 |
| 3 |
You can't update a column used in the ON clause in a MERGE. But if you don't need to make other changes that MERGE allows like WHEN NOT MATCHED or deleting, etc. you can just use a UPDATE to achieve this.
You mentioned this is an ID that needs an update. Here's an example using a scalar subquery. As it is an ID, this presumes UNIQUE ID_OLD values in REF_TABLE. I wasn't sure if Every row needs an update or only a sub-set, so set the update here to only update rows that have a value in REF_TABLE.
CREATE TABLE TABLE1(
ID NUMBER
);
CREATE TABLE REF_TABLE(
ID_OLD NUMBER,
ID_NEW NUMBER
);
INSERT INTO TABLE1 VALUES (1);
INSERT INTO TABLE1 VALUES (2);
INSERT INTO TABLE1 VALUES (100);
INSERT INTO REF_TABLE VALUES (1,10);
INSERT INTO REF_TABLE VALUES (2,20);
Initial State:
SELECT * FROM TABLE1;
ID
1
2
100
Then make the UPDATE
UPDATE TABLE1
SET TABLE1.ID = (SELECT REF_TABLE.ID_NEW
FROM REF_TABLE
WHERE REF_TABLE.ID_OLD = ID)
WHERE TABLE1.ID IN (SELECT REF_TABLE.ID_OLD
FROM REF_TABLE);
2 rows updated.
And check the change:
SELECT * FROM TABLE1;
ID
10
20
100

delete rows using sql 'like' command using data from another table

I am trying to delete rows from a table ("lovalarm") where a field ("pointid") is like any one of a number of strings.
Currently I am entering them all manually however I need to be able to have a list of over 100,000 options.
My thoughts are to have a table ("lovdata") containing all possible strings and running a query to delete rows where the field is 'like' any of the strings in the other table.
Can anyone point me in the right direction as to if/how I can use like in this way?
Many thanks,
Cap
sure you can join with the LIKE operator:
DELETE FROM lovalarm a
WHERE EXISTS (SELECT NULL
FROM lovdata d
WHERE a.pointid LIKE d.pointid)
The lovdata.pointid column may contain wildcards, consider:
SQL> CREATE TABLE lovalarm AS
2 SELECT 'AA' pointid FROM dual
3 UNION ALL SELECT 'AB' FROM dual
4 UNION ALL SELECT 'AC' FROM dual
5 UNION ALL SELECT 'BA' FROM dual
6 UNION ALL SELECT 'BB' FROM dual
7 UNION ALL SELECT 'BC' FROM dual;
Table created
SQL> --# suppose that you want to remove all entries that start
2 --# with A or end with C
3 CREATE TABLE lovdata AS
4 SELECT 'A%' pointid FROM dual
5 UNION ALL SELECT '%C' FROM dual;
Table created
SQL> DELETE FROM lovalarm a
2 WHERE EXISTS (SELECT NULL
3 FROM lovdata d
4 WHERE a.pointid LIKE d.pointid);
4 rows deleted
SQL> select * from lovalarm;
POINTID
-------
BA
BB
Just use a sub query, like this:
DELETE
FROM lovalarm
WHERE pointid IN (SELECT mystring FROM lovdata)
EDIT: Just noticed that LIKE is required, so IN isn't going to work as this will be exact matches. I'll leave this here for reference as Vincent has already added the correct query.
here is an example with the % concatenations
CREATE TABLE LOVALARM(POINTID VARCHAR2(50) ,TXT VARCHAR2(50));
create table deleteThese(deleteStringList varchar2(50));
/
INSERT INTO LOVALARM(POINTID, TXT) VALUES('abc def','1');
INSERT INTO LOVALARM(POINTID, TXT) VALUES('def','2');
INSERT INTO LOVALARM(POINTID, TXT) VALUES('abc','3');
INSERT INTO LOVALARM(POINTID, TXT) VALUES('efd','4');
INSERT INTO LOVALARM(POINTID, TXT) VALUES('abb','5');
INSERT INTO LOVALARM(POINTID, TXT) VALUES('efd bbb','6');
INSERT INTO LOVALARM(POINTID, TXT) VALUES('abb ccc','7');
INSERT INTO LOVALARM(POINTID, TXT) VALUES('abbbccc','8');
INSERT INTO DELETETHESE(DELETESTRINGLIST) VALUES('abc');
INSERT INTO DELETETHESE(DELETESTRINGLIST) VALUES('bbb');
COMMIT;
/
DELETE LOVALARM
where exists(
SELECT NULL
FROM DELETETHESE
where LOVALARM.pointid like '%' || dELETETHESE.DELETESTRINGLIST || '%'
);
select * from LOVALARM;
POINTID TXT
-------------------------------------------------- -----
def 2
efd 4
ABB 5
abb ccc 7