Add IF ELSE Condition in PL SQL stored procedure - oracle

I have a scenario where I want to add an IF ELSE condition in my below procedure:
I tried like below but it is not getting compiled
PROCEDURE VSAT_EXCEL_REPORT
(
P_R4GSTATE IN NVARCHAR2,
TBLDATA OUT SYS_REFCURSOR
)
AS
BEGIN
OPEN TBLDATA FOR
IF P_R4GSTATE = "ALL"
THEN
SELECT ENB.SAP_ID AS SAP_ID,CAND.CANDIDATEID AS CANDIDATE_ID,ENB.R4G_STATE AS STATE,ENB.SITE_TYPE AS SITE_TYPE,CAND.SITENAME AS SITE_NAME
,ENB.SITEID AS SITEID,ENB.PRIORITY_SITE AS PRIORITYSITE,ENB.CIRCLE AS CIRCLE,
DECODE (VTS.STATUS_NAME, null, 'Fill VSAT form', VTS.STATUS_NAME) AS STATUS,
ENB.LATITUDE
,ENB.LONGITUDE
,VT.SITE_DETAILS_TECHNOLOGY
,VT.SITE_DETAILS_MOUNT_TYPE
,VT.SITE_DETAILS_ANTENNA_SIZE
,VT.SITE_DETAILS_AMSL
,VT.SITE_DETAILS_PLATFORM_REQUIRED
,VT.SITE_DETAILS_PLATFORM_HEIGHT
,VT.SITE_DETAILS_ROOFTOP_SITE
,VT.SITE_DETAILS_NOOFLOORS
,VT.SITE_DETAILS_ADD_ROOFTOP_SITE
,VT.SITE_DETAILS_STRUCTURAL_STAB
,VT.SITE_DETAILS_HEIGHT_OF_ANTENNA
,VT.SITE_DETAILS_SATELLITE_BAND
,VT.ANTENNA_DISTANCE_NEAR_AIRPORT
,VT.ANTENNA_AIRPORT_NAME
,VT.GSAT_LOS_16_55E
,VT.GSAT_LOS_18_74E
,VT.GSAT_LOS_12_83E
FROM R4G_OSP.ENODEB ENB
INNER JOIN R4G_OSP.CANDIDATE CAND ON ENB.SAP_ID = CAND.SAP_ID
LEFT JOIN TBL_VSAT_MST_DETAIL VT ON ENB.SAP_ID = VT.SAP_ID
LEFT JOIN TBL_VSAT_STATUS_MST VTS ON VT.STATUS_ID = VTS.STATUS_ID
WHERE ENB.SCOPE = 'EnodeB-Connected_MW'
AND ENB.SITEBACKHAUL = 'MW'
AND CAND.STATUS = 'Fill Vendor Survey Form'
AND UPPER(STATE) IN
(SELECT REGEXP_SUBSTR(UPPER(P_R4GSTATE),'[^,]+', 1, LEVEL) AS RFIELDS FROM DUAL
CONNECT BY REGEXP_SUBSTR(UPPER(P_R4GSTATE), '[^,]+', 1, LEVEL) IS NOT NULL);
ELSE
--Some condtion
END VSAT_EXCEL_REPORT;

I think it should be image like this:
procedure VSAT_EXCEL_REPORT
(
P_R4GSTATE in nvarchar2,
TBLDATA out sys_refcursor
) as
begin
if P_R4GSTATE = 'ALL' then
open TBLDATA for
select ENB.SAP_ID as SAP_ID
,CAND.CANDIDATEID as CANDIDATE_ID
,ENB.R4G_STATE as STATE
,ENB.SITE_TYPE as SITE_TYPE
,CAND.SITENAME as SITE_NAME
,ENB.SITEID as SITEID
,ENB.PRIORITY_SITE as PRIORITYSITE
,ENB.CIRCLE as CIRCLE
,DECODE(VTS.STATUS_NAME
,null
,'Fill VSAT form'
,VTS.STATUS_NAME) as STATUS
,ENB.LATITUDE
,ENB.LONGITUDE
,VT.SITE_DETAILS_TECHNOLOGY
,VT.SITE_DETAILS_MOUNT_TYPE
,VT.SITE_DETAILS_ANTENNA_SIZE
,VT.SITE_DETAILS_AMSL
,VT.SITE_DETAILS_PLATFORM_REQUIRED
,VT.SITE_DETAILS_PLATFORM_HEIGHT
,VT.SITE_DETAILS_ROOFTOP_SITE
,VT.SITE_DETAILS_NOOFLOORS
,VT.SITE_DETAILS_ADD_ROOFTOP_SITE
,VT.SITE_DETAILS_STRUCTURAL_STAB
,VT.SITE_DETAILS_HEIGHT_OF_ANTENNA
,VT.SITE_DETAILS_SATELLITE_BAND
,VT.ANTENNA_DISTANCE_NEAR_AIRPORT
,VT.ANTENNA_AIRPORT_NAME
,VT.GSAT_LOS_16_55E
,VT.GSAT_LOS_18_74E
,VT.GSAT_LOS_12_83E
from R4G_OSP.ENODEB ENB
inner join R4G_OSP.CANDIDATE CAND
on ENB.SAP_ID = CAND.SAP_ID
left join TBL_VSAT_MST_DETAIL VT
on ENB.SAP_ID = VT.SAP_ID
left join TBL_VSAT_STATUS_MST VTS
on VT.STATUS_ID = VTS.STATUS_ID
where ENB.SCOPE = 'EnodeB-Connected_MW'
and ENB.SITEBACKHAUL = 'MW'
and CAND.STATUS = 'Fill Vendor Survey Form'
and
UPPER(STATE) in
(select REGEXP_SUBSTR(UPPER(P_R4GSTATE), '[^,]+', 1, level) as RFIELDS
from DUAL
connect by REGEXP_SUBSTR(UPPER(P_R4GSTATE), '[^,]+', 1, level) is not null);
else
open TBLDATA for
select * from dual;
end if;
end VSAT_EXCEL_REPORT;

