I need to retrieve datas from two tables based on the Job code which is given in the input xml in IIB/WMB.
SQL Query
SELECT D.DEPTNO,D.DNAME,D.LOC,E.EMPNO,E.ENAME,E.JOB,E.SAL FROM DEPT D JOIN EMP E on E.JOB ='MANAGER' OR E.JOB = 'CLERK'
ESQL Query
Getting the JOB details from input xml and assigning it to local variable.
SET type1 = InputRoot.XMLNSC.Employee.Type[1];
SET type2 = InputRoot.XMLNSC.Employee.Type[2];
Now I need to pass the above variables to my ESQL query.
SET Outputroot.XMLNSC.List[] = PASSTHRU('SELECT D.DEPTNO,D.DNAME,D.LOC,E.EMPNO,E.ENAME,E.JOB,E.SAL FROM Database.EMP AS E,Database.DEPT AS D where E.JOB =?' VALUES('type1','type2')) ;
But the above query is not working.
There are two problems in this query:
First, you are passing two parameters but only one question mark (?) exists in the query.
Second, you are passing a string value like 'type1' and the correct is to pass the variable:
SET Outputroot.XMLNSC.List[] = PASSTHRU('SELECT D.DEPTNO,D.DNAME,D.LOC,E.EMPNO,E.ENAME,E.JOB,E.SAL FROM Database.EMP AS E,Database.DEPT AS D where E.JOB =?' VALUES(type1)) ;
Here is the solution:
DECLARE designation char;
DECLARE designation1 char;
DECLARE Query char;
FOR source AS InputRoot.XMLNSC.Employees.Designation[] DO
IF(source = '') THEN
SET designation = 'Empty';
ELSE
SET designation = designation1 ||''''||UPPER(source)||'''';
SET designation1 = ''''||UPPER(source)||''''||','||designation1 ;
END IF;
SET Query = '(SELECT D.DEPTNO,D.DNAME,D.LOC,E.EMPNO,E.ENAME,E.JOB,E.SAL FROM EMP AS E JOIN DEPT AS D on D.DEPTNO = E.DEPTNO where E.JOB in ('||designation||'))';
SET Outputroot.XMLNSC.List[] = PASSTHRU(Query);
The passthru call should be
SET Outputroot.XMLNSC.List[] = PASSTHRU('SELECT D.DEPTNO,D.DNAME,D.LOC,E.EMPNO,E.ENAME,E.JOB,E.SAL FROM Database.EMP AS E,Database.DEPT AS D where E.JOB in (? , ?)', type1,type2) ;
OR
SET Outputroot.XMLNSC.List[] = PASSTHRU('SELECT D.DEPTNO,D.DNAME,D.LOC,E.EMPNO,E.ENAME,E.JOB,E.SAL FROM Database.EMP AS E,Database.DEPT AS D where E.JOB=? or E.JOB=?)', type1,type2) ;
Add To Database.DBName after your PASSTHRU statement where DBName is your database name
Like this:
SET Outputroot.XMLNSC.List[] = PASSTHRU('SELECT D.DEPTNO,D.DNAME,D.LOC,E.EMPNO,E.ENAME,E.JOB,E.SAL FROM Database.EMP AS E,Database.DEPT AS D where E.JOB =?' VALUES(type1)) To Database.DBName ;
Related
I need some help with this query. It works fine in SSMS, but will not work in Oracle. How can I rewrite this so it will work in Oracle? I really appreciate the help.
UPDATE reservation_Daily_elements e
SET e.MARKET_CODE = 'PHEE',
e.ORIGIN_OF_BOOKING = 'DESKTOP'
FROM reservation_Daily_elements e
LEFT JOIN reservation_daily_element_name dn
ON e.resv_daily_el_seq = dn.resv_daily_el_seq
WHERE dn.RESV_NAME_ID IN ('3747957');
Commit;
How about MERGE?
merge into reservation_daily_elements e
using (select dn.resv_daily_el_seq
from reservatioin_daily_element_name dn
where dn.resv_name_id = '3747957'
) x
on (e.resv_daily_el_seq = x.resv_daily_el_seq)
when matched then update set
e.market_code = 'PHEE',
e.origin_of_booking = 'DESKTOP';
If it has to be UPDATE, then
update reservation_daily_elements e set
e.market_code = 'PHEE',
e.origin_of_booking = 'DESKTOP'
where exists (select null
from reservation_daily_element_name dn
where dn.resv_daily_el_seq = e.resv_daily_el_seq
and dn.resv_name = '3747957'
);
Btw, oracle supports updatable inline views, so you can use simple update (Select... From t1 join t2 on...) set... .
Also as I see your 'left join' is really 'inner join' because of the predicates in where clause.
I have the below code in SQL Server:
UPDATE RH SET
--SELECT RH.economic_class ,
economic_class = C.economic_class
FROM dbo.tblRecHist RH
JOIN tblComp C ON RH.company_id = C.company_id
WHERE RH.company_id = #CompID
AND RH.rec_date = #RecDate
Which I have converted in Oracle to:
UPDATE tblRecHist
SET economic_class = (Select C.economic_class from tblComp C join tblRecHist
RH on RH.company_id = C.company_id)
WHERE RH.company_id = v_CompID
AND RH.rec_date = v_RecDate;
As this is a part of a big procedure I am unable to execute this independently.
So, my question is are both the logic correct? If not, what could be the possible change?
That logic won't work becuase your subquery will return more than one record. Instead of joining tblRecHist into your subquery, omit it and correlate the subquery with the current row of tblRecHist from the UPDATE.
UPDATE tblRecHist rh
SET economic_class = (Select C.economic_class
from tblComp C
WHERE C.company_id = RH.company_id )
WHERE RH.company_id = v_CompID
AND RH.rec_date = v_RecDate;
This assumes company_id is a unique key in tblComp.
I have update query which returns updated rows ID. Execution time of query is about 90 seconds. When i remove Returning clause, then execution time is 1ms.
Table update_table has 39000 rows.
Query updates 0 rows in this case. When updates 3 rows- execution time is same.
DECLARE
type intTable IS TABLE OF INTEGER;
idCol intTable;
BEGIN
UPDATE
update_table
SET
prop1 = 3, prop2 = NULL
WHERE EXISTS (
SELECT null FROM update_table f
INNER JOIN rel_table1 u ON f.ID= u.ID
INNER JOIN rel_table2 VP ON f.another_ID = VP.another_ID
WHERE (u.prop1 = 3)
AND VP.prop1 = 1
AND (u.prop2 = 75)
AND f.ID = update_table.ID
)
ReTURNING ID BULK COLLECT INTO idCol;
.
.
.
END;
Why returning clause slows down query?
A good part of using Oracle is knowing what is "supposed" to happen and what isn't.
Adding a RETURNING INTO clause is not "supposed" to make your update run more slowly. When something happens that isn't supposed to happen, check Oracle's support site to see whether it is a known bug.
In your case, it looks like you are encountering:
Bug 27131648 - SUB OPTIMAL PLAN ON UPDATE STATEMENT WITH RETURNING INTO
I am not sure if there is a patch, but there is a simple workaround: use the UNNEST hint. In your case, that would be:
UPDATE
update_table
SET
prop1 = 3, prop2 = NULL
WHERE EXISTS (
SELECT /*+ UNNEST */ null FROM update_table f
INNER JOIN rel_table1 u ON f.ID= u.ID
INNER JOIN rel_table2 VP ON f.another_ID = VP.another_ID
WHERE (u.prop1 = 3)
AND VP.prop1 = 1
AND (u.prop2 = 75)
AND f.ID = update_table.ID
)
ReTURNING ID BULK COLLECT INTO idCol;
I would recommend splitting it into two parts, first BULK COLLECT and next FORALL collected ID's, both extremely fast and you'll keep being able to further reference updated ID's from idCol.
DECLARE
type intTable IS TABLE OF INTEGER;
idCol intTable;
BEGIN
SELECT f.id
BULK COLLECT INTO idCol
FROM update_table f
INNER JOIN rel_table1 u ON f.ID= u.ID
INNER JOIN rel_table2 VP ON f.another_ID = VP.another_ID
WHERE (u.prop1 = 3)
AND VP.prop1 = 1
AND (u.prop2 = 75);
FORALL indx IN 1..idCol.COUNT
UPDATE update_table
SET prop1 = 3, prop2 = NULL
WHERE id = idCol(indx);
.
.
.
END;
I hope I helped!
CREATE OR REPLACE
PROCEDURE "UNASSIGN_CUSTOMER_FEATURES"
(CustomerID_Param IN NUMBER, FeatureID_Param IN NUMBER, WalletID_Param IN NUMBER)
AS
BEGIN
DELETE FROM CUSTOMER_EXTRA_FEATURES WHERE FEATURES_ID = FeatureID_Param
AND CUSTOMER_ID = CustomerID_Param;
MERGE INTO CUSTOMER_SERVICE_CONFIG c
USING
(SELECT BUSINESS_SERVICE_CONFIG.ID from BUSINESS_SERVICE_CONFIG join SERVICE_CONFIG_MAP
ON BUSINESS_SERVICE_CONFIG.Business_SERVICE_TYPE = SERVICE_CONFIG_MAP.Service_type_ID
and BUSINESS_SERVICE_CONFIG.ORGANIZATION_ID = WalletID_Param
and BUSINESS_SERVICE_CONFIG.BUSINESSSERVICECATEGORY = 0
and SERVICE_CONFIG_MAP.FEATURES_ID = FeatureID_Param ) ids
ON (c.SERVICE_CONFIG_ID = ids.ID and c.CUSTOMER_ID = CustomerID_Param )
WHEN MATCHED THEN
DELETE WHERE CUSTOMER_ID = CustomerID_Param AND SERVICE_CONFIG_ID = ids.ID;
END;
You are missing an UPDATE statement prior to doing a DELETE
something like
WHEN MATCHED THEN
UPDATE SET <some field> with <some value>
DELETE WHERE CUSTOMER_ID = CustomerID_Param AND SERVICE_CONFIG_ID =ids.ID;
END;
Refer the following url:
Oracle sql merge to insert and delete but not update
Hope that helps!
The syntax diagram for the merge statement shows that you the delete clause is an optional part of the update, not standalone:
You're getting the "ORA-00905: missing keyword" error because you don't have an update. You could put in a dummy update, but it looks like you really want to delete all rows that match, with no updates to other rows or inserts of new rows; which would be simpler as a plain delete, something like:
DELETE FROM CUSTOMER_SERVICE_CONFIG c
WHERE CUSTOMER_ID = CustomerID_Param
AND SERVICE_CONFIG_ID IN (
SELECT BUSINESS_SERVICE_CONFIG.ID from BUSINESS_SERVICE_CONFIG join SERVICE_CONFIG_MAP
ON BUSINESS_SERVICE_CONFIG.Business_SERVICE_TYPE = SERVICE_CONFIG_MAP.Service_type_ID
and BUSINESS_SERVICE_CONFIG.ORGANIZATION_ID = WalletID_Param
and BUSINESS_SERVICE_CONFIG.BUSINESSSERVICECATEGORY = 0
and SERVICE_CONFIG_MAP.FEATURES_ID = FeatureID_Param );
or
DELETE FROM CUSTOMER_SERVICE_CONFIG c
WHERE CUSTOMER_ID = CustomerID_Param
AND EXISTS (
SELECT null from BUSINESS_SERVICE_CONFIG join SERVICE_CONFIG_MAP
ON BUSINESS_SERVICE_CONFIG.Business_SERVICE_TYPE = SERVICE_CONFIG_MAP.Service_type_ID
and BUSINESS_SERVICE_CONFIG.ORGANIZATION_ID = WalletID_Param
and BUSINESS_SERVICE_CONFIG.BUSINESSSERVICECATEGORY = 0
and SERVICE_CONFIG_MAP.FEATURES_ID = FeatureID_Param
WHERE BUSINESS_SERVICE_CONFIG.ID = c.SERVICE_CONFIG_ID );
MERGE INTO table_1 a
USING
(SELECT * from table_2) b ON ( a.row_id = b.row_id and a.in_correct IS NULL)
WHEN MATCHED THEN UPDATE SET a.in_correct = 'Y';
In the above query ORA-38104:Column referenced in ON clause cannot be updated.
I have been sitting for hours to resolve this.
I have identified that the problem is the field in_correct.
This field "in_correct" cannot be put in both ON clause and also after SET. But in order satisfy my criteria,I have no option.
Please help me out
MERGE INTO table_1 a USING
(SELECT * from table_2) b
ON ( a.row_id = b.row_id)
WHEN MATCHED THEN UPDATE
SET a.in_correct = NVL(in_correct, 'Y');
UPDATE:
A more "general" command (for non null values):
MERGE INTO table_1 a USING
(SELECT * from table_2) b
ON ( a.row_id = b.row_id)
WHEN MATCHED THEN UPDATE
SET a.in_correct = case
when in_correct = 'valuetobereplaced' then 'Y';
else in_correct;
end;