MERGE using Rowid Oracle Conversion in Postgres - oracle

Can some help me the best way convert the below Oracle code to Postgres
MERGE INTO INCLUSION_EXCLUSION_ALL A
USING (SELECT DISTINCT IEALL.rowid rid, IE.BLACKOUT_DURATION, ie.BLACKOUT_CATEGORY
FROM INCLUSION_EXCLUSION_ALL IEALL, INCLUSION_EXCLUSION_RULES IE
WHERE IE.PRI_PROMOTION_ID = IEALL.PRI_PROMOTION_ID
AND IE.PRI_OFFER_ID = IEALL.PRI_OFFER_ID
AND IE.SEC_PROMOTION_ID = IEALL.SEC_PROMOTION_ID
AND IE.SEC_OFFER_ID = IEALL.SEC_OFFER_ID
AND IE.BLACKOUT_DURATION > 0
AND (IEALL.BLACKOUT_DURATION <> IE.BLACKOUT_DURATION OR IEALL.BLACKOUT_CATEGORY <> IE.BLACKOUT_CATEGORY)
--AND IE.INCL_EXCL_IND <> 'I'
AND IE.SECONDARY_TYPE = 'B') B
ON (A.rowid = B.rid)
WHEN MATCHED THEN UPDATE SET A.BLACKOUT_DURATION = B.BLACKOUT_DURATION, A.BLACKOUT_CATEGORY = B.BLACKOUT_CATEGORY;
Thank You,
I want to know different ways on how we can do it as the rowid's are involved in here and are not compatible with Postgres esp in Joining using Alias even by using CTID,
Please advise

Related

MERGE into getting stuck all of a sudden

I have a stored procedure that is getting stuck on the merge into all of a sudden but has been working fine for years. Any advice ? The SP is in Oracle SQL developer and it is being executed in a Visual Studios SSIS package
Query below:
MERGE INTO CORE_APPT C
USING (
SELECT DISTINCT
TST_TKR_ID,
TST_TKR_ID_SRC,
APPT_ID_SRC,
APPT_CRETN_DTE,
APNTMT_DT,
ETS_APNTMT_ID,
ETS_TST_CNTR,
TST_STS_TYP_DESC,
APPT_STS_TYP,
TST_DLVR_MTHD_TYP_CDE,
DLVR_GRP_TYP,
TST_LNCH_TM,
TRNSCTN_ID,
TST_PKG_ID,
RND_TRIP_TST_FLG,
UPDT_DTE_TM,
PROD_ID,
ACTN_DTE_TM,
TST_DURN,
BRKRG_APPT_UPDT_DTM,
TST_APPT_TYP_CDE,
CNCL_MKUP_RSN_TYP,
DLNQNT_HLD_FLG,
TST_TKR_TST_UPDT_DTE_TM,
SCR_HLD_LVL_TYP_CDE,
SCR_CNCL_FLG,
ETS_CANDTE_SCR_HLD_FLG,
TST_CNTR_HLD_FLG,
TST_ADM_HLD_FLG,
ETS_SCR_CNCL_FLG,
TST_ADM_HLD_RSN_CDE,
TST_CNTR_HLD_RSN_CDE,
ETS_CANDTE_SCR_HLD_RSN_CDE,
ETS_SCR_CNCL_RSN_CDE,
DLNQNT_HLD_RSN_CDE,
TST_TKR_SCR_STS_TYP_CDE,
TST_TKR_SCR_STS_RSN_TYP_CDE,
PYMT_NO,
DATA_SRCE AS DATA_SRCE,
CURRENT_FLAG,
PRGRM_TYP,
sysdate as RCD_CRTD_DTE,
user as RCD_CRTD_USER,
REG_SYS_ID AS REG_SYS_ID,
TST_NAM AS TST_NAM,
CNTRY_SETL_RSP AS CNTRY_SETL_RSP
FROM STG_CMMN_APPT A
WHERE A.CURRENT_FLAG = 'Y'
) S
ON (C.APPT_ID_SRC = S.APPT_ID_SRC
AND C.REG_SYS_ID = S.REG_SYS_ID
AND C.DATA_SRCE = S.DATA_SRCE) -- added in EREG4.0 changes
WHEN MATCHED THEN UPDATE
SET C.CURRENT_FLAG = 'N'
WHERE C.CURRENT_FLAG = 'Y'
WHEN NOT MATCHED THEN INSERT
(
C.TST_TKR_ID,
C.TST_TKR_ID_SRC,
C.APPT_ID_SRC,
C.APPT_CRETN_DTE,
C.APNTMT_DT,
C.ETS_APNTMT_ID,
C.TST_CNTR_ID_SRC,
C.TST_STS_TYP_DESC,
C.APPT_STS_TYP,
C.TST_DLVR_MTHD_TYP_CDE,
C.DLVR_GRP_TYP,
C.TST_LNCH_TM,
C.TRNSCTN_ID,
C.TST_PKG_ID,
C.RND_TRIP_TST_FLG,
C.UPDT_DTE_TM,
C.PROD_ID,
C.ACTN_DTE_TM,
C.TST_DURN,
C.BRKRG_APPT_UP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_ADM_HLD_RSN_CDE,
S.TST_CNTR_HLD_RSN_CDE,
S.ETS_CANDTE_SCR_HLD_RSN_CDE,
S.ETS_SCR_CNCL_RSN_CDE,
S.DLNQNT_HLD_RSN_CDE,
S.TST_TKR_SCR_STS_TYP_CDE,
S.TST_TKR_SCR_STS_RSN_TYP_CDE,
S.PYMT_NO,
S.DATA_SRCE,
S.CURRENT_FLAG,
S.PRGRM_TYP,
S.RCD_CRTD_DTE,
S.RCD_CRTD_USER,
S.REG_SYS_ID,
S.TST_NAM,
S.CNTRY_SETL_RSP
)
;
TIA!