Related

Pl / SQL Oracle helps to run a Date in Subquery

How could I get the date of the Maximum Value, by means of a subquery
I can't put the Date in the Main query because I would have to add it to the group by it would bring me a lot of data
Here is the Code:
SELECT MAX (A1.VALOR) AS VALOR,
(SELECT sq1.FECHA
FROM VARIABLE_VALORES_SMEC sq1
WHERE sq1.ID_AGENTE = A1.ID_AGENTE)
MES, -- {<-- Here is the Problem}
(SELECT CODIGO_AGENTE
FROM AGENTES
WHERE ID_AGENTE = A1.ID_AGENTE)
Agentess,
(SELECT NOMBRE_AGENTE
FROM AGENTES
WHERE ID_AGENTE = A1.ID_AGENTE)
Nombre_Agente
FROM VARIABLE_VALORES_SMEC A1
WHERE A1.VALOR < '1'
AND A1.VALOR != '0'
AND A1.ID_AGENTE IN (SELECT C1.ID_AGENTE
FROM VARIABLE_VALORES_SMEC C1
WHERE A1.FECHA = C1.FECHA)
AND A1.ID_AGENTE IN (SELECT B1.ID_AGENTE
FROM AGENTES B1
WHERE ID_CATEGORIA_AGENTE = 'AC006')
AND (A1.FECHA BETWEEN (ADD_MONTHS (TO_DATE ( :FECHAIN, 'MM/DD/YYYY'),
-1))
AND (LAST_DAY (
ADD_MONTHS (
TO_DATE ( :FECHAIN, 'MM/DD/YYYY'),
-1))))
AND A1.ID_VARIABLE LIKE '%_calc_total_pot#%'
GROUP BY ID_AGENTE
Am I correct that you need (fecha) for maximum A1.VALOR?
If - yes, you can use the following query, or if - no, just replace A1.VALOR with the required column in keep() clause:
SELECT MAX (A1.VALOR) AS VALOR,
max(A1.FECHA)keep(dense_rank first order by A1.VALOR desc) MES, -- A1.VALOR is used here as sort key, replace it with what you want
(SELECT CODIGO_AGENTE
FROM AGENTES
WHERE ID_AGENTE = A1.ID_AGENTE)
Agentess,
(SELECT NOMBRE_AGENTE
FROM AGENTES
WHERE ID_AGENTE = A1.ID_AGENTE)
Nombre_Agente
FROM VARIABLE_VALORES_SMEC A1
WHERE A1.VALOR < '1'
AND A1.VALOR != '0'
AND A1.ID_AGENTE IN (SELECT C1.ID_AGENTE
FROM VARIABLE_VALORES_SMEC C1
WHERE A1.FECHA = C1.FECHA)
AND A1.ID_AGENTE IN (SELECT B1.ID_AGENTE
FROM AGENTES B1
WHERE ID_CATEGORIA_AGENTE = 'AC006')
AND (A1.FECHA BETWEEN (ADD_MONTHS (TO_DATE ( :FECHAIN, 'MM/DD/YYYY'),
-1))
AND (LAST_DAY (
ADD_MONTHS (
TO_DATE ( :FECHAIN, 'MM/DD/YYYY'),
-1))))
AND A1.ID_VARIABLE LIKE '%_calc_total_pot#%'
GROUP BY ID_AGENTE
You can use row_number analytical function to fetch one record for which value is highest and use the fecha of that record. Use following sub query:
(Select fecha from
(SELECT sq1.FECHA, row_number() over (order by sq1.value desc nulls last) as rn
FROM VARIABLE_VALORES_SMEC sq1
WHERE sq1.ID_AGENTE = A1.ID_AGENTE)
Where rn = 1) MES

ORA-01427 Single-Row Subquery Returns More than One Row - how to resolve?

