Join relation query for an Excel File - laravel

Hello i try to display some values from my Eloquent model "Licencies" who are in others tables in relation with an id but i get
Integrity constraint violation: 1052 Column 'id' in on clause is
ambiguous
I'm not sure to make the join method correctly , someone could help me ?
Here my query :
$licencies = Licencies::where('lb_assurance' , '=' , 'Lafont')
->leftJoin('activite_licencie' , 'id' , '=' , 'licencie.activite_licencie_id')
->leftJoin('saisons' , 'id' , '=' , 'licencie.saison_id')
->leftJoin('pays' , 'id' , '=' , 'licencie.pays_naissance_id')
->leftJoin('type_licence' , 'id' , '=' , 'licencie.type_licence_id')
->leftJoin('structures' , 'id' , '=' , 'licencie.structure.id')
->select('num_licence' , 'lb_nom' , 'lb_prenom' , 'dt_naissance' , 'lb_surclassement' , 'lb_assurance' , 'cd_dept_naissance' , 'activite_licencie.lb_activite' , 'saisons.lb_saison' , 'pays.fr' , 'type_licence.lb_type' , 'structures.nom_structure' )->get();

There are column name id in all the related tables so mysql does not know which id you are referring to. To resolve this issue prepend the ids with their respective table names. eg. 'structures.id'
->leftJoin('structures' , 'structures.id' , '=' , 'licencie.structure.id')

Related

I have Oracle Join query that picks data very slow

I have an Oracle join query that picks data very slow. It is like 1000 rows for 7 mins. Please could you help in writing the code in a different way so the data is pulled faster. The next steps for it is using the Select values and dumping the data into MySQL table. I am using Pentaho tool here. Thanks
select
null id,
ss.ILOAN_CODE ,
ss.INST_NUM ,
ss.INST_AMT ,
ss.INST_PRINCIPAL ,
ss.INST_INTEREST ,
ss.BALANCE_PRINCIPAL ,
ss.INST_DUE_DATE ,
ss.PAID_FLAG ,
ss.LATE_FEE ,
ss.PAYMENT_DATE ,
ss.INST_AMT_PAID ,
ss.INST_AMT_DUE ,
ss.REV_CHECK_NUM ,
ss.REV_CHECK_AMT ,
ss.CREATED_BY ,
ss.DATE_CREATED ,
ss.UPDATED_BY ,
ss.DATE_UPDATED ,
ss.INST_DAYS ,
ss.MATURED_INTEREST ,
ss.UNPAID_INTEREST ,
ss.ADJ_INST_PRINCIPAL ,
ss.ADJ_INST_AMT ,
ss.ADJ_INST_INTEREST ,
ss.ADJ_BALANCE_PRINCIPAL ,
ss.ADJ_MATURED_INTEREST ,
ss.ADJ_UNPAID_INTEREST ,
ss.IS_PRINTED ,
ss.RTN_FEE_AMT ,
ss.WAIVE_FEE_AMT ,
ss.LATE_FEE_AMT ,
ss.APR_BALANCE_PRINCIPAL ,
ss.ACHDEPOSIT_DATE ,
ss.ACHRETURN_DATE ,
ss.ACHCLEAR_DATE ,
ss.APR_INST_INTEREST ,
ss.APR_UNPAID_INTEREST ,
ss.CSO_FEE ,
ss.MATURED_CSO_FEE ,
ss.UNPAID_CSO_FEE ,
ss.CSO_FEE_BALANCE
from ST_IL_SCHEDULE ss,
ST_IL_MASTER sm,
BO_MASTER bm
where sm.iloan_code = ss.iloan_code
and sm.bo_code = bm.bo_code
and ss.ILOAN_CODE in (select distinct loan_Number from SVP_LOAN_MASTER_INVENTORY)
and ss.ILOAN_CODE in (select distinct loan_Number from SVP_LOAN_MASTER_INVENTORY)
This is candidate for being slow. You don't need distinct here and also please use explicit join for readability.
Try:
Select
null id,
ss.ILOAN_CODE ,
ss.INST_NUM ,
ss.INST_AMT ,
ss.INST_PRINCIPAL ,
ss.INST_INTEREST ,
ss.BALANCE_PRINCIPAL ,
ss.INST_DUE_DATE ,
ss.PAID_FLAG ,
ss.LATE_FEE ,
ss.PAYMENT_DATE ,
ss.INST_AMT_PAID ,
ss.INST_AMT_DUE ,
ss.REV_CHECK_NUM ,
ss.REV_CHECK_AMT ,
ss.CREATED_BY ,
ss.DATE_CREATED ,
ss.UPDATED_BY ,
ss.DATE_UPDATED ,
ss.INST_DAYS ,
ss.MATURED_INTEREST ,
ss.UNPAID_INTEREST ,
ss.ADJ_INST_PRINCIPAL ,
ss.ADJ_INST_AMT ,
ss.ADJ_INST_INTEREST ,
ss.ADJ_BALANCE_PRINCIPAL ,
ss.ADJ_MATURED_INTEREST ,
ss.ADJ_UNPAID_INTEREST ,
ss.IS_PRINTED ,
ss.RTN_FEE_AMT ,
ss.WAIVE_FEE_AMT ,
ss.LATE_FEE_AMT ,
ss.APR_BALANCE_PRINCIPAL ,
ss.ACHDEPOSIT_DATE ,
ss.ACHRETURN_DATE ,
ss.ACHCLEAR_DATE ,
ss.APR_INST_INTEREST ,
ss.APR_UNPAID_INTEREST ,
ss.CSO_FEE ,
ss.MATURED_CSO_FEE ,
ss.UNPAID_CSO_FEE ,
ss.CSO_FEE_BALANCE
from ST_IL_SCHEDULE ss,
inner join ST_IL_MASTER sm on (sm.iloan_code = ss.iloan_code)
inner join BO_MASTER bm on (sm.bo_code = bm.bo_code)
inner join SVP_LOAN_MASTER_INVENTORY slm on (ss.loan_code = slm.loan number)
If that not helps please consider creating indexes on columns used in join.