Error 71561: Unresolved reference to object

In Visual Studio 2017, SSDT 2015, I am receiving the "unresolved reference to object" error. This error is occurring 2,589 times, so it's not an issue with one particular piece of code. All of the code compiles and runs in SQL Server directly.
I've tried everything from this post, except for disabling "extended Transact-SQL verification for common objects" because that option has been removed.
I've tried pretty much everything else I can find on Google; I've been banging away on this for two days now.
Here is an example of some of the code that's getting the error. It runs fine on SQL Server directly. The error comes up in the FROM clause on each line that contains a database reference. I can't remove those references or change the location of the view. This is also happening in stored procedures that reference the views.
CREATE view [Migration].[vwCHILDS_Allegation_AllegationTrackingCharacteristics]
as
with src as (
select
(select AllegationId from CWNS_Migration.Allegation.Allegation where SourceSystemId = cast(1 as int) and SourceSystemIdentifier = cast(a.IDNBR as nvarchar(64))) AllegationId
, (select TrackingCharacteristicsId from CWNS_Migration.Allegation.TrackingCharacteristics where Code = dfrvmi1.DestinationDataFieldReferenceValueCode) TrackingCharacteristicsId
, (select VerificationStatusId from CWNS_Migration.Allegation.VerificationStatus where Code = dfrvmi2.DestinationDataFieldReferenceValueCode) VerificationStatusId
, cast(1 as int) SourceSystemId
, cast(src.IDNBR as nvarchar(64)) SourceSystemIdentifier
, src.IDNBR SourceSystemIdentifier_Numeric
, case when src.CRET_DT_TM = '0001-01-01' then null else src.CRET_DT_TM end SourceSystemCreatedDateTime
, (
select
max(pe.PersonId)
from
CWNS_Migration.PersonIdentity.PersonIdentifier pe
join CHILDSDB2.VLCHA.STAFF_USERID st on cast(st.FK_STAFFFK_PERSID as nvarchar(64)) = pe.Identifier
and pe.PersonIdentificationSystemId = 4
where
st.USERID = ltrim(rtrim(src.CRET_USER_ID))) SourceSystemCreatedPersonId
from
CHILDSDB2.VLCHA.RPT_TRKNG_CHAR src
join CHILDSDB2.VLCHA.ALGTN a on a.FK_RPTRPT_NBR = src.FK_RPTRPT_NBR
join CHILDSDB2.VLCHA.FINDING f on f.FK_ALGTNID = a.IDNBR
join DataCatalog.dbo.DataFieldReferenceValueMappingInfo dfrvmi1 on dfrvmi1.SourceDataFieldReferenceValueDataFieldId = 5438
and dfrvmi1.SourceDataFieldReferenceValueCode = src.FK_TRKNG_CHAR_TCD
and dfrvmi1.DestinationDataFieldReferenceValueDataFieldId = 20586
join DataCatalog.dbo.DataFieldReferenceValueMappingInfo dfrvmi2 on dfrvmi1.SourceDataFieldReferenceValueDataFieldId = 1775
and dfrvmi2.SourceDataFieldReferenceValueCode = f.FINDING_DET_CD
and dfrvmi2.DestinationDataFieldReferenceValueDataFieldId = 55983
)
select
src.*
from
src
left join CWNS_Migration.Allegation.AllegationTrackingCharacteristics tgt on tgt.SourceSystemId = src.SourceSystemId and tgt.SourceSystemIdentifier = src.SourceSystemIdentifier
left join CWNS_Migration.Quarantine.AllegationTrackingCharacteristics q on q.SourceSystemId = src.SourceSystemId and q.SourceSystemIdentifier = src.SourceSystemIdentifier
and q.QExecutionId = 1
where
q.QExecutionId is null
and (
isnull(src.AllegationId, 0) <> isnull(tgt.AllegationId, 0)
or isnull(src.TrackingCharacteristicsId, 0) <> isnull(tgt.TrackingCharacteristicsId, 0)
or isnull(src.VerificationStatusId, 0) <> isnull(tgt.VerificationStatusId, 0)
or try_cast(isnull(src.SourceSystemCreatedDateTime, '') as datetime) <> isnull(tgt.SourceSystemCreatedDateTime, '')
or isnull(src.SourceSystemCreatedPersonId, '') <> isnull(tgt.SourceSystemCreatedPersonId, '')
);
Add a Database reference to your project:
You will need to refactor your code to use:
[$(*ReferencedDatabaseName*)].schemaName.tableName