I've had this query running for over 2 years and just recently this began throwing the ORA-01427 single row sub-query returns multiple rows error. What's the best way to debug this when there are multiple sub-queries? Should I be adding a MAX statement to each subquery? I've tried switching some of the = operators to IN but that's not working and I'm not confident it would give me the correct results either.
Appreciate any insight or assistance this community can provide. I'm ripping my hair out working through this one!
SELECT
NVL(
(SELECT
'Y'
FROM
PER_ASSIGNMENT_SUPERVISORS_F
WHERE
MANAGER_ID = PAPF.PERSON_ID
AND MANAGER_TYPE ='LINE_MANAGER'
AND TRUNC(SYSDATE) BETWEEN EFFECTIVE_START_DATE AND EFFECTIVE_END_DATE
AND ROWNUM = 1),'N') MANAGER_FLAG,
PAAM.ASSIGNMENT_STATUS_TYPE,
PAAM.EMPLOYMENT_CATEGORY,
TO_CHAR(PPOS.DATE_START,'YYYY-MM-DD') AS HIRE_DATE,
PER_EXTRACT_UTILITY.GET_STANDARD_WORKING_HOURS(PAAM.ASSIGNMENT_ID,TRUNC(SYSDATE)) AS STANDARD_WORKING_HOURS,
TO_CHAR(PAAM.EFFECTIVE_START_DATE,'YYYY-MM-DD') AS EFFECTIVE_START_DATE,
(
SELECT
PJFVL.JOB_CODE
FROM
PER_ALL_ASSIGNMENTS_M PAAM1,
PER_JOBS_F_VL PJFVL
WHERE
PAAM1.PERSON_ID = PASF.MANAGER_ID
AND TRUNC(SYSDATE) BETWEEN PAAM1.EFFECTIVE_START_DATE AND PAAM1.EFFECTIVE_END_DATE
AND TRUNC(SYSDATE) BETWEEN PJFVL.EFFECTIVE_START_DATE AND PJFVL.EFFECTIVE_END_DATE
and PAAM1.ASSIGNMENT_STATUS_TYPE='ACTIVE'
AND PAAM1.ASSIGNMENT_TYPE = 'E'
AND PAAM1.effective_latest_change = 'Y'
AND PJFVL.JOB_ID = PAAM1.JOB_ID
)as Manager_job_code,
(
SELECT
HOIF.ORG_INFORMATION1
FROM
PER_ALL_ASSIGNMENTS_M PAAM2,
HR_ORGANIZATION_INFORMATION_F HOIF
WHERE
PAAM2.PERSON_ID = PASF.MANAGER_ID
AND HOIF.ORG_INFORMATION_CONTEXT = 'DEPT_DET'
and PAAM2.ASSIGNMENT_STATUS_TYPE='ACTIVE'
AND PAAM2.ASSIGNMENT_TYPE = 'E'
AND PAAM2.effective_latest_change = 'Y'
AND TRUNC(SYSDATE) BETWEEN PAAM2.EFFECTIVE_START_DATE AND PAAM2.EFFECTIVE_END_DATE
AND TRUNC(SYSDATE) BETWEEN HOIF.EFFECTIVE_START_DATE AND HOIF.EFFECTIVE_END_DATE
AND HOIF.ORGANIZATION_ID = PAAM2.ORGANIZATION_ID
) as Manager_dep_code,
(SELECT
COUNT(PPOS.PERIOD_OF_SERVICE_ID)
FROM
PER_PERIODS_OF_SERVICE PPOS
WHERE
1 = 1
AND PAAM.PERSON_ID = PPOS.PERSON_ID
) AS INACTIVE_WORKRELATIONSHIP,
PAPF.PERSON_NUMBER as SAMACCOUNTNAME,
(SELECT CSB.NAME FROM
CMP_SALARY CS,
CMP_SALARY_BASES CSB
WHERE
CS.ASSIGNMENT_ID = PAAM.ASSIGNMENT_ID
AND CS.SALARY_BASIS_ID = CSB.SALARY_BASIS_ID
and TRUNC(SYSDATE) BETWEEN CS.DATE_FROM AND CS.DATE_TO
) AS hourly_salary_Paid,
TO_CHAR(PP.DATE_OF_BIRTH,'YYYY-MM-DD') AS DOB,
PPNFV.LAST_NAME,
PPNFV.FIRST_NAME,
HLA.LOCATION_NAME as Location,
HLA.ADDRESS_LINE_1 AS LOC_ADDRESS_1,
HLA.ADDRESS_LINE_2 AS LOC_ADDRESS_2,
HLA.TOWN_OR_CITY AS City,
HLA.POSTAL_CODE AS ZIP_CODE,
HLA.REGION_2 AS STATE,
PPNFV.KNOWN_AS AS PREFERRED_NAME,
TRIM((PPNFV.KNOWN_AS||' '||PPNFV.LAST_NAME)) AS PREFERRED_NAME_LAST_NAME,
(SELECT
PPNFV.DISPLAY_NAME
FROM per_person_names_f_v PPNFV
WHERE 1 = 1
AND PPNFV.PERSON_ID = PASF.MANAGER_ID
AND PPNFV.NAME_TYPE='GLOBAL'
AND TRUNC(SYSDATE) BETWEEN PPNFV.EFFECTIVE_START_DATE AND PPNFV.EFFECTIVE_END_DATE
) AS MANAGER_NAME,
(SELECT
PAPF.PERSON_NUMBER
FROM PER_ALL_PEOPLE_F PAPF
WHERE PAPF.PERSON_ID = PASF.MANAGER_ID
AND TRUNC(SYSDATE) BETWEEN PAPF.EFFECTIVE_START_DATE AND PAPF.EFFECTIVE_END_DATE
) AS MANAGER_NUMBER,
HAOUFVL.NAME AS DEPARTMENT,
PAAM.ASSIGNMENT_NAME AS JOB_TILE,
PLE.NAME as Company,
PJLG.INFORMATION1 AS FLSA,
SUBSTR(PNI.NATIONAL_IDENTIFIER_NUMBER,-4) AS SSN_NUMBER,
PAAM.ASS_ATTRIBUTE1 as Officer_TITLE,
(
select
bu.name
from
hr_all_organization_units_f_vl bu
where 1 = 1
and paam.business_unit_id = bu.organization_id
and trunc(sysdate) between bu.effective_start_date and bu.effective_end_date
) as BUS_UNIT,
TO_CHAR (PPOS.ORIGINAL_DATE_OF_HIRE,'YYYY-MM-DD') AS ORIGINAL_DATE_OF_HIRE1,
(
CASE WHEN PPLF.SEX = 'F' THEN
'TRUE'
ELSE
'FALSE'
END
) AS GENDER,
(
CASE WHEN PJFFVL.JOB_FAMILY_NAME = 'Executive' THEN
'ELT'
ELSE
' '
END
) AS ELT_DESIGNATION,
HOIF.ORG_INFORMATION1 as DEPATMENT_CODE,
PJFV.JOB_CODE AS JOB_CODE,
PAF.ADDRESS_LINE_1 AS HOME_ADDRESS_LINE_1,
PAF.ADDRESS_LINE_2 AS HOME_ADDRESS_LINE_2,
PAF.TOWN_OR_CITY AS HOW_ADDRESS_CITY,
PAF.REGION_2 AS HOME_ADDRESS_STATE,
PAF.POSTAL_CODE AS HOME_ADRESS_ZIP_CODE,
PGFTL.NAME as Grade_level,
(SELECT
distinct (per_extract_utility.get_decoded_lookup('JOB_FUNCTION_CODE',PJF.JOB_FUNCTION_CODE))
FROM
PER_JOB_SECURED_LIST_V job WHERE TRUNC(SYSDATE) BETWEEN effective_start_date AND effective_end_date
) as JOB_FUNCTION,
pp.attribute1 as PER_NETWORKID,
(SELECT
PPNFV.attribute1
FROM PER_PERSONS PPNFV
WHERE 1 = 1
AND PPNFV.PERSON_ID = PASF.MANAGER_ID
) AS MANAGER_NETWORKID,
HOIF.ORG_INFORMATION2 AS REGION,
to_char(paam.ass_attribute_date1,'MM/DD/YYYY') as OfficerPromoDate
FROM
PER_ALL_ASSIGNMENTS_M PAAM,
PER_ASSIGNMENT_SUPERVISORS_F PASF,
PER_PERIODS_OF_SERVICE PPOS,
PER_ALL_PEOPLE_F PAPF,
PER_PERSONS PP,
per_person_names_f_v PPNFV,
HR_LOCATIONS_ALL HLA,
HR_ALL_ORGANIZATION_UNITS_F_VL HAOUFVL,
PER_JOB_LEG_F PJLG,
PER_NATIONAL_IDENTIFIERS PNI,
PER_PEOPLE_LEGISLATIVE_F PPLF,
PER_JOB_FAMILY_F_VL PJFFVL,
PER_JOBS_F_V PJFV,
HR_ORGANIZATION_INFORMATION_F HOIF,
PER_ADDRESSES_F PAF,
PER_PERSON_ADDR_USAGES_F PPAUF,
PER_GRADES_F_TL PGFTL,
PER_JOBS_F PJF,
PER_LEGAL_EMPLOYERS PLE,
HR_ALL_ORGANIZATION_UNITS_F HAOUF
WHERE
PAAM.ASSIGNMENT_STATUS_TYPE='ACTIVE'
AND PAAM.ASSIGNMENT_TYPE = 'E'
AND PAAM.effective_latest_change = 'Y'
AND PAAM.ASSIGNMENT_ID = PASF.ASSIGNMENT_ID(+)
AND PASF.MANAGER_TYPE (+) = 'LINE_MANAGER'
AND PNI.PERSON_ID(+) = PAAM.PERSON_ID
AND PAAM.PERSON_ID = PPLF.PERSON_ID(+)
and PJFV.JOB_FAMILY_ID= PJFFVL.JOB_FAMILY_ID(+)
AND PJFV.JOB_ID(+) = PAAM.JOB_ID
AND HOIF.ORGANIZATION_ID(+)= PAAM.ORGANIZATION_ID
AND HOIF.ORG_INFORMATION_CONTEXT(+) = 'DEPT_DETAILS'
AND PPAUF.PERSON_ID(+)= PAPF.PERSON_ID
AND PPAUF.ADDRESS_TYPE (+) = 'HOME'
AND PPAUF.ADDRESS_ID= PAF.ADDRESS_ID(+)
AND PGFTL.GRADE_ID(+)= PAAM.GRADE_ID
AND PJF.JOB_ID(+)= PAAM.JOB_ID
AND PJLG.INFORMATION_CATEGORY='HRX_US_JOBS'
and HAOUF.ORGANIZATION_ID(+)=PLE.ORGANIZATION_ID
and PAAM.LEGAL_ENTITY_ID=PLE.ORGANIZATION_ID
AND TRUNC(SYSDATE) BETWEEN PAAM.EFFECTIVE_START_DATE(+) AND PAAM.EFFECTIVE_END_DATE (+)
AND PAAM.PERIOD_OF_SERVICE_ID = PPOS.PERIOD_OF_SERVICE_ID
AND PAPF.PERSON_ID = PAAM.PERSON_ID
AND PP.PERSON_ID = PAAM.PERSON_ID
AND PPNFV.PERSON_ID = PAAM.PERSON_ID
AND HLA.LOCATION_ID(+) = PAAM.LOCATION_ID
AND PJLG.JOB_ID(+) = PAAM.JOB_ID
AND HAOUFVL.ORGANIZATION_ID(+) = PAAM.ORGANIZATION_ID
AND PPNFV.NAME_TYPE = 'GLOBAL'
AND TRUNC(SYSDATE) BETWEEN PPNFV.EFFECTIVE_START_DATE AND PPNFV.EFFECTIVE_END_DATE
AND TRUNC(SYSDATE) BETWEEN PAPF.EFFECTIVE_START_DATE AND PAPF.EFFECTIVE_END_DATE
AND TRUNC(SYSDATE) BETWEEN HAOUFVL.EFFECTIVE_START_DATE(+) AND HAOUFVL.EFFECTIVE_END_DATE(+)
AND TRUNC(SYSDATE) BETWEEN PPLF.EFFECTIVE_START_DATE(+) AND PPLF.EFFECTIVE_END_DATE(+)
AND TRUNC(SYSDATE) BETWEEN PJFFVL.EFFECTIVE_START_DATE(+) AND PJFFVL.EFFECTIVE_END_DATE(+)
AND TRUNC(SYSDATE) BETWEEN PJFV.EFFECTIVE_START_DATE(+) AND PJFV.EFFECTIVE_END_DATE(+)
AND TRUNC(SYSDATE) BETWEEN HOIF.EFFECTIVE_START_DATE(+) AND HOIF.EFFECTIVE_END_DATE(+)
AND TRUNC(SYSDATE) BETWEEN PAF.EFFECTIVE_START_DATE(+) AND PAF.EFFECTIVE_END_DATE(+)
AND TRUNC(SYSDATE) BETWEEN PPAUF.EFFECTIVE_START_DATE(+) AND PPAUF.EFFECTIVE_END_DATE(+)
AND TRUNC(SYSDATE) BETWEEN PGFTL.EFFECTIVE_START_DATE(+) AND PGFTL.EFFECTIVE_END_DATE(+)
AND TRUNC(SYSDATE) BETWEEN PJF.EFFECTIVE_START_DATE(+) AND PJF.EFFECTIVE_END_DATE(+)
AND TRUNC(SYSDATE) BETWEEN PLE.EFFECTIVE_START_DATE AND PLE.EFFECTIVE_END_DATE
AND TRUNC(SYSDATE) BETWEEN HAOUF.EFFECTIVE_START_DATE(+) AND HAOUF.EFFECTIVE_END_DATE(+)
AND TRUNC(SYSDATE) BETWEEN HLA.EFFECTIVE_START_DATE(+) AND HLA.EFFECTIVE_END_DATE(+)
AND TRUNC(SYSDATE) BETWEEN PJLG.EFFECTIVE_START_DATE(+) AND PJLG.EFFECTIVE_END_DATE(+)
AND TRUNC(SYSDATE) BETWEEN PASF.EFFECTIVE_START_DATE(+) AND PASF.EFFECTIVE_END_DATE(+)
ORDER BY PAPF.PERSON_NUMBER
Run the query in a SQL client that will generate a more detailed error message. For example, the below code is run in SQL*Plus, which correctly indicates that the problem is with the subquery on line 3.
SQL> select
2 (select 1 from dual) this_will_work,
3 (select 1 from dba_objects) this_will_fail,
4 (select 1 from dual) this_will_work
5 from dual;
(select 1 from dba_objects) this_will_fail,
*
ERROR at line 3:
ORA-01427: single-row subquery returns more than one row