Postgresql query is performing really bad

I have a huge issue with query performance at my postgresql database.
The version of postgresql is: "PostgreSQL 8.4.3 on x86_64-unknown-linux-gnu, compiled by GCC gcc (GCC) 4.1.2 20070115 (prerelease) (SUSE Linux), 64-bit"
I have config file set as follows:
shared_buffers = 8GB
effective_cache_size = 24GB
work_mem = 419430kB
maintenance_work_mem = 2GB
checkpoint_segments = 128
checkpoint_completion_target = 0.9
wal_buffers = 16MB
default_statistics_target = 500
constraint_exclusion = on
The query which I need to execute is:
SELECT events.evt_id, events.evt_time, events.device_evt_time , to_ip_char(events.sip) , evt_agent.port , events.rv40 , events.evt , events.msg , events.sun , events.rv35 , events.dun , events.rv45 , events.fn , events.dp , events.trgt_trust_name , events.trgt_trust_domain , events.rv36 , events.rv43 , events.cv21 , events.cv40 , events.cv41 , events.cv42 , events.cv43 , events.cv44 , events.cv50 , events.cv51 , events.cv52 , events.cv53 , events.cv54 , events.cv55 , events.cv56 , events.cv35 , events.cv60 , events.cv61 , events.cv62
FROM events, evt_agent
WHERE
events.agent_id = evt_agent.agent_id AND (evt_agent.port::text = ANY (ARRAY['x'::character varying, 'y'::character varying, 'z'::character varying]::text[]))
AND events.evt::text <> 'Internal Message'::text
AND event_time > '2015-12-31 13:23:55.767+00'::timestamptz limit 10;
ORDER BY events.evt_time;
Table events is a partitioned table, one partition for each day.
Each partition has two constrains:
Name events_p_YYYYMMDDHHMISS_events_p_max_pk
Columns evt_time, evt_id
Name events_p_YYYYMMDDHHMISS_dc
Definition evt_time > '2015-12-04 13:24:25.267973+00'::timestamp with time zone AND evt_time <= '2015-12-05 13:24:25.267973+00'::timestamp with time zone
and seven indexes:
Name events_p_YYYYMMDDHHMISS_events_p_max_identity_ix1
Columns evt_time, init_usr_identity_guid, rid02
Operator classes timestamptz_ops, uuid_ops, int8_ops
Name events_p_YYYYMMDDHHMISS_events_p_max_identity_ix2
Columns evt_time, trgt_usr_identity_guid, rid02
Operator classes timestamptz_ops, uuid_ops, int8_ops
Name events_p_YYYYMMDDHHMISS_events_p_max_ix1
Columns evt_time, sev, agent_id
Operator classes timestamptz_ops, int4_ops, int8_ops
Name events_p_YYYYMMDDHHMISS_events_p_max_ix2
Columns evt_time, dip, sev
Operator classes timestamptz_ops, int4_ops, int4_ops
Name events_p_YYYYMMDDHHMISS_events_p_max_ix3
Columns evt_time, res, sev
Operator classes timestamptz_ops, text_ops, int4_ops
Name events_p_YYYYMMDDHHMISS_events_p_max_ix4
Columns evt_time, sip, sev
Operator classes timestamptz_ops, int4_ops, int4_ops
Name events_p_YYYYMMDDHHMISS_events_p_max_ix5
Columns evt_time, txnmy_id, agent_id
Operator classes timestamptz_ops, int8_ops, int8_ops
Table evt_agent is a dictionary table with about 20-30 rows only.
constrains:
Name evt_agent_pk
Columns agent_id
indexes:
Name evt_agent_ak1
Columns agent, port, rn, pn, sn, st, device_ctgry, src_id, cust_id
Operator classes text_ops, text_ops, text_ops, text_ops, text_ops, text_ops, text_ops, uuid_ops, int8_ops
Name evt_agent_ix1
Columns device_ctgry, agent_id
Operator classes text_ops, int8_ops
When I execute the query I got this explain plan:
http://explain.depesz.com/s/3xcu
Name evt_agent_ix2
Columns st, agent_id
Operator classes text_ops, int8_ops
Name test_ev_ag_indx1
Columns agent_id
Operator classes int8_ops
I thought there is a problem with statistics so I "vaccuum analyze" all related tables, but no improvement in the query performance, explain plan.
I tried with "inner query" trick, explain plan is here:
http://explain.depesz.com/s/FAkH
From what I can understand it got kind of worse.
Do you have nay idea how to get better execution plan for this query?
Now it takes about 42 minutes to get any results from the query.
Thanks in advance!
We will have to try and eliminate the hash join. A lateral join might just be the answer:
SELECT events.evt_id, events.evt_time, events.device_evt_time , to_ip_char(events.sip) , evt_agent.port , events.rv40 , events.evt , events.msg , events.sun , events.rv35 , events.dun , events.rv45 , events.fn , events.dp , events.trgt_trust_name , events.trgt_trust_domain , events.rv36 , events.rv43 , events.cv21 , events.cv40 , events.cv41 , events.cv42 , events.cv43 , events.cv44 , events.cv50 , events.cv51 , events.cv52 , events.cv53 , events.cv54 , events.cv55 , events.cv56 , events.cv35 , events.cv60 , events.cv61 , events.cv62
FROM evt_agent,
LATERAL (SELECT *
FROM events AS e
WHERE
e.agent_id = evt_agent.agent_id
AND e.evt::text <> 'Internal Message'::text
AND e.event_time > '2015-12-31 13:23:55.767+00'::timestamptz
LIMIT 10000) AS events
WHERE (evt_agent.port::text = ANY (ARRAY['x'::character varying, 'y'::character varying, 'z'::character varying]::text[]))
ORDER BY events.evt_time
LIMIT 10000;
Please try it with and without the inner limit and post the explain plans for both.