Divisor is equal to zero, Need guidance with case statement

This is my query & I'm getting error divisor is equal to zero, I know i need to build this as a case statement just tried a few things and cant get it to work, thanks in advance.
NVL(ROUND(((SELECT PC.BUCKET_ACCUM_COST
FROM PART_CB PC WHERE
PART_CB_NO = '201'
AND PC.PART_NO = I.PART_NO
AND
PC.CONTRACT = P.CONTRACT
AND
PC.TOP_LEVEL_PART_NO || '' =
Z_BEL_FINANCE_API.GET_PART_COST_TOP_PART_NO(P.CONTRACT, P.PART_NO,
P.COST_SET, P.ALTERNATIVE_NO,
P.ROUTING_ALTERNATIVE_NO)
AND
PC.COST_SET = P.COST_SET
AND
PC.COST_BUCKET_ID != 'SYS'
AND
PC.TOP_ALTERNATIVE_NO =
Z_BEL_FINANCE_API.GET_PART_COST_TOP_ALT_NO(P.CONTRACT, P.PART_NO,
P.COST_SET, P.ALTERNATIVE_NO,
P.ROUTING_ALTERNATIVE_NO)
AND
PC.TOP_ROUTING_NO =
Z_BEL_FINANCE_API.GET_PART_COST_TOP_ROUTING_NO(P.CONTRACT, P.PART_NO,
P.COST_SET, P.ALTERNATIVE_NO,
P.ROUTING_ALTERNATIVE_NO)
AND
PC.BUCKET_SEQ = Z_BEL_FINANCE_API.GET_PART_COST_BUCKET_SEQ(P.CONTRACT,
P.PART_NO, P.COST_SET, P.ALTERNATIVE_NO,
P.ROUTING_ALTERNATIVE_NO)) /
(SELECT WC_RATE
FROM WCT
WHERE WORK_CENTER_NO = 'COST1'
AND COST_SET = '1'
AND CONTRACT = P.CONTRACT)), 4), 0) MACHINE_SETUP_TIME,
The only dividing in this mess is here:
/ (SELECT WC_RATE FROM WCT ...)
If you don't want to divide with zero, you'll have to handle it.
For example, use DECODE (or CASE) and - if you want to get 0 as the result, divide with a very large number (e.g. 1E99)

Is MERGE supported in Greenplum Database 4.3.5.1 build 1