Oracle Add procedure using pl/sql

The Procedure is like below!
I'm new to oracle. As i copy the sqlserver procedure to oracle and change some parts. It will be a great thxs help me solve this !
CREATE OR REPLACE PROCEDURE SP_GetAdminResource
(
AdminId IN NVARCHAR2,
p_ResultSet OUT sys_refcursor
)
AS
BEGIN
WITH T AS(
SELECT T1.ResId, T1.UpResId
FROM SYS_Resource T1
WHERE T1.IsActive = 1
AND T1.ResId IN (SELECT DISTINCT T2.ResId
FROM SYS_RoleResource T2
WHERE T2.RoleId IN
(SELECT T3.RoleId
FROM SYS_RoleAdministrator T3, SYS_Role T10
WHERE T10.RoleId = T3.RoleId
AND T10.IsActive = 1
AND T3.AdminId = AdminId))),
TT AS (SELECT *
FROM T
UNION ALL (SELECT T4.ResId, T4.UpResId
FROM SYS_Resource T4, T
WHERE T4.IsActive = 1
AND T4.ResId = T.UpResId)),
SELECT T5.ResId, T5.UpResId, T5.ResIcon,T5.ResName, T5.ResUrl,T5.OrderNum,T8.ActionCode
FROM SYS_Resource T5 INTO p_ResultSet
LEFT JOIN (SELECT T6.ResId, T6.ActionCode
FROM SYS_RoleResource T6
WHERE T6.RoleId IN
(SELECT T7.RoleId
FROM SYS_RoleAdministrator T7, SYS_Role T9
WHERE T9.RoleId = T7.RoleId
AND T9.IsActive = 1
AND T7.AdminId = AdminId)) T8 ON T5.ResId =
T8.ResId
WHERE T5.IsActive = 1
AND T5.ResId IN (SELECT DISTINCT TT.ResId FROM TT)
ORDER BY T5.OrderNum ASC, T5.ResName ASC;
END SP_GetAdminResource;
Check below the working version of your procedure. I have put my comments in between . Please go through it and work accordingly.
CREATE OR REPLACE PROCEDURE SP_GetAdminResource
(
AdminId IN NVARCHAR2,
p_ResultSet OUT sys_refcursor
)
AS
BEGIN
Open p_ResultSet for -- this is the way to use refcursor in your procedure
WITH T AS(
SELECT T1.ResId, T1.UpResId
FROM SYS_Resource T1
WHERE T1.IsActive = 1
AND T1.ResId IN (SELECT DISTINCT T2.ResId
FROM SYS_RoleResource T2
WHERE T2.RoleId IN
(SELECT T3.RoleId
FROM SYS_RoleAdministrator T3, SYS_Role T10
WHERE T10.RoleId = T3.RoleId
AND T10.IsActive = 1
AND T3.AdminId = t2.AdminId))), ----Check which AdminId need to be joined.I joined it with T2
TT AS (SELECT T.ResId, T.UpResId
FROM T
UNION ALL (SELECT T4.ResId, T4.UpResId
FROM SYS_Resource T4, T
WHERE T4.IsActive = 1
AND T4.ResId = T.UpResId) )
-- , ----Extra Need to remove it
SELECT T5.ResId,
T5.UpResId
-- T5.ResIcon, ---Uncomment these columns in your query, I just made tables to test without these columns.
-- T5.ResName,
-- T5.ResUrl,
-- T5.OrderNum,
-- T8.ActionCode
FROM SYS_Resource T5
-- INTO p_ResultSet -------------------------------No need to fetch into sysrefcursor.
LEFT JOIN (SELECT T6.ResId, T6.ActionCode
FROM SYS_RoleResource T6
WHERE T6.RoleId IN
(SELECT T7.RoleId
FROM SYS_RoleAdministrator T7, SYS_Role T9
WHERE T9.RoleId = T7.RoleId
AND T9.IsActive = 1
AND T7.AdminId = t6.AdminId)) T8 ON T5.ResId = ------Check which AdminId need to be joined.I joined it with T6
T8.ResId
WHERE T5.IsActive = 1
AND T5.ResId IN (SELECT DISTINCT TT.ResId FROM TT)
ORDER BY T5.OrderNum ASC, T5.ResName ASC ;
END SP_GetAdminResource;

