What is the right Update Statement in Oracle 11g - oracle

My original update statement is still working great in Sybase (see below) but we're migrating to Oracle 11g and I have to convert this update statement into Oracle Update.
I tried so many version and keep getting error, I guess I have not gotten a grib of how Oracle Update statement works. Can anyone help?
My original good Update Statement (in Sybase):
UPDATE Valid
SET A.status = 'X',
A.reason = 'Missing'
FROM Valid A, Valid B
WHERE A.id_number = B.id_number
AND A.session_id = 69
AND A.userid = 'BS'
AND A.status = 'A'
AND isnull(B.street1, ' ') = ' '
Below is my Oracle version that is not working no matter what I did:
MERGE
INTO um_valid Target
USING (select * from um_valid) SOURCE
ON (t1.id_number = t2.id_number)
WHEN MATCHED THEN
UPDATE
SET status = 'X',
reason = 'Missing (street 1)'
WHERE Target.session_id = 69
AND Target.userid = 'BS'
AND Target.status = 'A'
AND NVL(SOURCE.street1, ' ') = ' ')
The error I'm getting:
ORA-00933: SQL command not properly ended

The WHERE clause at the end isn't appropriate. Maybe you need this:
MERGE
INTO um_valid Target
USING (
select distinct id_number
from um_valid
where street1 is null
) SOURCE
ON (
Target.id_number = SOURCE.id_number
AND Target.session_id = 69
AND Target.userid = 'BS'
AND Target.status = 'A'
)
WHEN MATCHED THEN UPDATE SET
status = 'X',
reason = 'Missing (street 1)'

You can use a normal update statement. Merge is for bulk operations.
If you need to update something when it exists or insert when it doesn't then you can use a merge statement.
So for your example try using the following:
UPDATE Valid
SET status = 'X',
reason = 'Missing'
WHERE session_id = 69
AND userid = 'BS'
AND status = 'A'
AND street1 is null
The above statement will update all rows in table "Valid" where session_id equals 69, userid equals BS, status equals A and street1 is empty (null).
I think this is what you need, if this is not the case pls let me know why you join valid with valid. The I will adjust my update.

This ought to do it:
UPDATE Valid a
SET status = 'X',
reason = 'Missing'
WHERE A.session_id = 69
AND A.userid = 'BS'
AND A.status = 'A'
AND exists (select null
from valid b
where a.id_number = b.id_number
AND coalesce(B.street1, ' ') = ' ')

Related

ora-00933 : oracle update query using join sql command not properly ended

I have a query , when I run the below query in Oracle
update cust_logs el
set batch_id = '3344'
from
client c
join port p on p.pid = c.pid
and
p.flag = 0
where
c.cid = el.cid
and
c.flag = 0
and
p.export = 1
and
el.trans_id is nul
I am getting error as
ORA-00933: sql command not properly ended.
I have tried
merge into cust_logs el
using (
select *
from client c
join port p
on (
p.pid = c.pid
and p.flag = 0
)
) "s"
on ((
c.cid = el.cid
and c.flag = 0
and p.export = 1
and decode(el.trans_id, nul, 1, 0) = 1
))
when matched then update set
batch_id = '3344'
Can you please let me know how to resolve
This is my sql fiddle
http://sqlfiddle.com/#!4/c9166/1
You can't join in an update.
You haven't said what the issue is with your merge attempt. But your on clause referring to the base table instead of the "s" alias. And the select * will cause an ambiguous identifier error as pid appears in both tables. And it's null not nul.
So this might be closer, at least:
merge into cust_logs el
using (
select c.cid, c.flag, p.export
from client c
join port p
on p.pid = c.pid
where p.flag = 0
) s
on (
s.cid = el.cid
and s.flag = 0
and s.export = 1
and decode(el.trans_id, null, 1, 0) = 1
)
when matched then update set batch_id = '3344'
But we don't have your tables or data to verify it.
If batch_id is a numeric column then the last line should be:
when matched then update set batch_id = 3344

ORA-01427: single-row Subquery Returns More Than One Row for Update

Hi I am trying to execute but getting error single row subquery returns more than one row.
update upld_mktprice
set (upld_mktprice.orig_security, upld_mktprice.stk_exch, upld_mktprice.stk_group, upld_mktprice.instr_type) =
(select security.security,nvl(upld_mktprice.stk_exch,
nvl(security.stk_exch,'DIRECT')),
security.stk_group,
decode(upld_mktprice.source,'FOREX','X','S') as instr_type -- HLAMUAT-1457146: Passing DIRECT as a default value for HLAM. confirmed by Dheeren/Prasanth.
from v_security_all security
where upld_mktprice.security = Decode(security.asset_type,'OPT',security.stk_sec_id||' '||substr(security.security,instrb(security.security, ' ', 1,1)+3, length(security.security)),security.stk_sec_id)
and rectype = 'L'),
upld_mktprice.currency = nvl(upld_mktprice.currency,''),
upld_mktprice.value_date = nvl(upld_mktprice.value_date,''),
upld_mktprice.amc_code = 'AMC'
Where exists (select 1
from v_security_all security
where upld_mktprice.security = Decode(security.asset_type,'OPT',security.stk_sec_id||' '||substr(security.security,instrb(security.security, ' ', 1,1)+3, length(security.security)),security.stk_sec_id)
and rectype = 'L')
and upld_mktprice.orig_security is null
and upld_mktprice.user_id = 'SRINIVAS'
and upld_mktprice.source = 'MKTPRICEMAN';
I think the error pretty much speaks for itself. The portion of your query below is returning more than one row. There is no way for us to validate because we do not have the data that you have. It might also be a bit easier to troubleshoot if you used table aliases in your update statement.
(SELECT security.security,
NVL (upld_mktprice.stk_exch, NVL (security.stk_exch, 'DIRECT')),
security.stk_group,
DECODE (upld_mktprice.source, 'FOREX', 'X', 'S') AS instr_type -- HLAMUAT-1457146: Passing DIRECT as a default value for HLAM. confirmed by Dheeren/Prasanth.
FROM v_security_all security
WHERE upld_mktprice.security =
DECODE (security.asset_type,
'OPT', security.stk_sec_id
|| ' '
|| SUBSTR (security.security,
INSTRB (security.security,
' ',
1,
1)
+ 3,
LENGTH (security.security)),
security.stk_sec_id)
AND rectype = 'L')