I am trying to create a merge statement for Greenplum DB and I am getting an syntax error. So I am wondering if the MERGE is even supported the way I am writing it.
I have two approaches
Approach 1-
MERGE into public.table20 pritab
USING
(
select stgout.key1, stgout.key2, stgout.col1
from public.table20_stage stgout
where stgout.sequence_id < 1000
) as stgtab
ON (pritab.key1 = stgtab.key1
and pritab.key2 = stgtab.key2)
WHEN MATCHED THEN
UPDATE SET pritab.key1 = stgtab.key1
,pritab.key2 = stgtab.key2
,pritab.col1 = stgtab.col1
WHEN NOT MATCHED THEN
INSERT (key1, key2, col1)
values (stgtab.key1, stgtab.key2, stgtab.col1);
Approach 2:
public.table20 pritab
SET pritab.key1 = stgtab.key1
,pritab.key2 = stgtab.key2
,pritab.col1 = stgtab.col1
from
(
select stgout.key1, stgout.key2, stgout.col1
from public.table20_stage stgout
where stgout.sequence_id < 1000
) as stgtab
ON (pritab.key1 = stgtab.key1
and pritab.key2 = stgtab.key2)
returning (stgtab.key1, stgtab.key2, stgtab.col1);
Is there any other way or something is wrong with my syntax itself?
Merge is not supported in Greenplum but I wrote a blog post on how to achieve the results of a merge statement in Greenplum.
http://www.pivotalguru.com/?p=104

1 result set from 3 queries?

I'm at my wits end attempting to make this happen. Currently I have 3 separate queries. For automation purposes I need to get the results of these three queries in one output, I cannot seem to join them properly to get the expected results.
QUERY 1:
SELECT OH.EXTN_HOST_ORDER_REF,
OL.EXTN_HOST_ORDER_LINE_REF,
OL.ORIGINAL_ORDERED_QTY,
OL.EXTN_TENDER_QUANTITY,
OL.EXTN_CUM_PICK_QTY,
OL.SHIPPED_QUANTITY,
OL.EXTN_REFUND_QTY
FROM YFS_ORDER_HEADER OH,
YFS_ORDER_LINE OL
WHERE OH.ORDER_HEADER_KEY = OL.ORDER_HEADER_KEY
AND OH.DOCUMENT_TYPE = '0001'
AND OH.EXTN_HOST_ORDER_REF = 'xxxxxxxxxxx'
ORDER BY PL.EXTN_HOST_ORDER_LINE_REF ASC;
QUERY 2:
SELECT RS.STATUS_QUANTITY AS RETURNED_QTY
FROM YFS_ORDER_HEADER OH,
YFS_ORDER_LINE OL,
YFS_ORDER_RELEASE_STATUS RS
WHERE OH.ORDER_HEADER_KEY = OL.ORDER_HEADER_KEY
AND OL.ORDER_LINE_KEY = RS.ORDER_LINE_KEY
AND RS.STATUS = '3700.02'
AND OH.EXTN_HOST_ORDER_REF = 'xxxxxxxxxxx';
QUERY 3
SELECT RS.STATUS_QUANTITY AS CANCELLED_QTY
FROM YFS_ORDER_HEADER OH,
YFS_ORDER_LINE OL,
YFS_ORDER_RELEASE_STATUS RS
WHERE OH.ORDER_HEADER_KEY = OL.ORDER_HEADER_KEY
AND OL.ORDER_LINE_KEY = RS.ORDER_LINE_KEY
AND RS.STATUS = '9000'
AND OH.EXTN_HOST_ORDER_REF = 'xxxxxxxxxxx';
The query should show NULL values where no data exists from query 2 & 3.
Thanks in advance for your help and advice!
If you are allowed to show both returned and cancelled quantities, the following simple edit should work. Hope this helps.
SELECT oh.extn_host_order_ref,
ol.extn_host_order_line_ref,
ol.original_ordered_qty,
ol.extn_tender_quantity,
ol.extn_cum_pick_qty,
ol.shipped_quantity,
ol.extn_refund_qty,
DECODE (rs.status, '3700.02', rs.status_quantity) AS returned_qty,
DECODE (rs.status, '9000', rs.status_quantity) AS cancelled_qty
FROM yfs_order_header oh
INNER JOIN yfs_order_line ol
ON oh.order_header_key = ol.order_header_key
LEFT OUTER JOIN yfs_order_release_status rs
ON ol.order_line_key = rs.order_line_key
WHERE oh.document_type = '0001' AND oh.extn_host_order_ref = 'xxxxxxxxxxx'
ORDER BY pl.extn_host_order_line_ref ASC;

Resources