Facing a issue while using the MERGE STATEMENT by using WEHN NOT MATCHED BY TARGET clause - oracle

By using HR schema, i have created backup table (EMPLOYEES_BKP) using EMPLOYEES table. when i am using MERGE statement, i am getting the below error..
MERGE INTO EMPLOYEES_BKP BKP
USING EMPLOYEES EMP
ON (BKP.EMPLOYEE_ID=EMP.EMPLOYEE_ID)
WHEN MATCHED THEN
UPDATE SET BKP.SALARY=EMP.SALARY
WHEN NOT MATCHED BY TARGET THEN
INSERT (BKP.EMPLOYEE_ID, BKP.FIRST_NAME,BKP.LAST_NAME, BKP.EMAIL, BKP.PHONE_NUMBER, BKP.HIRE_DATE, BKP.JOB_ID, BKP.SALARY, BKP.COMMISSION_PCT, BKP.MANAGER_ID, BKP.DEPARTMENT_ID)
VALUES (EMP.EMPLOYEE_ID, EMP.FIRST_NAME,EMP.LAST_NAME, EMP.EMAIL, EMP.PHONE_NUMBER, EMP.HIRE_DATE, EMP.JOB_ID, EMP.SALARY, EMP.COMMISSION_PCT, EMP.MANAGER_ID, EMP.DEPARTMENT_ID)
WHEN NOT MATCHED BY SOURCE
DELETE;
Error at Command Line : 15 Column : 18
Error report -
SQL Error: ORA-00905: missing keyword
00905. 00000 - "missing keyword"

Related

Oracle SQL Error occurred in XML processing for a query

Hi I am trying to run this query but getting this error
Query 1
select OWNER,table_name,
to_number(extractvalue(xmltype(dbms_xmlgen.getxml('select count(*) c from '||owner||'.'||table_name)),'/ROWSET/ROW/C')) as count
from all_tables
Runs fine
Query 2 - Included additional columns (sample_size, last_analyzed)
select OWNER,table_name,
to_number(extractvalue(xmltype(dbms_xmlgen.getxml('select count(*) c from '||owner||'.'||table_name)),'/ROWSET/ROW/C')) as count,
sample_size, last_analyzed
from all_tables
Error - ?
ORA-19202: Error occurred in XML processing
ORA-00933: SQL command not properly ended
ORA-06512: at "SYS.DBMS_XMLGEN", line 176
ORA-06512: at line 1
19202. 00000 - "Error occurred in XML processing%s"
*Cause: An error occurred when processing the XML function
*Action: Check the given error message and fix the appropriate problem
Query 2 - Issue what could be the issue ? - Explanation and resolution

Merge statement issue - error unable to get a stable set of rows in the source tables

Hi I am getting error while running the below merge statement in oracle db , can you please let me know how to fix the below error ?
--Query
MERGE INTO d_prod_fld dp USING
(SELECT stg_prod_fld_id,
prod_cd_id,
country_name
FROM stg_prod_fld_delta pd
LEFT OUTER JOIN d_loc dl ON (dl.prod_cd_num=lpad(pd.prod_cd_id, 3, '0'))
WHERE pd.efft_to > trunc(sysdate+1)
AND pd.prod_cd_id IS NOT NULL ) stg
ON (dp.cd_id=stg.stg_prod_fld_id)
WHEN matched THEN
UPDATE
SET dl.prod_country=stg.country_name;
d_prod_fld - target dimension table ,
stg_prod_fld_delta - stage table ,
d_loc - look up table
basically when i tried to run the above query in sandbox it is running fine ,but when i tried to run in actual development environment it is showing the above error -
Error starting at line : 1 in command -
Error report -
SQL Error: ORA-30926: unable to get a stable set of rows in the source tables
30926. 00000 - "unable to get a stable set of rows in the source tables"
*Cause: A stable set of rows could not be got because of large dml
activity or a non-deterministic where clause.
*Action: Remove any non-deterministic where clauses and reissue the dml.
This means parallel DML's are happening in the source table stg_prod_fld_delta or lookup table d_loc which are modifying the results of the SELECT query.
You need to acquire a lock on stg_prod_fld_delta and d_loc using select * from table where condition =value for update no wait before running the MERGE statement .
Also issue COMMIT after the MERGE statement.
Please check the below
SELECT *
FROM stg_prod_fld_delta pd
WHERE pd.efft_to > trunc(sysdate+1)
AND pd.prod_cd_id IS NOT NULL
for update no wait;
select * from d_loc dl
for update no wait;
MERGE INTO d_prod_fld dp USING
(SELECT stg_prod_fld_id,
prod_cd_id,
country_name
FROM stg_prod_fld_delta pd
LEFT OUTER JOIN d_loc dl ON (dl.prod_cd_num=lpad(pd.prod_cd_id, 3,
'0'))
WHERE pd.efft_to > trunc(sysdate+1)
AND pd.prod_cd_id IS NOT NULL ) stg ON (dp.cd_id=stg.stg_prod_fld_id)
WHEN matched THEN
UPDATE
SET dl.prod_country=stg.country_name;
COMMIT;

Error in Hive : For Exists/Not Exists operator SubQuery must be Correlated