How to improve the performance of the SQL query?

I have Sql Queries where in due to which it's effecting the performance of the package.
ln_trans_type_id is the variable declared.
UPDATE invoice_table xai
SET process_flag = 'E',
error_description = 'Invoice Number Does not Exists '
WHERE xai.process_flag = 'N'
AND NOT EXISTS (
SELECT 1
FROM ra_customer_trx_all rct
WHERE rct.org_id = 1001
AND rct.trx_number = xai.invoice_number
AND rct.cust_trx_type_id = ln_trans_type_id
);
kindly please review and advise.
Without an execution plan we can only guess. You should compare the performance with NOT IN but make sure the subquery doesn't return any NULL values, otherwise you won't get any hits.
UPDATE invoice_table xai
SET process_flag = 'E',
error_description = 'Invoice Number Does not Exists '
WHERE xai.process_flag = 'N'
AND xai.invoice_number
NOT IN (
SELECT rct.trx_number
FROM ra_customer_trx_all rct
WHERE rct.org_id = 2326
AND rct.trx_number is not null -- important!
AND rct.cust_trx_type_id = ln_trans_type_id
);
If you inside a package declare a variable (as tempVar) and pass later to update
SELECT 1 into tempVar FROM
ra_customer_trx_all rct , invoice_table xai
WHERE rct.org_id = 1001 AND rct.trx_number = xai.invoice_number AND rct.cust_trx_type_id = ln_trans_type_id

Update Empty Table with Records from a View

I am trying to update an empty table I just created ([LA_Temp].[dbo].[LA_MCO_EQR_MED_201612_20171116] t1) with records from a view ([LA_Temp].[dbo].[vClaim] t2). I know the update statement isn't working because of the join. Since t1 is a new (empty) table, I won't return any results because of the join. What is the best way to accomplish this task? I know I am missing something simple.
Update [LA_Temp].[dbo].[LA_MCO_EQR_MED_201612_20171116] --empty table
Set [Claim_Sys_ICN] = t2.ClaimID,
Claim_Line_Number = ClaimLine,
MCO_Claim_Status = Case When ClaimStatus = 'PAID' Then 'P'
When ClaimStatus = 'ADJUCATED' Then 'A'
When ClaimStatus = 'DENIED' Then 'D'
When ClaimStatus = 'REVERSED' Then 'R'
When ClaimStatus = 'VOID' Then 'V' End,
Patient_Account_Number = CarrierMemID,
MCO_Paid_Date = PaidDate,
Paid_Provider_NPI = PayToNPI,
Procedure_Code = ProcCode,
Procedure_Code_Modifier_1 = Modifier,
Procedure_Code_Modifier_2 = Modifier2,
Procedure_Code_Modifier_3 = Modifier3,
Procedure_Code_Modifier_4 = Modifier4,
Service_Provider_NPI = RenderingNPI,
HDR_Clm_Paid_Amount = AmountPaid
FROM [LA_Temp].[dbo].[LA_MCO_EQR_MED_201612_20171116] t1 --empty table
LEFT JOIN [LA_Temp].[dbo].[vClaim] t2 on --need records from this view
t1.[Claim_Sys_ICN] = t2.ClaimID
Where PaidDate Between '12/1/2016' and '12/31/2016'
Same idea using INSERT INTO...
INSERT INTO [LA_Temp].[dbo].[LA_MCO_EQR_MED_201612_20171116]
([Claim_Sys_ICN], Claim_Line_Number,
MCO_Claim_Status, Patient_Account_Number, MCO_Paid_Date, Paid_Provider_NPI,
Procedure_Code, Procedure_Code_Modifier_1, Procedure_Code_Modifier_2,
Procedure_Code_Modifier_3, Procedure_Code_Modifier_4,
Service_Provider_NPI, HDR_Clm_Paid_Amount)
Select ClaimID, ClaimLine, Case When ClaimStatus = 'PAID' Then 'P'
When ClaimStatus = 'ADJUCATED' Then 'A'
When ClaimStatus = 'DENIED' Then 'D'
When ClaimStatus = 'REVERSED' Then 'R'
When ClaimStatus = 'VOID' Then 'V' End,
CarrierMemID, PaidDate, PayToNPI, ProcCode, Modifier, Modifier2, Modifier3,
Modifier4, RenderingNPI, AmountPaid
FROM [LA_Temp].[dbo].[vClaim]
Where PaidDate Between '12/1/2016' and '12/31/2016'
Do an INSERT instead of an UPDATE.

when i run the procedure i get this error [Err] ORA-24344: success with compilation error missing keyword

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 );

Resources