Error in merging two tables using Oracle10g? - sql

I am using oracle 10g. I tried to merge two tables. At that time i got the following error...
ora-30926 unable to get a stable set of rows in the source tables.
The following is my query
merge into mt_test_dest t
using (select distinct d.dest_id dest_id,
d.c_id,
nvl(tt.destination, tt.destination) destination
from my_dest_extra d
join mt_test_dest tt
on d.c_id = tt.dest_cid
join my_dest dml
on dml.dest_id = d.dest_id
where tt.effectivedate <> to_date('12-12-2999', 'dd-mm-yyyy')) src
on (t.dest_cid = src.c_id)
when matched then
update set t.dest_id = src.dest_id, t.destination = src.destination;
Can someone help me out in this issue?

The most likely cause for the error is that your source query contains multiple rows with the same C_ID. If this happens, you have two or more rows competing to update the same data in the target table (since this column is used as the only join condition). Oracle detects this and throws an ORA-30926 error.

Related

SQL INNER JOIN duplicate columns

I am trying to return some columns from 2 tables which share an ID column using the following browser database query system, which reads from the tables shown on this webpage. I believe the way to do this is by using INNER JOIN (e.g. see this guide).
SELECT sami_dr2.DR2Sample.CATID,
sami_dr2.DR2Sample.Mstar,
sami_dr2.StellarKinematics.PA_STELKIN
FROM sami_dr2.DR2Sample INNER JOIN sami_dr2.StellarKinematics
ON sami_dr2.DR2Sample.CATID = sami_dr2.StellarKinematics.CATID;
However, when I run this query I get the error message:
sql: Duplicate columns are not supported. Try using an alias for those columns within the SELECT clause e.g., SELECT t1.CATAID, t2.CATAID becomes SELECT t1.CATAID as t1_CATAID, t2.CATAID as t2_CATAID
But as far as I'm aware the whole point of using the INNER JOIN is to remove duplications as I'm not returning sami_dr2.StellarKinematics.CATID in my output table, only sami_dr2.DR2Sample.CATID.
I've also found that using SELECT sami_dr2.DR2Sample.CATID as ID_1, sami_dr2.StellarKinematics.CATID as ID_2 in the selction doesn't fix the problem either.
Any help on this would be greatly appreciated!

Convert from JOIN on ROWID in Netezza to RedShift

I'm converting ETL queries written for Netezza to RedShift. I'm facing some issues with ROWID, because it's not supported in RedShift. I have tried using the key columns in the predicates, based on which ROWID is being generated to actually do a workaround. But i'm confused which columns would be used if there are multiple join operations. So is there anyone who can help me convert the query. I even tried to use ROW_NUMBER() over () function, but it also doesn't work because row ids won't be unique for all rows.
Here are the queries from netezza:
Query #1
CREATE TEMP TABLE TMPRY_DELTA_UPD_1000 AS
SELECT
nvl(PT.HOST_CRRNCY_SRRGT_KEY,-1) as HOST_CRRNCY_SRRGT_KEY,
delta1.ROWID ROW_ID
FROM TMPRY_POS_TX_1000 PT
LEFT JOIN TMPRY_TX_CSTMR_1000 TC ON PT.TX_SRRGT_KEY = TC.TX_SRRGT_KEY AND PT.UPDT_TMSTMP > '2017-01-01'
AND PT.INS_TMSTMP < '2017-01-01' AND PT.DVSN_NBR = 70
JOIN INS_EDW_CP.DM_TX_LINE_FCT delta1 ON PT.TX_SRRGT_KEY = delta1.TX_SRRGT_KEY
WHERE
(
delta1.HOST_CRRNCY_SRRGT_KEY <> PT.HOST_CRRNCY_SRRGT_KEY OR
)
AND PT.DVSN_NBR = 70;
Query #2
UPDATE INS_EDW_CP..DM_TX_LINE_FCT base
SET
base.HOST_CRRNCY_SRRGT_KEY = delta1.HOST_CRRNCY_SRRGT_KEY,
)
FROM TMPRY_DELTA_UPD_1000 delta1
WHERE base.ROWID = delta1.ROW_ID;
How can i convert query # 2?
Well, most of the time I have seen joins on rowid it is due to performance optimizations, but in some cases there ARE no unique combination of columns in the table.
Please talk to the people owning these data & run your own analysis of different key combinations and then get back to us.

Trying SQL table update matching on string field