Result set different from query than from stored procedure in Oracle

I have a sp that is giving me wrong results. That sp returns a cursor which is missing some rows. Now
When we fire the query within the sp directly in a query window, correct results come up. The only difference in this query is that instead of a cursor, the query directly outputs the result set; i.e. no cursor is opened when we fire the query directly.
When we recompile the sp, correct results come up for some time, and then it is back to missing rows after some time.
Have not been able to make much progress on this. Any pointers on what to look for, how to make progress etc. are much appreciated. I can paste the sp in here if needed, and probably the sp can be rewritten in some other way to make the problem go away, but I am more interested in knowing root cause - what could be causing this strange problem?
SP
CREATE OR REPLACE PROCEDURE "SP_GETTAXSETTINGCHANNELINFO" (v_bid varchar2,
p_rdTaxSetting out SYS_REFCURSOR) as
vT_bid varchar2(100);
begin
vT_bid := v_bid;
Open p_rdTaxSetting for
Select taxmappingid, CHANNELNAME,PROPERTYNAME, PROPERTYID, TAXTYPE, ISPARTIALLYINCLUSIVE, PARTIALLYINCLUSIVEVALUE
FROM (
WITH ChannelData AS (
SELECT ONDEMAND_SELECTED_SOURCES AS OnDemandChannels,
SCHEDULED_SELECTED_SOURCES AS ScheduledChannels
FROM SUB_ORDER S
INNER JOIN CONTACT C ON S.Supplier_ID = C.Contact_ID
WHERE C.BID = vT_bid
AND S.STATUS='Complete' and current_ind = 1
),
Property_Data As (
SELECT
SC.PROPERTY_NUM AS PropertyID
,SC.Company_Name as PropertyName
,SC.competitor_number as PropertyOrder
FROM SUB_ORDER S
INNER JOIN CONTACT C ON S.Supplier_ID = C.Contact_ID
INNER JOIN SUB_ORDER_COMPETITOR SC ON S.Order_Id = SC.Order_ID
WHERE C.BID = vT_bid
AND S.STATUS='Complete' and current_ind = 1
),
ODChannel as (
select regexp_substr (OnDemandChannels, '[^,]+', 1, rn) as Channel
from (Select OnDemandChannels From ChannelData)
cross join
(select rownum rn
from (select max (length (regexp_replace (OnDemandChannels, '[^,]+'))) + 1 max_value
from (Select OnDemandChannels From ChannelData))
connect by level <= max_value)
where regexp_substr (OnDemandChannels, '[^,]+', 1, rn) is not null
),
SCDChannel As (
select regexp_substr (ScheduledChannels, '[^,]+', 1, rn) as Channel
from (Select ScheduledChannels From ChannelData)
cross join
(select rownum rn
from (select max (length (regexp_replace (ScheduledChannels, '[^,]+'))) + 1 max_value
from (Select ScheduledChannels From ChannelData))
connect by level <= max_value)
where regexp_substr (ScheduledChannels, '[^,]+', 1, rn) is not null
),
ChaData1 As (
Select
CASE lower(Channel)
WHEN 'galileo' then 'GDS'
else Channel
end AS Channel
from ODChannel
),
ChaData2 As (
Select CASE lower(Channel)
WHEN 'galileo' then 'GDS'
else Channel
end AS Channel FROM SCDChannel
),
PropData As (
Select Distinct PropertyID,PropertyName from Property_Data
order by PropertyID
),
AllChannel AS (
Select Channel From ChaData1
union
Select Channel From ChaData2
),
Prop_Cha_Data As (
Select Distinct Channel,PropertyID,PropertyName from AllChannel,Property_Data
order by Channel,PropertyID
),
Tax_Channel_Prop_Exists AS ( select
CASE lower(CHANNELNAME)
WHEN 'galileo' then 'GDS'
else CHANNELNAME
end AS CHANNELNAME,
PropertyName As PROPERTYNAME, TCP.PROPERTYID As PROPERTYID,TAXTYPE,
ISPARTIALLYINCLUSIVE, PARTIALLYINCLUSIVEVALUE,taxmappingid
from TAXSETTING_CHANNEL_PROPERTY TCP
INNER JOIN AllChannel PC On TRIM (CASE lower(TCP.CHANNELNAME)
WHEN 'galileo' then 'GDS'else TCP.CHANNELNAME end) = TRIM(PC.Channel)
INNER JOIN PropData CP On TCP.PROPERTYID = CP.PropertyID
where TCP.BID=vT_bid
)
Select Distinct taxmappingid, CHANNELNAME,PROPERTYNAME, PROPERTYID, TAXTYPE, ISPARTIALLYINCLUSIVE, PARTIALLYINCLUSIVEVALUE from Tax_Channel_Prop_Exists
UNION ALL
select DISTINCT 0 As taxmappingid, PCD.Channel As CHANNELNAME,
PCD.PropertyName As PROPERTYNAME ,
PCD.propertyid As PROPERTYID,
-1 As TAXTYPE,
0 As ISPARTIALLYINCLUSIVE,
'' As PARTIALLYINCLUSIVEVALUE
FROM Prop_Cha_Data PCD
WHERE NOT EXISTS (
Select taxmappingid FROM Tax_Channel_Prop_Exists E
WHERE E.channelname = PCD.Channel AND
E.propertyid = PCD.propertyid )
)
Order by PROPERTYNAME,CHANNELNAME ;
end SP_GetTaxSettingChannelInfo;

