Error 00904. 00000 - "%s: invalid identifier" in plsql - plsql
I have this procedure:
create or replace procedure multiplicate_mts_api_data(number_of_runs NUMBER) AS
v_number_of_runs Number;
v_active_mts_table Varchar2(100);
v_product Varchar2(30) := 'TMP';
Begin
IF number_of_runs > 3 then
v_number_of_runs := 3;
else
v_number_of_runs := number_of_runs;
END IF;
select value into v_active_mts_table from settings where setting = 'mts.data.table';
For i IN 1..v_number_of_runs
Loop
execute IMMEDIATE ' insert into ' || v_active_mts_table || ' (MTSID ,
PRODUCT,
SERVICELEVEL,
CHARGECODE ,
CHARGECODENAME,
CHARGECODELOCALNAME,
ORIGINZONE,
DESTINATIONZONE ,
CATEGORY ,
SHIPPERCOUNTRY ,
ORIGINCOUNTRY ,
ORIGINSTATION ,
CONSIGNEECOUNTRY ,
DESTINATIONCOUNTRY,
DESTINATIONSTATION ,
AIRLINE ,
DIRECTFLIGHT,
ROUTING ,
FREQUENCY ,
DIRECTMOVEMENT,
FREQUENCYUNIT ,
PRICINGLEVEL ,
CAMPAIGNNAME ,
TRUCKTYPE ,
WEIGHTRATIO ,
CURRENCY ,
CHARGECODEINFOFLAG,
COSTBASE,
MINRATE,
MAXRATE,
UNIT ,
RATEPERUNIT,
RATEWM ,
RATEPERSHIPMENT,
RATEPERENTRY ,
NUMFREELINESINCLUDED,
RATEPERLINEITEM,
PERCENTAGE ,
CHAGECODEREFERENCE,
ALLIN ,
RATEFORDEDICATEDVEHICLE ,
TRUCKSIZEMETRICTONES,
INCLUSIVETOFRT ,
ONREQUEST ,
NOTES ,
TRANSITTIMEUNIT,
TRANSITTIME,
TRANSITTIMECFSTOPORT ,
TRANSITTIMEPORTTOCFS ,
VALIDFROM,
VALIDTO,
WBMIN ,
WBMAX,
OPERATOR1,
AMOUNT1,
UNITPRICE1 ,
OPERATOR2 ,
AMOUNT2 ,
UNITPRICE2,
OPERATOR3 ,
AMOUNT3,
UNITPRICE3 ,
OPERATOR4,
AMOUNT4,
UNITPRICE4 ,
OPERATOR5,
AMOUNT5,
UNITPRICE5 ,
OPERATOR6,
AMOUNT6 ,
UNITPRICE6 ,
OPERATOR7,
AMOUNT7 ,
UNITPRICE7 ,
OPERATOR8,
AMOUNT8 ,
UNITPRICE8,
OPERATOR9,
AMOUNT9 ,
UNITPRICE9,
OPERATOR10,
AMOUNT10 ,
UNITPRICE10,
DISCOUNT1 ,
DENSITY1,
DISCOUNT2,
DENSITY2,
DISCOUNT3,
DENSITY3 ,
DISCOUNT4 ,
DENSITY4 ,
DISCOUNT5 ,
DENSITY5 ,
DISCOUNT6 ,
DENSITY6 ,
DISCOUNT7 ,
DENSITY7 ,
DISCOUNT8 ,
DENSITY8 ,
DISCOUNT9 ,
DENSITY9 ,
DISCOUNT10 ,
DENSITY10 ,
DISCOUNT11 ,
DENSITY11 ,
DISCOUNT12 ,
DENSITY12 ,
ORIGINSTATIONNAME ,
DESTINATIONSTATIONNAME ,
CARRIER ,
CONTAINERTYPE ,
RATEPERCONTAINER ,
RATEPERCONTAINERPERDAY ,
FREETIME ,
FREETIMEUNIT ,
ORIGINLOCALITY ,
ORIGINZIPCODEFROM ,
ORIGINZIPCODETO ,
WEIGHTBREAK ,
WEIGHTBREAKUNIT ,
TEUTHERESHOLDFORRATEAPPLICABILITY ,
TRANSHIPMENTPORT ,
FREQUENCYOFSAILINGS ,
FREQUENCYOFSAILINGSUNIT ,
DESTINATIONLOCALITY ,
DESTINATIONZIPCODEFROM ,
DESTINATIONZIPCODETO ,
UPDATEDTS ,
ORIGINZONELR ,
DESTINATIONZONELR ,
CYTOCYTRANSITTIME ,
COMMODITYCODE ,
COMMODITYDESC ,
DEMURRAGEANDDETENTIONCOMBINEDFLAG ,
NUMBEROFFREEDAYS ,
EXCLUDEDDAYS ,
RATEPERWMPERDAY ,
SERVICELEVELCODE ,
ADDITIONALCHARGECODESINCLUDED ,
AFPTRANSITTIME ,
AFPBASIC ,
AFPMINMFSC ,
AFPRATEPERKGMFSC ,
AFPCOSTWEIGHTBASEMFSC ,
AFPMINMSSC ,
AFPRATEPERKGMSSC ,
AFPCOSTWEIGHTBASEMSSC ,
AFPRATEPERENTRYOCLE ,
AFPRATEPERLINEITEMOCAL ,
AFPRATEPERENTRYDCLE ,
AFPRATEPERLINEITEMDCAL ,
AFPchargeCodeCargoScreening,
AFPminCargoScreening,
AFPratePerKgCargoScreening,
AFPratePerShptCargoScreening,
AFPchargeCodeDGSurcharge,
AFPminDGSurcharge,
AFPratePerKgDGSurcharge,
AFPratePerShptDGSurcharge,
AFPCargoScreeningFlag,
AFPDGSurchargeFlag,
AFPCo2e,
AFPChargecodeBiofuel,
AFPPerKgBiofuel,
AFPBioFuelFlag,
AFPChargecodeOffsetting,
AFPPerKgOffsetting,
AFPOffsettingFlag,
AFPChargecodeBiofuelOffsetting,
AFPPerKgBiofuelOffsetting,
AFPBioFuelOffsettingFlag,
RATEPERTON ) select ID_GENERATOR.nextval ,
'|| v_product ||' ,
SERVICELEVEL,
CHARGECODE ,
CHARGECODENAME,
CHARGECODELOCALNAME,
ORIGINZONE,
DESTINATIONZONE ,
CATEGORY ,
SHIPPERCOUNTRY ,
ORIGINCOUNTRY ,
ORIGINSTATION ,
CONSIGNEECOUNTRY ,
DESTINATIONCOUNTRY,
DESTINATIONSTATION ,
AIRLINE ,
DIRECTFLIGHT,
ROUTING ,
FREQUENCY ,
DIRECTMOVEMENT,
FREQUENCYUNIT ,
PRICINGLEVEL ,
CAMPAIGNNAME ,
TRUCKTYPE ,
WEIGHTRATIO ,
CURRENCY ,
CHARGECODEINFOFLAG,
COSTBASE,
MINRATE,
MAXRATE,
UNIT ,
RATEPERUNIT,
RATEWM ,
RATEPERSHIPMENT,
RATEPERENTRY ,
NUMFREELINESINCLUDED,
RATEPERLINEITEM,
PERCENTAGE ,
CHAGECODEREFERENCE,
ALLIN ,
RATEFORDEDICATEDVEHICLE ,
TRUCKSIZEMETRICTONES,
INCLUSIVETOFRT ,
ONREQUEST ,
NOTES ,
TRANSITTIMEUNIT,
TRANSITTIME,
TRANSITTIMECFSTOPORT ,
TRANSITTIMEPORTTOCFS ,
VALIDFROM,
VALIDTO,
WBMIN ,
WBMAX,
OPERATOR1,
AMOUNT1,
UNITPRICE1 ,
OPERATOR2 ,
AMOUNT2 ,
UNITPRICE2,
OPERATOR3 ,
AMOUNT3,
UNITPRICE3 ,
OPERATOR4,
AMOUNT4,
UNITPRICE4 ,
OPERATOR5,
AMOUNT5,
UNITPRICE5 ,
OPERATOR6,
AMOUNT6 ,
UNITPRICE6 ,
OPERATOR7,
AMOUNT7 ,
UNITPRICE7 ,
OPERATOR8,
AMOUNT8 ,
UNITPRICE8,
OPERATOR9,
AMOUNT9 ,
UNITPRICE9,
OPERATOR10,
AMOUNT10 ,
UNITPRICE10,
DISCOUNT1 ,
DENSITY1,
DISCOUNT2,
DENSITY2,
DISCOUNT3,
DENSITY3 ,
DISCOUNT4 ,
DENSITY4 ,
DISCOUNT5 ,
DENSITY5 ,
DISCOUNT6 ,
DENSITY6 ,
DISCOUNT7 ,
DENSITY7 ,
DISCOUNT8 ,
DENSITY8 ,
DISCOUNT9 ,
DENSITY9 ,
DISCOUNT10 ,
DENSITY10 ,
DISCOUNT11 ,
DENSITY11 ,
DISCOUNT12 ,
DENSITY12 ,
ORIGINSTATIONNAME ,
DESTINATIONSTATIONNAME ,
CARRIER ,
CONTAINERTYPE ,
RATEPERCONTAINER ,
RATEPERCONTAINERPERDAY ,
FREETIME ,
FREETIMEUNIT ,
ORIGINLOCALITY ,
ORIGINZIPCODEFROM ,
ORIGINZIPCODETO ,
WEIGHTBREAK ,
WEIGHTBREAKUNIT ,
TEUTHERESHOLDFORRATEAPPLICABILITY ,
TRANSHIPMENTPORT ,
FREQUENCYOFSAILINGS ,
FREQUENCYOFSAILINGSUNIT ,
DESTINATIONLOCALITY ,
DESTINATIONZIPCODEFROM ,
DESTINATIONZIPCODETO ,
UPDATEDTS ,
ORIGINZONELR ,
DESTINATIONZONELR ,
CYTOCYTRANSITTIME ,
COMMODITYCODE ,
COMMODITYDESC ,
DEMURRAGEANDDETENTIONCOMBINEDFLAG ,
NUMBEROFFREEDAYS ,
EXCLUDEDDAYS ,
RATEPERWMPERDAY ,
SERVICELEVELCODE ,
ADDITIONALCHARGECODESINCLUDED ,
AFPTRANSITTIME ,
AFPBASIC ,
AFPMINMFSC ,
AFPRATEPERKGMFSC ,
AFPCOSTWEIGHTBASEMFSC ,
AFPMINMSSC ,
AFPRATEPERKGMSSC ,
AFPCOSTWEIGHTBASEMSSC ,
AFPRATEPERENTRYOCLE ,
AFPRATEPERLINEITEMOCAL ,
AFPRATEPERENTRYDCLE ,
AFPRATEPERLINEITEMDCAL ,
AFPchargeCodeCargoScreening,
AFPminCargoScreening,
AFPratePerKgCargoScreening,
AFPratePerShptCargoScreening,
AFPchargeCodeDGSurcharge,
AFPminDGSurcharge,
AFPratePerKgDGSurcharge,
AFPratePerShptDGSurcharge,
AFPCargoScreeningFlag,
AFPDGSurchargeFlag,
AFPCo2e,
AFPChargecodeBiofuel,
AFPPerKgBiofuel,
AFPBioFuelFlag,
AFPChargecodeOffsetting,
AFPPerKgOffsetting,
AFPOffsettingFlag,
AFPChargecodeBiofuelOffsetting,
AFPPerKgBiofuelOffsetting,
AFPBioFuelOffsettingFlag,
RATEPERTON from ' || v_active_mts_table || ' where product != '|| v_product;
End loop;
End;
When I try to run it, I obtain error:
Error report -
ORA-00904: "TMP": invalid identifier
ORA-06512: at "DEV_CRDB.MULTIPLICATE_MTS_API_DATA", line 17
ORA-06512: at line 2
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
Can someone please give me a point how to solve this issue? If I try to run this insert via sql developer, it runs smoothly. Im beginner in plsql, so please be patient if this is dummy question. Maybe there is a problem with sql insert concatenation, I dont know. Thank you Petr
This could be it:
Looking at the error:
ORA-00904: "TMP": invalid identifier
ORA-06512: at "DEV_CRDB.MULTIPLICATE_MTS_API_DATA", line 17
You're assigning TMP to a variable here:
v_product Varchar2(30) := 'TMP';
and then referencing v_product here:
RATEPERTON ) select ID_GENERATOR.nextval ,
'|| v_product ||' ,
SERVICELEVEL,
It looks like the above line is causing the error. The reason is that there are no quotes around "v_product" - just the quotes needed for the concatenation. So the select becomes
...
SELECT ID_GENERATOR.nextval ,
TMP,
SERVICELEVEL,
...
and there probably is not column called "TMP" in that table which raises the error.
My guess is you want to select the value "TMP", and not the column TMP. The value "TMP" needs to be enclosed in quotes and those quotes need to be escaped so try the following instead. :
RATEPERTON ) select ID_GENERATOR.nextval ,
'''|| v_product ||''' ,
SERVICELEVEL,
Give that a try.
Related
Insert from remote table using unpivot
So, there's a table for scheduling in our organization and reporting data from it. A schedule line has a column for each day of the week with values of 'Y' or 'N' depending on whether or not there is a meeting on that day. I have managed to use UNPIVOT to do this in a simple select, but, unfortunately, our needs are more complex than that. I need to insert into a local table with a query from the schedule table on a remote table over a DB_Link, which is unpivoted, something like: SELECT * FROM (SELECT class_nbr, TERM, MON, TUES, WED, THURS, FRI, SAT, SUN FROM schedule_tbl) sched UNPIVOT (wkDay FOR DayWeek IN (MON, TUES, WED, THURS, FRI, SAT, SUN)) piv WHERE piv.WkDay = 'Y' Again, this works fine when I run it in Sql Developer against the server where the schedule_tbl lives. I get results like Class_nbr TERM DayWeek 1234 Fall MON 1234 Fall Wed .... However, amazingly, when I try to insert the result of this from a different server like INSERT INTO other_table (class_nbr, TERM, DayWeek) SELECT * FROM (SELECT class_nbr, TERM, MON, TUES, WED, THURS, FRI, SAT, SUN FROM schedule_tbl#db_link) sched UNPIVOT (wkDay FOR DayWeek IN (MON, TUES, WED, THURS, FRI, SAT, SUN)) piv WHERE piv.WkDay = 'Y' what happens is that only rows with DayWeek = 'MON' are inserted. The actual number of Monday rows which should be inserted are. I'm not sure where this is breaking down. If I change the order of the UNPIVOT columns, making TUES the first one, then only the Tuesday rows are inserted (again, the correct number). Edit: I think I can share my actual code here. Keep in mind that if I take this exact query to the remote database and remove the dblinks, I am getting more than just Monday rows: SELECT '%processinstance', 0 , crse_id , crse_offer_nbr , strm , session_code , class_section , subject , institution , facility_id , ' ' , ' ' , '0001484' , '4' , PDB_DE0120_TRM_ID , subject || catalog_nbr , strm || class_nbr , to_char ( start_dt , 'YYYYMMDD' ) , to_char ( end_dt , 'YYYYMMDD' ) , DECODE(DAYWEEK,'MON', 'M', 'TUES','T', 'WED','W', 'THURS','R', 'FRI','F', 'SAT','S', 'SUN','X'), nvl ( to_char ( meeting_time_start ,'HH24MI' ) ,'9999' ) , nvl ( to_char ( meeting_time_end ,'HH24MI' ) ,'9999' ) , ' ' , ' ' , ' ' , 0 , 'S' , 'C' , decode ( instruction_mode ,'HB' ,'X' ,'DL' ,'X' ,'N' ) , ' ' , ' ' , ' ' , ' ' , ' ' , 'E' , SYSDATE , ' ' , SYSDATE, ' ' , ' ' FROM (SELECT A.crse_id , A.crse_offer_nbr , A.strm , A.session_code , A.class_section , B.institution , A.facility_id , B.instruction_mode, B.SUBJECT, B.CATALOG_NBR, A.meeting_time_start, A.meeting_time_end, B.START_DT, B.END_DT, B.CLASS_NBR, C.PDB_DE0120_TRM_ID, MON, TUES, WED, THURS, FRI, SAT, SUN FROM PS_CLASS_MTG_PAT#CSDV1 A INNER JOIN PS_CLASS_TBL#CSDV1 B ON A.STRM = B.STRM AND A.CRSE_ID = B.CRSE_ID AND A.CLASS_SECTION = B.CLASS_SECTION INNER JOIN PS_FSC_SRPT_TRM_VW#CSDV1 C ON C.STRM = B.STRM AND C.ACAD_CAREER = 'CRED' WHERE A.STRM = '2182' AND (B.CLASS_STAT <> 'X' OR (B.ENRL_TOT > 0 AND B.ENRL_STAT <> 'C')) AND B.acad_group NOT IN ( '11','12','13','23','80','99' )) B UNPIVOT EXCLUDE NULLS (wkDay FOR DayWeek IN ( MON AS 'MON', TUES AS 'TUES', WED AS 'WED', THURS AS 'THURS', FRI AS 'FRI', SAT AS 'SAT', SUN AS 'SUN')) piv WHERE piv.wkDay = 'Y'; Second update: As requested, I am pasting the corrected query: SELECT '%processinstance', 0 , crse_id , crse_offer_nbr , strm , session_code , class_section , subject , institution , facility_id , ' ' , ' ' , '0001484' , '4' , PDB_DE0120_TRM_ID , subject || catalog_nbr , strm || class_nbr , to_char ( start_dt , 'YYYYMMDD' ) , to_char ( end_dt , 'YYYYMMDD' ) , DECODE(DAYWEEK,'MON', 'M', 'TUES','T', 'WED','W', 'THURS','R', 'FRI','F', 'SAT','S', 'SUN','X'), nvl ( to_char ( meeting_time_start ,'HH24MI' ) ,'9999' ) , nvl ( to_char ( meeting_time_end ,'HH24MI' ) ,'9999' ) , ' ' , ' ' , ' ' , 0 , 'S' , 'C' , decode ( instruction_mode ,'HB' ,'X' ,'DL' ,'X' ,'N' ) , ' ' , ' ' , ' ' , ' ' , ' ' , 'E' , ' ' , ' ' , ' ' FROM (SELECT A.crse_id , A.crse_offer_nbr , A.strm , A.session_code , A.class_section , B.institution , A.facility_id , B.instruction_mode, B.SUBJECT, B.CATALOG_NBR, A.meeting_time_start, A.meeting_time_end, B.START_DT, B.END_DT, B.CLASS_NBR, C.PDB_DE0120_TRM_ID, MON, TUES, WED, THURS, FRI, SAT, SUN FROM PS_CLASS_MTG_PAT#CSDV1 A INNER JOIN PS_CLASS_TBL#CSDV1 B ON A.STRM = B.STRM AND A.CRSE_ID = B.CRSE_ID AND A.CLASS_SECTION = B.CLASS_SECTION INNER JOIN PS_FSC_SRPT_TRM_VW#CSDV1 C ON C.STRM = B.STRM AND C.ACAD_CAREER = 'CRED' WHERE A.STRM = '2182' AND (B.CLASS_STAT <> 'X' OR (B.ENRL_TOT > 0 AND B.ENRL_STAT <> 'C')) AND B.acad_group NOT IN ( '11','12','13','23','80','99' )) B UNPIVOT EXCLUDE NULLS (wkDay FOR DayWeek IN ( MON AS 'MON', TUES AS 'TUES', WED AS 'WED', THURS AS 'THURS', FRI AS 'FRI', SAT AS 'SAT', SUN AS 'SUN')) piv WHERE piv.wkDay = 'Y';
This isn't a completely satisfactory solution, but it is working for me. If I remove two expressions from my select clause (any two columns), this issue disappears. This is obviously a previously unknown Oracle issue involving DB Links, as if I take the same query and run it on the remote server where the tables live it is not present, and there is nothing in the Oracle documentation regarding DB Links about this. I will at some point attempt to raise it with them.
Filter access report on subtotals
I'd like to filter my report on the Actual OT(Overtime) subtotal that is more than 30. How can i do it? Here's the pass-through query to SQL Server: SELECT OT_Entry.App_ID AS OT_Entry_App_ID , OT_Entry.EmpID , OT_Entry.EmpName , OT_Entry.[Pre-Approved_OT] , OT_Entry.Actual_OT , tblMain.App_ID AS tblMain_App_ID , tblMain.Start_Date , tblMain.End_Date , tblMain.WO_Number , tblMain.Plant_Section , tblMain.Job_Category , tblMain.[Maint_Area/Section] , tblMain.[Group/Discipline] , tblMain.Break_In , tblMain.Requestor , tblMain.Justification , tblMain.Work_Activity , tblMain.Approver , tblMain.Request_Date , EmpData.Position , EmpData.Reporting_to , EmpData.Company , EmpData.Personnel_Type , EmpData.Group FROM EmpData INNER JOIN (tblMain INNER JOIN OT_Entry ON tblMain.[App_ID] = OT_Entry.[App_ID]) ON EmpData.EmpID = OT_Entry.EmpID; Below is the screenshot of my report Report Screenshot
Oracle 11x Newbie - simple, but sincere...
First, thank you for taking the time to read this, I am fairly well experienced with most versions of MSSQL, but not so much with Oracle and PL SQL. My problem and question are this: I have a SSRS report in MSSQK2k12 that is calling a stored proc on an Oracle 11x db. there are several params passed in that are used in the where clause, I need to add one more param ( a simple Y/N) that will add additional filters to the where clause, effectively saying at the report prompt 'do you want to see all parts? (Y/N). A 'Y' answer will run the proc nearly wide open, including parts that have no inventory, parts on hand, but already sold, parts no longer active. A 'N' response will pass filters that require available inventory, valid current part class, etc. I have attempted a half-dozen seemingly good solutions, to no avail. I have asked an associate (who is well versed in PLSQL) for his advice, his resulting 'DECODE' addition did nothing. I would like to request advice/assistance that does not involve months of study, as I only have a few days to finish. Please note, the original SP was not my work, I have inherited from predecessor would did/could not complete. Stored procedure (Scrubbed) attached CREATE OR REPLACE PROCEDURE ORACLE_PROD.RPT_LIKE_PARTS_TEST ( p_Pos1 IN varchar2 , p_Description IN varchar2 , p_StartPos IN varchar2 , p_StartPosValue IN varchar2 , p_ViewAll IN varchar2 --ADDED to allow filtered return for data , p_recordset OUT SYS_REFCURSOR) AS BEGIN OPEN p_recordset FOR SELECT T1.ITEM , T1.REVISION , T1.DESCRIPTION , T2.CCN , T2.DELETED , T2.OBSOLETED , T2.FINGOOD , T2.ABC , T3.MAS_LOC , T3.LOCATION , (T3.OH_QTY - T3.COM_QTY) - T3.RESV_QTY as Avail_QTY , T1.USER_NUM1 , case when T2.HALT<>' ' then 'Y' else NULL end as Halt , T4.DESCRIPTION as T4_Description , det.OH_QTY as Det_OH_QTY , det.COM_QTY as Det_COM_QTY , DECODE(det.INSP_STAT,'3','Passed Inpection' ,DECODE det.INSP_STAT,'1','Waiting Inspection' ,' ')) as Inspect_Descr , T3.RESV_QTY from ITEM T1 LEFT OUTER JOIN ITEM_CCN T2 ON T1.ITEM=T2.ITEM and T1.REVISION=T2.REVISION LEFT OUTER JOIN ITEM_LOC T3 on T2.CCN=T3.CCN and T2.ITEM=T3.ITEM and T2.REVISION=T3.REVISION LEFT OUTER JOIN HALT T4 on T2.HALT=T4.HALT and T2.CCN=T4.CCN LEFT OUTER JOIN ITEM_DET det on T3.CCN=det.CCN and T3.ITEM=det.ITEM and T3.REVISION=det.REVISION and T3.MAS_LOC=det.MAS_LOC and T3.LOCATION=det.LOCATION where T2.OBSOLETED is null --** all commented parts are attempted adds --&&&and CASE (p_ViewAll) --&&& when 'N' --&&& THEN --T2.HALT = DECODE(T2.HALT,'DSGN', 'XXX',' ','XXX',T2.HALT) -- and --&&& (T2.HALT != 'DSGN' and (((T3.OH_QTY - T3.COM_QTY) - T3.RESV_QTY))> 0 --and T2.HALT <> 'DSGN' and T3.mas_loc <>'99' and T3.mas_loc <>' ' --&&&WHEN 'Y' --&&& THEN and T2.HALT <>'DSGN' --&&&ELSE NULL-- or T3.mas_loc <> '') --&&&END --** /*and T2.HALT = decode(p_ViewAll,'Y', DECODE(T2.HALT,'DSGN', T2.HALT), DECODE(T2.HALT,'DSGN', T2.HALT) --DECODE(T2.HALT,'DSGN', 'XXX',' ','XXX',T2.HALT), --DECODE(T2.HALT,'DSGN', 'XXX', T2.HALT) ) and (p_ViewAll != 'Y' or T3.mas_loc not in ('99',' '))*/ --** AND UPPER(TRIM(T1.ITEM)) LIKE (CASE WHEN LENGTH(TRIM(p_pos1)) > 0 THEN UPPER(TRIM(p_pos1) || '%') ELSE UPPER(TRIM(T1.ITEM))END) AND UPPER(T1.DESCRIPTION) LIKE (CASE WHEN LENGTH(p_Description) > 0 THEN UPPER(('%' || p_Description || '%')) ELSE UPPER(UPPER(T1.DESCRIPTION))END) AND (CASE WHEN TO_NUMBER(NVL(TRIM(p_StartPos),'0')) > 0 THEN SUBSTR(TRIM(T1.ITEM),TO_NUMBER(TRIM(p_StartPos)),NVL(LENGTH(UPPER(TRIM(p_StartPosValue))),'0')) ELSE 'False' END) = (CASE WHEN TO_NUMBER(NVL(TRIM(p_StartPos),'0')) > 0 THEN NVL(UPPER(TRIM(p_StartPosValue)),'') ELSE 'False' END) ORDER BY T1.ITEM , T1.revision desc ; END RPT_LIKE_PARTS_TEST; /
You have to use UNION and not decode. In one side of UNION you have to handle parameter Y with outer join and other will be as you have coded already. OR you need to use if / else of PLSQL since user can pass only one of them as parameter. To be honest, this question is nothing specific to oracle and mere SQL.
so with the union all or union solution probably looks something like this /* Formatted on 7/5/2016 1:50:34 PM (QP5 v5.256.13226.35510) */ SELECT T1.ITEM, T1.REVISION, T1.DESCRIPTION, T2.CCN, T2.DELETED, T2.OBSOLETED, T2.FINGOOD, T2.ABC, T3.MAS_LOC, T3.LOCATION, (T3.OH_QTY - T3.COM_QTY) - T3.RESV_QTY AS Avail_QTY, T1.USER_NUM1, CASE WHEN T2.HALT <> ' ' THEN 'Y' ELSE NULL END AS Halt, T4.DESCRIPTION AS T4_Description, det.OH_QTY AS Det_OH_QTY, det.COM_QTY AS Det_COM_QTY, DECODE (det.INSP_STAT, '3', 'Passed Inpection', DECODE det.INSP_STAT,'1','Waiting Inspection' ,' ')) as Inspect_Descr , T3.RESV_QTY from ITEM T1 LEFT OUTER JOIN ITEM_CCN T2 ON T1.ITEM=T2.ITEM and T1.REVISION=T2.REVISION LEFT OUTER JOIN ITEM_LOC T3 on T2.CCN=T3.CCN and T2.ITEM=T3.ITEM and T2.REVISION=T3.REVISION LEFT OUTER JOIN HALT T4 on T2.HALT=T4.HALT and T2.CCN=T4.CCN LEFT OUTER JOIN ITEM_DET det on T3.CCN=det.CCN and T3.ITEM=det.ITEM and T3.REVISION=det.REVISION and T3.MAS_LOC=det.MAS_LOC and T3.LOCATION=det.LOCATION where T2.OBSOLETED is null and p_ViewAll = 'Y' UNION ALL SELECT T1.ITEM , T1.REVISION , T1.DESCRIPTION , T2.CCN , T2.DELETED , T2.OBSOLETED , T2.FINGOOD , T2.ABC , T3.MAS_LOC , T3.LOCATION , (T3.OH_QTY - T3.COM_QTY) - T3.RESV_QTY as Avail_QTY , T1.USER_NUM1 , case when T2.HALT<>' ' then 'Y' else NULL end as Halt , T4.DESCRIPTION as T4_Description , det.OH_QTY as Det_OH_QTY , det.COM_QTY as Det_COM_QTY , DECODE(det.INSP_STAT,'3','Passed Inpection' ,DECODE det.INSP_STAT,'1','Waiting Inspection' ,' ')) as Inspect_Descr , T3.RESV_QTY from ITEM T1 LEFT OUTER JOIN ITEM_CCN T2 ON T1.ITEM=T2.ITEM and T1.REVISION=T2.REVISION LEFT OUTER JOIN ITEM_LOC T3 on T2.CCN=T3.CCN and T2.ITEM=T3.ITEM and T2.REVISION=T3.REVISION LEFT OUTER JOIN HALT T4 on T2.HALT=T4.HALT and T2.CCN=T4.CCN LEFT OUTER JOIN ITEM_DET det on T3.CCN=det.CCN and T3.ITEM=det.ITEM and T3.REVISION=det.REVISION and T3.MAS_LOC=det.MAS_LOC and T3.LOCATION=det.LOCATION where T2.OBSOLETED is null and p_ViewAll = 'N' and myfilterstuff = 'whatever'
Combining Tables of Different Schema based on ID in SQLite
I have a SQLite database with tables: Demographics, with Schema: "MRN", "Demographics","MaritalDescription" ,"DOB" ,"PCCCode" ,"DeceasedIND" ,"Death Date" ,"RaceDescription" ,"LanguageDescription" ,"SexDescription"); ED_Visit, with Schema: "MRN" , "VisitNo" , "AdmitTime" , "HospService" , "VisitStatus" , "VisitTime" , "Location" , "Clinic" , "Pt Type" , "Pt Status" , "EDSeverityDescription" , "EDDisp" , "Code" , "Description" ); I want to select all the records from ED_Visits which match on MRN with Demographics and merge them to Demographics. I have tried: SELECT VisitNo , AdmitTime , HospService , VisitStatus , VisitTime , Location , Clinic , "Pt Type", "Pt Status", EDSeverityDescription , EDDisp , Code , Description FROM ED_Visits INNER JOIN Demographics ON ED_Visits.MRN = Demographics.MRN UNION SELECT MaritalDescription, DOB, PCCCode, DeceasedIND, "Death Date", RaceDescription, LanguageDescription, SexDescription FROM Demographics LEFT OUTER JOIN ED_Visits ON Demographics.MRN = ED_Visits.MRN;
SQL Paging on more than 10 lac Records
I am using MS SQL 2008 R2. One of my table have more than 10 lac rows — 1 lac is 105 or 100,000, so 10 lac is 1,000,000). I want to bind this to ASP Gridview. I tried custom paging with page size and index. But grid not binded. Timeout Error occured. Tried directly execute stored procedure, but it takes a long time. How can I optimize this procedure ? My procedure ALTER PROCEDURE SP_LOAN_APPROVAL_GET_LIST #USERCODE NVARCHAR(50) , #FROMDATE DATETIME = NULL , #TODATE DATETIME = NULL , #PAGESIZE INT , #PAGENO INT , #TOTALROW BIGINT OUTPUT AS BEGIN SELECT * FROM ( SELECT DOC_NO , DOC_DATE_GRE , EMP_CODE , EMP_NAME_ENG as Name , LOAN_AMOUNT , DESC_ENG as Discription , REMARKS , ROW_NUMBER() OVER( ORDER BY ( SELECT 1 ) ) AS [ROWNO] from VW_PER_LOAN Where isnull( POST_FLAG , 'N' ) = 'N' and ISNULl( CANCEL_FLAG , 'N' ) != 'Y' and DOC_DATE_GRE between ISNULL(#FROMDATE , DOC_DATE_GRE ) and ISNULL(#TODATE , DOC_DATE_GRE ) and BRANCH in ( SELECT * FROM DBO.FN_SSP_GetAllowedBranches(#USERCODE) ) ) T WHERE T.ROWNO BETWEEN ((#PAGENO-1)*#PAGESIZE)+1 AND #PAGESIZE*(#PAGENO) SELECT #TOTALROW=COUNT(*) from VW_PER_LOAN Where isnull(POST_FLAG,'N')= 'N' and ISNULl(CANCEL_FLAG,'N')!='Y' and DOC_DATE_GRE between ISNULL(#FROMDATE,DOC_DATE_GRE)and ISNULL(#TODATE,DOC_DATE_GRE) and BRANCH in ( SELECT * FROM DBO.FN_SSP_GetAllowedBranches(#USERCODE) ) END Thanks
The first thing to do is to look at your execution plan and discuss it with a DBA if you don't understand it. The obvious thing that stands out is that your where clause has pretty much every column reference wrapped in some sort of function. That makes them expressions and make the SQL optimizer unable to use any covering indices that might exist. It looks like you are calling a table-valued function as an uncorrelated subquery. That would worry me with respect to performance. I'd probably move that out of the query. Instead run it just once and populate a temporary table.