Could really use some help with an update query...(SQL Serer 2008 R2 Express)
I have two tables, tblJP and tblMaster.
I only have a string field that matches between the two tables.
tblJP AND tblMaster
I need to update tblJP.LangString with tblMaster.Long_text when
tblJP.short_text = tblMaster.short_text AND tblMaster.Lang = 'jp'
Any help would be greatly appreciated. I am spinning my wheels trying all sorts of logic and syntax from creating temp tables to other types of joins all with no luck.
A simple update with an INNER JOIN should do the trick.
UPDATE tblJP
SET tblJP.LangString = tblMaster.Long_Text
FROM tblJP
INNER JOIN tblMaster ON tblMaster.alt_text = tblJP.short_text
WHERE tblMaster.Lang = 'jp'
WARNING: Never run an update statement against your production server without first testing it against a development server - especially when someone else wrote the SQL.
You could also use MERGE
MERGE INTO tblJP
USING (SELECT *
FROM tblMaster
WHERE Lang = 'jp') AS SOURCE
ON SOURCE.alt_text = tblJP.short_text
WHEN MATCHED THEN
UPDATE SET LangString = SOURCE.Long_Text;
In the event that the JOIN returns multiple rows you will be alerted to the problem with an error The MERGE statement attempted to UPDATE or DELETE the same row more than once.

Delete entire row across two tables (inner join?)

I'm trying to delete a row of data across two tables. I have the following code which isn't working:
DELETE
FROM PROCESS_OWNER.ARTIFACTS
JOIN PROCESS_OWNER.ARTIFACT_METADATA
ON ARTIFACTS.ARTIFACT_ID = ARTIFACT_METADATA.ARTIFACT_ID
WHERE ARTIFACT_LABEL = 'getTest'
I get the error message:
"SQL command not properly ended"
Would really appreciate some help as I am struggling to get to grips with Oracle.
You can't do it with a join. DELETE FROM has to target a single table. You could do this:
DELETE FROM PROCESS_OWNER.ARTIFACT_METADATA WHERE ARTIFACT_ID = (SELECT ARTIFACT_ID FROM PROCESS_OWNER.ARTIFACTS WHERE ARTIFACT_LABEL = 'getTest');
DELETE FROM PROCESS_OWNER.ARTIFACTS WHERE ARTIFACT_LABEL = 'getTest';

Why won't this merge statement work?

I've spent the better part of the day trying to determine why a merge statement won't work and I'm starting to think the problem must be something a bit exotic.
My database has dozens of PL/SQL procedures that use merge statements but I absolutely cannot get one in particular to work. Although it's much larger than the example shown, I've stripped it down so that it only updates a couple of columns and it still will not compile.
The error is 'ORA-00904 "alias"."column_name" invalid identifier'. This typically means that a column name was mistyped or, in the case of a merge, you are attempting to update a field that's used in a join. This is definately NOT the case. I've quadrupled-checked and the column names are right, they all exist and the format of the statement is exactly the same as what I'm using in many other place.
/**
Result: ORA-00904 "P"."SFDC_CUST_CONTACT_PK": invalid identifier
I'm certain that the table and column names are all correct.
If I join on any of the dozen or so other columns instead, I
get the exact same error.
Note: I'm NOT attempting to update the column that I join
against.
**/
merge into customer_contact c
using (select p.fax_number,
p.email
from sfdc_cust_contact_temp p
) p
on (p.sfdc_cust_contact_pk = c.sfdc_cust_contact_pk)
when matched then
update set
c.fax_number = p.fax_number,
c.email = p.email;
/***
This works fine on the same machine
**/
merge into customer_contact_legacy c
using (select ct.contact_legacy_pk,
ct.fax_number,
ct.email
from customer_contact_temp ct
) ct
on (upper(trim(ct.contact_legacy_pk)) = upper(trim(c.contact_legacy_pk)))
when matched then
update set
c.fax_number = ct.fax_number,
c.email = ct.email;
Any ideas what else could be wrong here? Could there be some type of corruption with the table?
The version is 10g.
It looks like your using clause is missing the column you're trying to join on.
Your code:
merge into customer_contact c
using (select p.fax_number,
p.email
from sfdc_cust_contact_temp p
) p
on (p.sfdc_cust_contact_pk = c.sfdc_cust_contact_pk)
Potential fix:
merge into customer_contact c
using (select p.sfdc_cust_contact_pk,
p.fax_number,
p.email
from sfdc_cust_contact_temp p
) p
on (p.sfdc_cust_contact_pk = c.sfdc_cust_contact_pk)