Conversion from Oracle into SQL Server

I am trying to interpret Oracle script into SQL script and I see that I am converting the joins wrong as Oracle is using multiple instances of the same table(see orguserfield_c, orguserfield_e, etc in select statment). Could anyone please help me in converting the Oracle into SQL script. Thanks
ORACLE SCRIPT:
select distinct
wh_acctcommon.effdate
, wh_acctcommon.acctnbr
, wh_acctcommon.acctclosecurrmonthyn
, wh_acctcommon.acctofficer
, wh_acctcommon.acctofficernbr
, wh_acctcommon.acctopencurrmonthyn
, wh_acctcommon.notebal
, wh_acctcommon.branchname
, wh_acctcommon.branchorgnbr
, orguserfield_c.value branch_internal_no
, orguserfield_e.value branch_status
, orguserfield_h.value branch_hub_no
, orguserfield_i.value metro_micro
, orguserfield_j.value division
, userfieldvalue_l.userfieldvaluedesc division_name
, orguserfield.value region
, userfieldvalue.userfieldvaluedesc region_name
, wh_acctcommon.primaryownercity
, wh_acctcommon.closedate
, wh_acctcommon.compoundcalpercd
, wh_acctcommon.contractdate
, wh_acctcommon.datelastmaint
, wh_acctcommon.ownername
, wh_acctcommon.bankorgnbr
, wh_acctcommon.intbase
, wh_acctcommon.intmethcd
, wh_acctcommon.noteintrate
, wh_acctcommon.ownersortname
, wh_acctcommon.loanofficer
, wh_acctcommon.loanofficersnbr
, wh_acctcommon.mjaccttypcd
, wh_acctcommon.managingofficer
, wh_acctcommon.managingofficernbr
, acctacctrolepers.persnbr SBB_Portfolio_Mgr_Nbr
, persview.fullname SBB_Portfolio_Mgr
, acctacctrolepers_c.persnbr Orig_Loan_Officer_Nbr
, persview_d.fullname Orig_Loan_Officer
, acctacctrolepers_e.persnbr Collection_Officer_Nbr
, persview_e.fullname Collection_Officer
, wh_acctcommon.datemat
, wh_acctcommon.intminbalamt
, wh_acctcommon.intmincalcbaltypcd
, wh_acctcommon.monthendyn
, wh_acctcommon.notemtdavgbal
, wh_acctcommon.nameaddr1
, wh_acctcommon.nameaddr2
, wh_acctcommon.nameaddr3
, wh_acctcommon.nameaddr4
, wh_acctcommon.nameaddr5
, wh_acctcommon.notenextratechangedate
, wh_acctcommon.noteopenamt
, wh_acctcommon.originatingperson
, wh_acctcommon.origpersnbr
, wh_acctcommon.bookbalance
, wh_acctcommon.businessphone
, wh_acctcommon.homephone
, wh_acctcommon.currmiaccttypcd
, wh_acctcommon.product
, wh_acctcommon.calcbaltypcd
, wh_acctcommon.noteratechangecalpercd
, wh_acctcommon.daysmethcd
, wh_acctcommon.noteintcalcschednbr
, wh_acctcommon.ratetypcd
, wh_acctcommon.primaryownerstate
, wh_acctcommon.curracctstatcd
, wh_acctcommon.curracctstateffdate
, wh_acctcommon.taxidnbr
, wh_acctcommon.taxrptfororgnbr
, wh_acctcommon.taxrptforpersnbr
, wh_acctcommon.currterm
, wh_acctcommon.primaryownerzipcd
, wh_acctcommon.primaryownerzipcdsuff
from OSIBANK.wh_acctcommon
, OSIBANK.orguserfield
, OSIBANK.orguserfield orguserfield_c
, OSIBANK.orguserfield orguserfield_e
, OSIBANK.orguserfield orguserfield_h
, OSIBANK.orguserfield orguserfield_i
, OSIBANK.orguserfield orguserfield_j
, OSIBANK.userfieldvalue
, OSIBANK.userfieldvalue userfieldvalue_l
, OSIBANK.acctacctrolepers
, OSIBANK.persview
, OSIBANK.acctacctrolepers acctacctrolepers_c
, OSIBANK.persview persview_d
, OSIBANK.acctacctrolepers acctacctrolepers_e
, OSIBANK.persview persview_e
where ( wh_acctcommon.branchorgnbr = orguserfield.orgnbr(+)
and upper(orguserfield.userfieldcd(+)) = 'WREG'
and orguserfield.userfieldcd = userfieldvalue.userfieldcd(+)
and orguserfield.value = userfieldvalue.userfieldvalue(+)
)
and ( wh_acctcommon.branchorgnbr = orguserfield_c.orgnbr(+)
and upper(orguserfield_c.userfieldcd(+)) = 'WBRN'
)
and ( wh_acctcommon.branchorgnbr = orguserfield_e.orgnbr(+)
and upper(orguserfield_e.userfieldcd(+)) = 'WBRS'
)
and ( wh_acctcommon.branchorgnbr = orguserfield_h.orgnbr(+)
and upper(orguserfield_h.userfieldcd(+)) = 'WHBN'
)
and ( wh_acctcommon.branchorgnbr = orguserfield_i.orgnbr(+)
and upper(orguserfield_i.userfieldcd(+)) = 'WSIZ'
)
and ( wh_acctcommon.branchorgnbr = orguserfield_j.orgnbr(+)
and upper(orguserfield_j.userfieldcd(+)) = 'WDIV'
and orguserfield_j.userfieldcd = userfieldvalue_l.userfieldcd(+)
and orguserfield_j.value = userfieldvalue_l.userfieldvalue(+)
)
and ( wh_acctcommon.acctnbr = acctacctrolepers.acctnbr(+)
and upper(acctacctrolepers.acctrolecd(+)) = 'PMGR'
and acctacctrolepers.persnbr = persview.persnbr(+)
)
and ( wh_acctcommon.acctnbr = acctacctrolepers_c.acctnbr(+)
and acctacctrolepers_c.acctrolecd(+) = 'OLOF'
and acctacctrolepers_c.persnbr = persview_d.persnbr(+)
)
and ( wh_acctcommon.acctnbr = acctacctrolepers_e.acctnbr(+)
and acctacctrolepers_e.acctrolecd(+) = 'COFF'
and acctacctrolepers_e.persnbr = persview_e.persnbr(+)
);
SQL SCRIPT:
select
wh_acctcommon.[EFFDATE]
, wh_acctcommon.acctnbr
, wh_acctcommon.acctclosecurrmonthyn
, wh_acctcommon.acctofficer
, wh_acctcommon.acctofficernbr
, wh_acctcommon.acctopencurrmonthyn
, wh_acctcommon.notebal
, wh_acctcommon.branchname
, wh_acctcommon.branchorgnbr
,OUF.value AS 'branch_internal_no'
, OUF.value AS 'branch_status'
, OUF.value AS 'branch_hub_no'
, OUF.value AS 'metro_micro'
, OUF.value AS 'division'
, UFV.[USERFIELDVALUEDESC] AS 'division_name'
, OUF.value AS 'region'
, UFV.userfieldvaluedesc AS 'region_name'
, wh_acctcommon.primaryownercity
, wh_acctcommon.closedate
, wh_acctcommon.compoundcalpercd
, wh_acctcommon.contractdate
, wh_acctcommon.datelastmaint
, wh_acctcommon.ownername
, wh_acctcommon.bankorgnbr
, wh_acctcommon.intbase
, wh_acctcommon.intmethcd
, wh_acctcommon.noteintrate
, wh_acctcommon.ownersortname
, wh_acctcommon.loanofficer
, wh_acctcommon.loanofficersnbr
, wh_acctcommon.mjaccttypcd
, wh_acctcommon.managingofficer
, wh_acctcommon.managingofficernbr
, AARP.persnbr AS 'SBB_Portfolio_Mgr_Nbr'
, PV.fullname as 'SBB_Portfolio_Mgr'
, AARP.persnbr AS 'Orig_Loan_Officer_Nbr'
, PV.fullname AS 'Orig_Loan_Officer'
, AARP.persnbr as 'Collection_Officer_Nbr'
, PV.fullname AS 'Collection_Officer'
, wh_acctcommon.datemat
, wh_acctcommon.intminbalamt
, wh_acctcommon.intmincalcbaltypcd
, wh_acctcommon.monthendyn
, wh_acctcommon.notemtdavgbal
, wh_acctcommon.nameaddr1
, wh_acctcommon.nameaddr2
, wh_acctcommon.nameaddr3
, wh_acctcommon.nameaddr4
, wh_acctcommon.nameaddr5
, wh_acctcommon.notenextratechangedate
, wh_acctcommon.noteopenamt
, wh_acctcommon.originatingperson
, wh_acctcommon.origpersnbr
, wh_acctcommon.bookbalance
, wh_acctcommon.businessphone
, wh_acctcommon.homephone
, wh_acctcommon.currmiaccttypcd
, wh_acctcommon.product
, wh_acctcommon.calcbaltypcd
, wh_acctcommon.noteratechangecalpercd
, wh_acctcommon.daysmethcd
, wh_acctcommon.noteintcalcschednbr
, wh_acctcommon.ratetypcd
, wh_acctcommon.primaryownerstate
, wh_acctcommon.curracctstatcd
, wh_acctcommon.curracctstateffdate
, wh_acctcommon.taxidnbr
, wh_acctcommon.taxrptfororgnbr
, wh_acctcommon.taxrptforpersnbr
, wh_acctcommon.currterm
, wh_acctcommon.primaryownerzipcd
, wh_acctcommon.primaryownerzipcdsuff
FROM
[DNA_Staging].[dbo].[WH_ACCTCOMMON] wh_acctcommon
LEFT OUTER JOIN [DNA_Staging].[dbo].orguserfield OUF
ON wh_acctcommon.branchorgnbr = OUF .orgnbr
and upper(OUF.userfieldcd) IN( 'WREG','WBRN','WBRS','WHBN','WSIZ','WDIV')
LEFT OUTER JOIN [DNA_Staging].[dbo].userfieldvalue UFV
ON OUF.userfieldcd = UFV.userfieldcd
and OUF.value = UFV.userfieldvalue
LEFT OUTER JOIN [DNA_Staging].[dbo].[ACCTACCTROLEPERS] AARP
ON wh_acctcommon.acctnbr = AARP.acctnbr
and upper(AARP.acctrolecd) IN ('PMGR','OLOF','COFF')
LEFT OUTER JOIN [DNA_Cleanup_DM].[dbo].PERSVIEW PV
ON AARP.persnbr = PV.persnbr
This is mostly about converting the pre-SQL99 Oracle proprietary joins into SQL99 joins. Rather than try to convert your actual code, here is some advice:
The number of tables in the from clause should remain exactly the same in both queries. If a table is listed 5 times (with aliases) in the original query, you should have the same 5 instances in the revised query. A fatal flaw with your revised query is that you're trying to collapse these relationships using in. You simply can't do that and retain the meaning of the original query.
Start with the first table and work down, converting each comma in the original from clause into an on.
If a relationship in the original where clause contains the outer join indicator ((+)), then that relationship must go in the outer table's on clause. For instance, a.a = b.a(+) must become left join b on a.a = b.a.

