MERGE table from two different schema - oracle

I have two same tables from two different schema, db link between schema already created. The table in schema B (old) need to be merged to schema A(new). The table structure between 2 schemas are identical, except an additional timestamp column exist in schema A.The timestamp value is defaulted as SYSDATE when merge.
When i tried to run the following script in schema B, it complains with
ORA-00904: "A"."LAST_UPDATED_TIMESTAMP": invalid identifier
MERGE INTO LOOKUP_RANGE A
USING (SELECT RANGE_NAME,RANGE_TYPE,RANGE_MIN,RANGE_MAX,RANGE_DESC FROM LOOKUP_RANGE) B
ON (A.RANGE_NAME = B.RANGE_NAME AND A.RANGE_TYPE = B.RANGE_TYPE)
WHEN MATCHED THEN UPDATE SET A.RANGE_MIN = B.RANGE_MIN, A.RANGE_MAX = B.RANGE_MAX,A.LAST_UPDATED_TIMESTAMP = SYSDATE,A.RANGE_DESC = B.RANGE_DESC
WHEN NOT MATCHED THEN INSERT (A.RANGE_MIN,A.RANGE_MAX,A.LAST_UPDATED_TIMESTAMP,A.RANGE_DESC) VALUES (A.RANGE_MIN,A.RANGE_MAX,SYSDATE,A.RANGE_DESC);
The key column in ON is the primary key of table.
Am I execute in wrong schema? It should run in schema A?

The below code work perfectly. B_MIG_61_TO_74 is the database link created, OLD is the schema name of old schema, NEW is the schema name of new schema
MERGE INTO NEW.LOOKUP_RANGE A
USING (SELECT RANGE_NAME,RANGE_TYPE,RANGE_MIN,RANGE_MAX,RANGE_DESC FROM OLD.LOOKUP_RANGE#DB_MIG_61_TO_74) B
ON (A.RANGE_NAME = B.RANGE_NAME AND A.RANGE_TYPE = B.RANGE_TYPE)
WHEN MATCHED THEN UPDATE SET A.RANGE_MIN = B.RANGE_MIN, A.RANGE_MAX = B.RANGE_MAX,A.LAST_UPDATED_TIMESTAMP = SYSDATE,A.RANGE_DESC = B.RANGE_DESC
WHEN NOT MATCHED THEN INSERT (A.RANGE_NAME,A.RANGE_TYPE,A.RANGE_MIN,A.RANGE_MAX,A.LAST_UPDATED_TIMESTAMP,A.RANGE_DESC) VALUES (B.RANGE_NAME,B.RANGE_TYPE,B.RANGE_MIN,B.RANGE_MAX,SYSDATE,B.RANGE_DESC);

Related

ORACLE - Update table if exists else insert use rownum to match

I have table master (A) without field ID_No. So i create table B (copy from A) and set B.ID_No = rownum.
Now i want update B if A exist else will insert B with B.ID_NO = A.rownum
In oracle can create query with B.ID_NO = A.rownum?
My logic is ID of B = rownum of A, row++ then ID++, row have change value then fields ID will update.
For rownum repeatability is not guaranteed. You should find unique column or combination of columns for key in tables A and B, for a one-to-one relationship.

Copying the data from the old table to the new table with only different new table has sequence

I need to create a new table from old one, need to insert the rows from the old one, only change is the new sequence column needs to be added in the new one.
Can someone please help me for the same?
INSERT INTO dps_session_map (TrackSESSION_SEQ,session_seq, column3)
   SELECT
        A.SEQ_Session.NEXTVAL, sm.session_seq, sm.column3
    FROM 
        dps_session_map  A,
        dps_exec_session_map sm
WHERE sm.session_seq is not null;
Remove the VALUES keyword and its brackets:
--general pattern of inserting from one table to another existing table
INSERT INTO table(columns)
SELECT columns FROM othertable
Column count and type must match (or be implicitly convertible)
If your new table doesn't exist, do:
CREATE TABLE x AS
(SELECT columns FROM othertable)
INSERT INTO dps_session_map (TrackSESSION_SEQ,session_seq, column3)
SELECT
**SCHEMA.**SEQ_Session.NEXTVAL, sm.session_seq, sm.column3
FROM
dps_session_map A,
dps_exec_session_map sm
WHERE sm.session_seq is not null;
Reference the sequence by its name or fully qualified schema.name. Sequences don't belong to table. Are you getting a "ORA-02289: sequence does not exist" error?

Upserting in GreenPlum

How can I upsert a record in GreenPlum while copying the data from a CSV file. The CSV file has multiple records for a given value of the primary key. If a row with some value already exists in the database I want to update that record. Otherwise, it should append a new row.
One way to do this is to copy the data to a staging table, then insert/update from that table.
Here is an example of that:
-- Duplicate the definition of your table.
CREATE TEMP TABLE my_table_stage (LIKE my_table INCLUDING DEFAULTS);
-- Your COPY statment
COPY my_table FROM 'my_file.csv' ...
-- Insert any "new" records
INSERT INTO my_table (key_field, data_field1, data_field2)
SELECT
stg.key_field,
stg.data_field1,
stg.data_field2
FROM
my_table_stage stg
WHERE
NOT EXISTS (SELECT 1 FROM my_table WHERE key_field = stg.key_field);
-- Update any existing records
UPDATE my_table orig
SET
data_field1 = stg.data_field1,
data_field2 = stg.data_field2
FROM
my_table_stage stg
WHERE
orig.key_field = stg.keyfield;

Can't insert into a temporary table

I have connected to my new DB2 database via JDBC and am trying to create and insert a temporary table:
String createTemporaryTable = "declare global temporary table temporary_table (RECORD smallint) ON COMMIT PRESERVE ROWS in TEMPTABLESPACE";
statement.execute(createTemporaryTable);
String insert = "INSERT INTO temporary_table (RECORD) VALUES (1)";
statement.execute(insert);
connection.commit();
This results in
"DB2 SQL Error: SQLCODE=-204, SQLSTATE=42704"
Creating a normal table and inserting this way results in no errors. Is there something I need to configure?
You must refer to the temporary table as being in the schema session. Try the insert like:
String insert = "INSERT INTO session.temporary_table (RECORD) VALUES (1)";
session is implicit when declaring the table, but for clarity I usually declare it as:
declare global temporary table session.temporary_table (...

ORA-01461 with MERGE INTO statement and clob column

The query:
MERGE INTO app_role_data x USING (select ? name, ? xml FROM dual) d ON (x.app_name = d.name) WHEN MATCHED THEN UPDATE SET x.xml_blob = d.xml WHEN NOT MATCHED THEN INSERT(app_name, xml_blob) VALUES(d.name, d.xml)
The table:
create table app_role_data(app_name varchar2(64), xml_blob clob);
The result: When a row exists, we get ORA-01461.
The desired goal: This is a "create or replace" operation on a row in this table, effectively. If 'name' exists in the table, then the xml column should be updated, else a new row inserted.
I think this turns out to be solved by using the Spring JDBC LOB setting functionality documented at 11.7.2 of the Spring Framework documentation.
However, that isn't working either... but will be the subject of another question.

Resources