Sql recursive error invalid column name CTE

I have done following CTE it works fine,but I need to get sum of the last node so the problem is when I add T1.Debit Column to be calculated its give me an invalid column name 'Debit' !!! on line ***
ALTER Function [dbo].[SubTopics_GetSum]
-- Add the parameters for the stored procedure here
(
#TopicID int
)
RETURNS TABLE
AS
RETURN
(
-- Add the SELECT statement with parameter references here
WITH cte
AS (
SELECT
T1.TopicID,
T1.Code,
T1.Description,
T1.ParentID,
T1.ParentID AS NewParentID,
CAST(T1.Code AS nvarchar(MAX)) AS TopicCode,
CAST(T1.Description AS nvarchar(MAX)) AS TopicDescription,
isnull((Accounting.DocumentDetail.Debit),0) AS Debit,
isnull((Accounting.DocumentDetail.Credit),0) AS Credit
FROM Accounting.Topics AS T1
LEFT OUTER JOIN Accounting.DocumentDetail
ON T1.TopicID = Accounting.DocumentDetail.TopicFK
where NOT EXISTS(
SELECT
T2.TopicID,
T2.Code,
T2.Description,
T2.ParentID,
isnull((Accounting.DocumentDetail.Debit),0) AS Debit,
isnull((Accounting.DocumentDetail.Credit),0) AS Credit
FROM Accounting.Topics AS T2
LEFT OUTER JOIN Accounting.DocumentDetail
ON T2.TopicID = Accounting.DocumentDetail.TopicFK
WHERE (ParentID = T1.TopicID)
)
UNION ALL
SELECT
c.TopicID,
c.Code,
c.Description,
c.ParentID,
T1.ParentID AS NewParentID,
CAST(T1.Code AS nvarchar(MAX)) + c.TopicCode AS TopicCode,
CAST(T1.Description AS nvarchar(MAX)) + ' - ' + c.TopicDescription AS TopicDescription,
*** isnull((T1.Debit),0)+isnull(c.Debit,0) AS Debit,--IN THIS LINE error 'Invalid Column Name 'Debit''
isnull(c.Credit,0) AS Credit
FROM cte AS c
INNER JOIN Accounting.Topics AS T1
ON T1.TopicID = c.NewParentID
)
SELECT isnull(sum(Debit),0)AS Debit,
isnull(sum(Credit),0)AS Credit
FROM cte AS c
WHERE (NewParentID = #TopicID)
)
let me know whats wrong with my code confused !!!
actually it doesn't return me a last node sum of debit,credit ... !!!
check following pic
I think below code will help,
ALTER FUNCTION [dbo].[Subtopics_getsum]
-- Add the parameters for the stored procedure here
(#TopicID INT)
returns TABLE
AS
RETURN (
-- Add the SELECT statement with parameter references here
WITH cte
AS (SELECT T1.topicid,
T1.code,
T1.description,
T1.parentid,
T1.parentid AS
NewParentID
,
Cast(T1.code AS NVARCHAR(max))
AS TopicCode,
Cast(T1.description AS NVARCHAR(max)) AS
TopicDescription,
Isnull(( accounting.documentdetail.debit ), 0) AS Debit,
Isnull(( accounting.documentdetail.credit ), 0) AS Credit
FROM accounting.topics AS T1
LEFT OUTER JOIN accounting.documentdetail
ON T1.topicid =
accounting.documentdetail.topicfk
WHERE NOT EXISTS(SELECT T2.topicid,
T2.code,
T2.description,
T2.parentid,
Isnull(( accounting.documentdetail.debit ), 0)
AS
Debit,
Isnull(( accounting.documentdetail.credit ), 0) AS
Credit
FROM accounting.topics AS T2
LEFT OUTER JOIN accounting.documentdetail
ON T2.topicid =
accounting.documentdetail.topicfk
WHERE ( parentid = T1.topicid )))
SELECT Isnull(Sum(debit), 0) AS Debit,
Isnull(Sum(credit), 0)AS Credit
FROM cte AS c
WHERE ( newparentid = #TopicID )
)
I think you Accounting.Topics hasn't column Debit.
UPDTAE
isnull((T1.Debit),0)+isnull(c.Debit,0) AS Debit,--IN THIS LINE error 'Invalid Column Name 'Debit''
isnull(c.Credit,0) AS Credit
FROM cte AS c
INNER JOIN Accounting.Topics AS T1
ON T1.TopicID = c.NewParentID
In the code above your T1.Debit refers to Accounting.Topics.

Resources