If statement in Oracle

I need to create a query, that if a certain field is blank or null. I need to do a select statement to another table and retrieve the blank field . Could you please advise on a way to accomplish this. Below is the query. The field in question is BEAT.
SELECT COALESCE(ADDRESSES.BEAT,Incident_addresses.beat)
, COALESCE (ADDRESSES.SUB_BEAT,Incident_addresses.sub_beat)
, ADDRESSES.STREET_NAME
, ADDRESSES.STREET_NUMBER
, ADDRESSES.SUB_NUMBER
, WARRANT_PEOPLE_VW.LNAME
, WARRANT_PEOPLE_VW.FNAME
, WARRANT_PEOPLE_VW.DOB
, WARRANT_PEOPLE_VW.RACE_RACE_CODE
, WARRANT_PEOPLE_VW.SEX_SEX_CODE
, WARRANT_PEOPLE_VW.CASE_NUMBER
, E_WARRANTS.DATE_ISSUED
, E_WARRANTS.TELETYPE_NUMBER
, E_WARRANTS.ORDINANCE_VIOLATION
FROM EJSDBA.ADDRESSES
, POL_LEEAL.E_WARRANTS
, POL_LEEAL.WARRANT_PEOPLE_VW,incident_people,Incident_addresses
WHERE ADDRESSES.ADDRESS_ID =E_WARRANTS.ADDR_ADDRESS_ID
AND E_WARRANTS.WARRANT_ID = WARRANT_PEOPLE_VW.WARRANT_ID
AND WARRANT_PEOPLE_VW.NME_TYP_NAME_TYPE_CODE = 'P'
AND WARRANT_PEOPLE_VW.AGNCY_CD_AGENCY_CODE = 'MCPD'
AND WARRANT_PEOPLE_VW.WSC_CODE='A'
AND EJSDBA.ADDRESSES.ADDRESS_ID= Incident_addresses.ADDRESS_ID
and incident_people.inc_incident_id=Incident_addresses.incident_id
ORDER BY ADDRESSES.BEAT
, ADDRESSES.SUB_BEAT
, ADDRESSES.STREET_NAME
, ADDRESSES.STREET_NUMBER
;
You can embed a correlated subquery into a case expression, but you MUST reference the table inside the subquery to some values of the outer query so that the correct value can be located.
SELECT
COALESCE(ADDRESSES.BEAT, Incident_addresses.beat)
, CASE
WHEN Addresses.BEAT IS NULL THEN (
SELECT
Beat
FROM incidents inner_ref
WHERE ???outer??.incident_id = inner_ref.id
AND rownum = 1)
END AS x
, COALESCE(ADDRESSES.SUB_BEAT, Incident_addresses.sub_beat)
...
and that subquery MUST only return a single value (hence I have used "and rownum = 1".
(In Oracle 12 you could use FETCH FIRST 1 ROW ONLY)

I am new to oracle but I am creating a procedure in a package in oracle and I get this Error Message Can anyone see what i am doing wrong?

"Error(33,13): PLS-00323: subprogram or cursor 'USP_CHECK_USER_ADMIN' is declared in a package specification and must be defined in the package body"
create or replace
PACKAGE PKG_CUST_XREF1 AS
PROCEDURE USP_ADD_CUSTOMER
(
p_CUST_ID IN MDW.CUSTOMER_STRUCTURE_XREF.CUST_ID%TYPE
, p_ADDR_SUF IN MDW.CUSTOMER_STRUCTURE_XREF.ADDR_SUF%TYPE
, p_PLAN_SYS_ID_SOURCE_CD IN MDW.CUSTOMER_STRUCTURE_XREF.PLAN_SYS_ID_SOURCE_CD%TYPE
, p_REGION_ID IN MDW.CUSTOMER_STRUCTURE_XREF.REGION_ID%TYPE
, p_COMPANY_ID IN MDW.CUSTOMER_STRUCTURE_XREF.COMPANY_ID%TYPE
, p_REP_ID IN MDW.CUSTOMER_STRUCTURE_XREF.REP_ID%TYPE
, p_TYPE_ID IN MDW.CUSTOMER_STRUCTURE_XREF.TYPE_ID%TYPE
, p_EXTERNAL_ACCT_NO IN MDW.CUSTOMER_STRUCTURE_XREF.EXTERNAL_ACCT_NO%TYPE
, p_REGION_NAME IN MDW.CUSTOMER_STRUCTURE_XREF.REGION_NAME%TYPE
, p_COMPANY_NAME IN MDW.CUSTOMER_STRUCTURE_XREF.COMPANY_NAME%TYPE
, p_SUF_NAME IN MDW.CUSTOMER_STRUCTURE_XREF.SUF_NAME%TYPE
, p_CUST_NAME IN MDW.CUSTOMER_STRUCTURE_XREF.CUST_NAME%TYPE
, p_INDUSTRY_TYPE_DESC IN MDW.CUSTOMER_STRUCTURE_XREF.INDUSTRY_TYPE_DESC%TYPE
, p_CHANNEL_TYPE_DESC IN MDW.CUSTOMER_STRUCTURE_XREF.CHANNEL_TYPE_DESC%TYPE
, p_INDUSTRY_SPECIALIST_ID IN MDW.CUSTOMER_STRUCTURE_XREF.INDUSTRY_SPECIALIST_ID%TYPE
, p_CUSTOMER_CLASS IN MDW.CUSTOMER_STRUCTURE_XREF.CUSTOMER_CLASS%TYPE
, p_ORA_CUST_ID IN MDW.CUSTOMER_STRUCTURE_XREF.ORA_CUST_ID%TYPE
, p_STATUS IN MDW.CUSTOMER_STRUCTURE_XREF.STATUS%TYPE
);
END PKG_CUST_XREF1;
/* BODY!!*/
create or replace
PACKAGE BODY PKG_CUST_XREF1
IS
PROCEDURE USP_ADD_CUSTOMER(
p_CUST_ID IN MDW.CUSTOMER_STRUCTURE_XREF.CUST_ID%TYPE
, p_ADDR_SUF IN MDW.CUSTOMER_STRUCTURE_XREF.ADDR_SUF%TYPE
, p_PLAN_SYS_ID_SOURCE_CD IN MDW.CUSTOMER_STRUCTURE_XREF.PLAN_SYS_ID_SOURCE_CD%TYPE
, p_REGION_ID IN MDW.CUSTOMER_STRUCTURE_XREF.REGION_ID%TYPE
, p_COMPANY_ID IN MDW.CUSTOMER_STRUCTURE_XREF.COMPANY_ID%TYPE
, p_REP_ID IN MDW.CUSTOMER_STRUCTURE_XREF.REP_ID%TYPE
, p_TYPE_ID IN MDW.CUSTOMER_STRUCTURE_XREF.TYPE_ID%TYPE
, p_EXTERNAL_ACCT_NO IN MDW.CUSTOMER_STRUCTURE_XREF.EXTERNAL_ACCT_NO%TYPE
, p_REGION_NAME IN MDW.CUSTOMER_STRUCTURE_XREF.REGION_NAME%TYPE
, p_COMPANY_NAME IN MDW.CUSTOMER_STRUCTURE_XREF.COMPANY_NAME%TYPE
, p_SUF_NAME IN MDW.CUSTOMER_STRUCTURE_XREF.SUF_NAME%TYPE
, p_CUST_NAME IN MDW.CUSTOMER_STRUCTURE_XREF.CUST_NAME%TYPE
, p_INDUSTRY_TYPE_DESC IN MDW.CUSTOMER_STRUCTURE_XREF.INDUSTRY_TYPE_DESC%TYPE
, p_CHANNEL_TYPE_DESC IN MDW.CUSTOMER_STRUCTURE_XREF.CHANNEL_TYPE_DESC%TYPE
, p_INDUSTRY_SPECIALIST_ID IN MDW.CUSTOMER_STRUCTURE_XREF.INDUSTRY_SPECIALIST_ID%TYPE
, p_CUSTOMER_CLASS IN MDW.CUSTOMER_STRUCTURE_XREF.CUSTOMER_CLASS%TYPE
, p_ORA_CUST_ID IN MDW.CUSTOMER_STRUCTURE_XREF.ORA_CUST_ID%TYPE
, p_STATUS IN MDW.CUSTOMER_STRUCTURE_XREF.STATUS%TYPE)
IS
BEGIN
INSERT INTO MDW.CUSTOMER_STRUCTURE_XREF
(
CUST_ID
, ADDR_SUF
, PLAN_SYS_ID_SOURCE_CD
, REGION_ID
, COMPANY_ID
, REP_ID
, TYPE_ID
, EXTERNAL_ACCT_NO
, REGION_NAME
, COMPANY_NAME
, SUF_NAME
, CUST_NAME
, INDUSTRY_TYPE_DESC
, CHANNEL_TYPE_DESC
, INDUSTRY_SPECIALIST_ID
, CUSTOMER_CLASS
, ORA_CUST_ID
, STATUS
)
VALUES (
p_CUST_ID
, p_ADDR_SUF
, p_PLAN_SYS_ID_SOURCE_CD
, p_REGION_ID
, p_COMPANY_ID
, p_REP_ID
, p_TYPE_ID
, p_EXTERNAL_ACCT_NO
, p_REGION_NAME
, p_COMPANY_NAME
, p_SUF_NAME
, p_CUST_NAME
, p_INDUSTRY_TYPE_DESC
, p_CHANNEL_TYPE_DESC
, p_INDUSTRY_SPECIALIST_ID
, p_CUSTOMER_CLASS
, p_ORA_CUST_ID
, p_STATUS);
COMMIT;
END USP_ADD_CUSTOMER;
END PKG_CUST_XREF1;
Yes, we can see what you're doing wrong - the PL/SQL compiler tells you the exact reason:
PLS-00323: subprogram or cursor 'USP_CHECK_USER_ADMIN' is declared in a package
specification and must be defined in the package body
If you have problems to understand the error message please see chapter Package Body from PL/SQL Packages.
You probably should read the whole Oracle Database PL/SQL Language Reference too.

Resources