select * from students1;
students1.name students1.age students1.gpa
fred 35 1.28
barney 32 2.32
shyam 32 2.32
select * from students2;
students1.name students1.age
fred 35
barney 32
When I am running this query
select
name,age from students1
where not exists
(select name,age from students2);
I am getting this bellow error
Error while compiling statement: FAILED: SemanticException line 39:22
Invalid SubQuery expression 'age' in definition of SubQuery sq_1 [
exists (select name,age from students2) ] used as sq_1 at Line 3:10:
For Exists/Not Exists operator SubQuery must be Correlated.
The error message is clear. The subquery should be correlated when using exists/not exists.
select name,age
from students1 s1
where not exists (select 1
from students2 s2
where s1.name=s2.name and s1.age=s2.age
)
You are trying to achieve a MINUS output of a query. It is unfortunately not available in Hive.
You can read through the limitations of HQL and SQL here. HQL vs SQL
For usage of not exists, the manual has good example.
subqueries in hive
MINUS is now available in Hive. You can achieve this as the following:
select name,age from students1
MINUS
select name,age from students2;

Getting Unknown Command error on IF-THEN-ELSE

I have the following query that I am using in Oracle 11g
IF EXISTS (SELECT * FROM EMPLOYEE_MASTER WHERE EMPID='ABCD32643')
THEN
update EMPLOYEE_MASTER set EMPID='A62352',EMPNAME='JOHN DOE',EMPTYPE='1' where EMPID='ABCD32643' ;
ELSE
insert into EMPLOYEE_MASTER(EMPID,EMPNAME,EMPTYPE) values('A62352','JOHN DOE','1') ;
END IF;
On running the statement I get the following output:
Error starting at line : 4 in command -
ELSE
Error report -
Unknown Command
1 row inserted.
Error starting at line : 6 in command -
END IF
Error report -
Unknown Command
The values get inserted with error when I run it directly. But when I try to execute this query through my application I get an oracle exception because of the error generated :
ORA-00900: invalid SQL statement
And hence the values are not inserted.
I am relatively new to Oracle. Please advise on what's wrong with the above query so that I could run this query error free.
If MERGE doesn't work for you, try the following:
begin
update EMPLOYEE_MASTER set EMPID='A62352',EMPNAME='JOHN DOE',EMPTYPE='1'
where EMPID='ABCD32643' ;
if SQL%ROWCOUNT=0 then
insert into EMPLOYEE_MASTER(EMPID,EMPNAME,EMPTYPE)
values('A62352','JOHN DOE','1') ;
end if;
end;
Here you you the update on spec, then check whether or not you found a matching row, and insert in case you didn't.
"what's wrong with the above query "
What's wrong with the query is that it is not a query (SQL). It should be a program snippet (PL/SQL) but it isn't written as PL/SQL block, framed by BEGIN and END; keywords.
But turning it into an anonymous PL/SQL block won't help. Oracle PL/SQL does not support IF EXISTS (select ... syntax.
Fortunately Oracle SQL does support MERGE statement which does the same thing as your code, with less typing.
merge into EMPLOYEE_MASTER em
using ( select 'A62352' as empid,
'JOHN DOE' as empname,
'1' as emptype
from dual ) q
on (q.empid = em.empid)
when not matched then
insert (EMPID,EMPNAME,EMPTYPE)
values (q.empid, q.empname, q.emptype)
when matched then
update
set em.empname = q.empname, em.emptype = q.emptype
/
Except that you're trying to update empid as well. That's not supported in MERGE. Why would you want to change the primary key?
"Does this query need me to add values to all columns in the table? "
The INSERT can have all the columns in the table. The UPDATE cannot change the columns used in the ON clause (usually the primary key) because that's a limitation of the way MERGE works. I think it's the same key preservation mechanism we see when updating views. Find out more.

Sybase query to delete duplicates does not work on oracle 11g: using joins only

I used a query to delete duplicate rows in sybase.
It worked; However when I used similar query for practice on oracle and it did not work;
Please advise
Sybase query that worked:
--delete all the duplicate rdb_id where row_id is greater than the min row_id
delete ratings_xref..xref_ratg_obj_id
from ratings_xref..xref_ratg_obj_id a, ratings_xref..xref_ratg_obj_id b
where a.rdb_id = b.rdb_id
and a.row_id > b.row_id
go
However, the below code did not work in oracle :
delete dup6
from dup6 d6,dup6 d61
where d6.rowid>d61.rowid
and d6.i=d6i.i;
Same with update :
Below sybase code works :
update ratings_xref..xref_ratg_obj_id
set a.last_chg_usr=suser_name(),
a.last_chg_dt=getdate()
from ratings_xref..xref_ratg_obj_id a, ratings_xref..xref_ratg_obj_id b
where a.rdb_id = b.rdb_id
and a.row_id > b.row_id
Whereas the oracle code does not work
update dupli
set bb.chg_dt=sysdate-360
from dupli bb,dupli b
where bb.i=b.i
and bb.rowid>b.rowid;
The error received while deleting using the oracle query was :
Error report -
SQL Error: ORA-00933: SQL command not properly ended
00933. 00000 - "SQL command not properly ended"

Resources