Oracle-Conveting SQL to ANSI SQL - sql

This is regarding converting query to ANSI SQL. I tried writing this query with Oracle old syntax and it threw the following error so I ended up changing it as shown below . After researching, I found that ANSI SQL supports such requirements.
ERROR :a table may be outer joined to at most one other table tips
Here is the query that I wrote which is working but it would be great to know if there are ways this can be re-written in ANSI-SQL or by using old outer join syntax. I am looking for the union queries to be re-written as others in the with clause work fine.
WITH BOM_PARENT AS (
SELECT MX.SAP_MATNR MATNR
, TRIM(I.IPRODUCTION) IPRODUCTION
, TRIM(I.IDRAWING) IDRAWING
, TRIM(M.BCHLD) BCHLD
, M.BSEQ BSEQ
, PX.WERKS WERKS
, M.BPSCP MENGE
, UX.SAP_UOM MEINS
, I.IUMS
FROM XX_MAIN.XX_BPCS_IIM I
, XX_MAIN.XX_BPCS_MPB M
, XX_MAIN.XX_MATER_XREF MX
, XX_MAIN.XX_TRUNK_XREF PX
, XX_MAIN.XX_MP_UNIT_XREF UX
WHERE TRIM(I.IPRODUCTION) = TRIM(M.BPROD)
AND TRIM(MX.PROD_MATNR) = TRIM(I.IPRODUCTION)
AND MX.CONV_FACTOR = TO_CHAR(I.IUMCN)
AND I.IUMS = UX.LEGACY_UOM
AND UX.SOURCE = 'AP'
AND I.SOURCE = PX.SOURCE
AND I.ENTITY = PX.LEGACY_PLANT
AND I.ENTITY = 'SG'
AND I.IITYP = '4'
--AND PX.WERKS IN ('1379')
AND MX.SAP_MTART <> 'ZPRD'
)
, BOM_CHILDS AS
(
SELECT B.*,
X.SAP_MATNR IDNRK
, ROW_NUMBER () OVER ( PARTITION BY B.MATNR,B.WERKS ORDER BY B.MATNR,B.WERKS) ID_ITEM_NO
, X.PROD_MATNR IDNRK_IPRODUCTION
, X.DRAWING_MATNR IDNRK_IDRAWING
FROM BOM_PARENT B
, XX_MAIN.XX_MATER_XREF X
WHERE B.BCHLD = TRIM(X.PROD_MATNR )
AND X.SAP_MTART <> 'ZPRD'
AND X.SOURCE = 'AP'
)
SELECT DISTINCT C.MATNR
, C.IPRODUCTION
, C.IDRAWING
, C.WERKS
, (C.ID_ITEM_NO*10) ID_ITEM_NO
, C.BSEQ
, C.IDNRK
, C.IDNRK_IPRODUCTION
, C.IDNRK_IDRAWING
, C.MENGE BPSCP
, STPO.MENGE STPO_MENGE
, C.MEINS MEINS
, C.IUMS IUMS
, STPO.MEINS STPO_MEINS
FROM BOM_CHILDS C
, XX_MAIN.XX_MAST MAST
, XX_MAIN.XX_STPO STPO
WHERE C.MATNR = MAST.MATNR
AND MAST.STLNR = STPO.STLNR
AND MAST.STLAN = '1'
AND MAST.WERKS = C.WERKS
AND STPO.IDNRK = C.IDNRK
UNION
SELECT DISTINCT C.MATNR
, C.IPRODUCTION
, C.IDRAWING
, C.WERKS
, (C.ID_ITEM_NO*10) ID_ITEM_NO
, C.BSEQ
, C.IDNRK
, C.IDNRK_IPRODUCTION
, C.IDNRK_IDRAWING
, C.MENGE BPSCP
, STPO.MENGE STPO_MENGE
, C.MEINS MEINS
, C.IUMS IUMS
, STPO.MEINS STPO_MEINS
FROM BOM_CHILDS C
, XX_MAIN.XX_MAST MAST
, XX_MAIN.XX_STPO STPO
WHERE C.MATNR = MAST.MATNR(+)
AND MAST.STLNR = STPO.STLNR(+)
AND MAST.STLAN(+) = '1'
AND MAST.WERKS(+) = C.WERKS
AND STPO.IDNRK IS NULL
ORDER BY MATNR,WERKS,ID_ITEM_NO,BSEQ;

This section is probably causing the problem:
FROM BOM_CHILDS C
, XX_MAIN.XX_MAST MAST
, XX_MAIN.XX_STPO STPO
WHERE C.MATNR = MAST.MATNR(+)
AND MAST.STLNR = STPO.STLNR(+)
AND MAST.STLAN(+) = '1'
AND MAST.WERKS(+) = C.WERKS
AND STPO.IDNRK IS NULL
To make this a bit easier, lets rearrange the WHERE clause to order the tables by how they relate:
FROM BOM_CHILDS C
, XX_MAIN.XX_MAST MAST
, XX_MAIN.XX_STPO STPO
-- Joining C to MAST
WHERE C.MATNR = MAST.MATNR(+)
AND C.WERKS = MAST.WERKS(+)
AND MAST.STLAN(+) = '1'
-- Joining MAST to STPO
AND MAST.STLNR = STPO.STLNR(+)
AND STPO.IDNRK IS NULL
We have C joined to MAST using C as the "driver" table and picking up data from MAST where it matches (a left join):
FROM BOM_CHILDS C
LEFT JOIN XX_MAIN.XX_MAST MAST
ON C.MANTR = MAST.MANTR
AND C.WERKS = MAST.WERKS
AND MAST.STLAN = '1'
Then we need to add STPO to the joins:
LEFT JOIN XX_MAIN.XX_STPO STPO
ON MAST.STLNR = STPO.STLNR
AND STPO.IDNRK IS NULL
Putting it all together we get:
FROM BOM_CHILDS C
LEFT JOIN XX_MAIN.XX_MAST MAST
ON C.MANTR = MAST.MANTR
AND C.WERKS = MAST.WERKS
AND MAST.STLAN = '1'
LEFT JOIN XX_MAIN.XX_STPO STPO
ON MAST.STLNR = STPO.STLNR
AND STPO.IDNRK IS NULL
That said, even though (+) works for left/right/outer joins, Oracle recommends not using it:
Oracle recommends that you use the FROM clause OUTER JOIN syntax rather than the Oracle join operator. Outer join queries that use the Oracle join operator (+) are subject to the following rules and restrictions, which do not apply to the FROM clause OUTER JOIN syntax: ...

Related

Which query is faster to execute

Following are two options to query CTE and NFSE data. Pls advise !
A similar approach may have to be used in many places.
I would like you to verify them and suggest, if there are better options.
Option 1: Query CTE with all necessary joins UNION with query on NFSe with all necessary joins
SELECT DISTINCT 'CTE' as docTypeID
, cte.isqn_mstr_cd as ctePk
, cte.ct_e_cd
, cter.stat_desc
, awb.CREATE_DT
, awb.AWB_NBR
, awb.SHPR_NM
, awbs.DEST_LOC_CD
, nfe.NT_FSCL_CD
, nfe.FSCL_DOC_NBR
FROM cte_identity_master cte
INNER JOIN cte_response_detail cter ON (cte.isqn_mstr_cd = cter.isqn_mstr_cd)
LEFT JOIN match_ref_awb mawb ON (cte.isqn_ref_cd = mawb.isqn_mstr_cd)
LEFT JOIN awb_cust_master awb ON (mawb.awb_nbr = awb.awb_nbr)
LEFT JOIN awb_shipment_detail awbs ON (awb.awb_nbr = awbs.awb_nbr)
LEFT JOIN match_ref_nfe mnfe ON (cte.isqn_ref_cd = mnfe.isqn_mstr_cd)
LEFT JOIN nfe_identity_master nfe ON (mnfe.nt_fscl_cd = nfe.nt_fscl_cd)
UNION
SELECT DISTINCT 'NFSE' as docTypeID
, nfse.isqn_mstr_cd as nfsePk
, nfse.rp_s_id
, nfser.stat_desc
, awb.CREATE_DT
, awb.AWB_NBR
, awb.SHPR_NM
, awbs.DEST_LOC_CD
, nfe.NT_FSCL_CD
, nfe.FSCL_DOC_NBR
FROM nfse_request_detail nfse
INNER JOIN nfse_response_detail nfser ON (nfse.isqn_mstr_cd = nfser.isqn_mstr_cd)
LEFT JOIN match_ref_awb mawb ON (nfse.isqn_ref_cd = mawb.isqn_mstr_cd)
LEFT JOIN awb_cust_master awb ON (mawb.awb_nbr = awb.awb_nbr)
LEFT JOIN awb_shipment_detail awbs ON (awb.awb_nbr = awbs.awb_nbr)
LEFT JOIN match_ref_nfe mnfe ON (nfse.isqn_ref_cd = mnfe.isqn_mstr_cd)
LEFT JOIN nfe_identity_master nfe ON (mnfe.nt_fscl_cd = nfe.nt_fscl_cd)
;
Option 2: Use Union of CTe and NFSe first and then apply joins with other tables
SELECT ctnf.*
, awb.CREATE_DT
, awb.AWB_NBR
, awb.SHPR_NM
, awbs.DEST_LOC_CD
, nfe.NT_FSCL_CD
, nfe.FSCL_DOC_NBR
FROM (
SELECT DISTINCT 'CTE' as docTypeID
, cte.isqn_mstr_cd as docPk
, cte.ct_e_cd as docNbr
, cter.stat_desc as docStat
, cte.isqn_ref_cd as matchRef
FROM cte_identity_master cte
INNER JOIN cte_response_detail cter ON (cte.isqn_mstr_cd = cter.isqn_mstr_cd)
UNION
SELECT DISTINCT 'NFSE' as docTypeID
, nfse.isqn_mstr_cd as nfsePk
, nfse.rp_s_id
, nfser.stat_desc
, nfse.isqn_ref_cd
FROM nfse_request_detail nfse
INNER JOIN nfse_response_detail nfser ON (nfse.isqn_mstr_cd = nfser.isqn_mstr_cd)
) ctnf
LEFT JOIN match_ref_awb mawb ON (ctnf.matchRef = mawb.isqn_mstr_cd)
LEFT JOIN awb_cust_master awb ON (mawb.awb_nbr = awb.awb_nbr)
LEFT JOIN awb_shipment_detail awbs ON (awb.awb_nbr = awbs.awb_nbr)
LEFT JOIN match_ref_nfe mnfe ON (ctnf.matchRef = mnfe.isqn_mstr_cd)
LEFT JOIN nfe_identity_master nfe ON (mnfe.nt_fscl_cd = nfe.nt_fscl_cd)
;
Both approaches of the above will have following Where Clause:
WHERE lower(cte.sttn_cd) = lower(:stationId)
and (:documentType is null or lower(:documentType) = 'cte')
and (:shipperName is null or lower(awb.shipperNm) like lower(concat(concat('%',:shipperName),'%')))
and (:awbCreated is null or to_char(awb.createDt, 'MM-DD-YYYY') = :awbCreated)
and (:awbNumber is null or m2.awbNbr like concat(concat('%',:awbNumber),'%'))
and (:serviceType = 0 or awbs.baseServiceCd = :serviceType)
and (:commitmentDate is null or awbs.commitmentDate = :commitmentDate)
and (:ursa is null or lower(awbs.ursaCd) like lower(concat(concat('%',:ursa),'%')))
and (:destLocationId is null or lower(awbs.destLocCd) like lower(concat(concat('%',:destLocationId),'%')))
and (:nfeNumber is null or nfe.fiscalDocumentNumber like concat(concat('%',:nfeNumber),'%'))
PLEASE SUGGEST - Output of these two approaches to get data which one will be better to fix at Java End to retrieve the data.
Any help will be greatly appreciated. Also suggest if there is any other better query apart from these two!

Column Ambiguously Defined - No Line / Column Number

I've been trying to create a query that joins two sets of tables together using several columns. I have tried assigning aliases to each column but I am still receiving a (ORA-00918: column ambiguously defined) error with no line number / direction on which column oracle has an issue with. Can someone please help me? Been stuck on this issue for hours now. My code is below, thanks in advance!
SELECT *
FROM
(select v.value_id as i_value_id
, v.value_nb as i_value_nb
, v.utc_offset as i_utc_offset
, v.data_Date as i_data_date
, v.hr_utc as i_hr_utc
, v.utc_offset as i_utc_offset
, v.hr as i_hr
, v.hr_num as i_hr_num
, v.data_Code as i_data_code
, v.Code as i_code
, ff.form_Field_tx as i_form_field_tx
, sv.submission_value_id as i_submission_value_id
, s.submission_name_tx as i_submission_name_tx
, s.submission_id as i_submission_id
, fl.form_line_tx as i_form_line_tx
, fs.form_section_tx as i_form_section_tx
, sf.form_name_tx as i_form_name_tx
, sf.form_label_tx as i_form_label_tx
, sf.form_number_tx as i_form_number_tx
, sf.survey_id as i_survey_id
from value v
join sub_value sv on v.value_id = sv.value_id
join form_Field ff on sv.form_Field_id = ff.form_Field_id
join sub s on sv.submission_id = s.submission_id
LEFT OUTER JOIN form_line fl ON ff.form_line_id = fl.form_line_id
LEFT OUTER JOIN form_section fs ON fl.form_section_id = fs.form_section_id
LEFT OUTER JOIN survey_form sf ON fs.survey_form_id = sf.survey_form_id) subq1
JOIN
(select va.value_id as o_value_Id
, va.value_nb as o_value_nb
, va.utc_offset as o_utc_offset
, va.data_Date as o_data_date
, va.hr_utc as o_hr_utc
, va.hr as o_hr
, va.hr_num as o_hr_num
, va.data_Code as o_data_Code
, va.balancing_Authority_Code as o_balancing_authority_code
, ff2.form_Field_tx as o_form_field_tx
, sva.submission_value_id as o_submission_value_id
, s.submission_id as o_submission_id
, fl.form_line_tx as o_form_line_tx
, fs.form_section_tx as o_form_section_tx
, sf.form_name_tx as o_form_name_tx
, sf.form_label_tx as o_form_label_tx
, sf.form_number_tx as o_form_number_tx
, sf.survey_id as o_survey_id
from submission s
join submission_value_audit sva on s.submission_id = sva.submission_id
join value_audit va on sva.value_id = va.value_id
join form_Field ff2 on sva.form_Field_id = ff2.form_Field_id
LEFT OUTER JOIN form_line fl ON ff2.form_line_id = fl.form_line_id
LEFT OUTER JOIN form_section fs ON fl.form_section_id = fs.form_section_id
LEFT OUTER JOIN survey_form sf ON fs.survey_form_id = sf.survey_form_id) subq2
on subq1.sub_id = subq2.sub_id
on subq1.code = subq2.code;
looks like you have a duplicate definition of i_utc_offset (see code snippet below)
(select v.value_id as i_value_id
, v.value_nb as i_value_nb
, v.utc_offset as i_utc_offset
, v.data_Date as i_data_date
, v.hr_utc as i_hr_utc
, v.utc_offset as i_utc_offset
ORA-00918: column ambiguously defined: You tried to execute a SQL statement that joined two or more tables, where a column with the same name exists in both tables.
Reference: https://www.techonthenet.com/oracle/errors/ora00918.php
The duplicate column name is below.
SELECT *
FROM
(
select ...
, v.utc_offset as i_utc_offset
...
, v.utc_offset as i_utc_offset

SQL select with not required where statement

I want to select some data from different tables with some primary and foreign key links.
The AppointmentID is not necessary but when there is no link betweeen RC.CMO and RC.Appointments_RC.CMO, the select give me a null result, cause the "where" is not satisfied.
How can I select all data, also when there is no appointment for the cmo?
I tink that is the problem:
AND
[RC.CMO].cmo_id = [RC.Appointments_RC.CMO].cmo_id
AND
[RC.Appointments_RC.CMO].appointment_id = [RC.Appointments].appointment_id
I cant find some data if the person is not linked to an appointment.
Statement:
SELECT
[RC.CMO].ci_nummer
, [RC.CMO].raum
, [RC.CMO].anzahl_monitore as cmo_mon
, [RC.CMO].gebäude as gebaeude
, [RC.CMO].bemerkung
, [RC.Persons].kostenstelle
, [RC.Persons].vorname
, [RC.Persons].nachname
, [RC.FMO].hardware_typ
, [RC.FMO].anzahl_monitore as fmo_mon
, [RC.FMO].zubehör as zubehoer
, [RC.Appointments].appointment_id
FROM [RC.CMO]
, [RC.Persons]
, [RC.Persons_RC.CMO]
, [RC.FMO]
, [RC.Persons_RC.FMO]
, [RC.Appointments]
, [RC.Appointments_RC.CMO]
WHERE [RC.CMO].cmo_id = #cmo_id
AND [RC.CMO].cmo_id = [RC.Persons_RC.CMO].cmo_id
AND [RC.Persons_RC.CMO].person_id = [RC.Persons].person_id
AND [RC.Persons].person_id = [RC.Persons_RC.FMO].person_id
AND [RC.Persons_RC.FMO].fmo_id = [RC.FMO].fmo_id
AND [RC.CMO].cmo_id = [RC.Appointments_RC.CMO].cmo_id
AND [RC.Appointments_RC.CMO].appointment_id = [RC.Appointments].appointment_id
Is it possible to make an exists OR not exists?
If you convert your query to use the modern explicit JOIN syntax instead of the old-fashioned implict one, you can have OUTER joins, which do exactly what you want:
SELECT
[RC.CMO].ci_nummer
, [RC.CMO].raum
, [RC.CMO].anzahl_monitore as cmo_mon
, [RC.CMO].gebäude as gebaeude
, [RC.CMO].bemerkung
, [RC.Persons].kostenstelle
, [RC.Persons].vorname
, [RC.Persons].nachname
, [RC.FMO].hardware_typ
, [RC.FMO].anzahl_monitore as fmo_mon
, [RC.FMO].zubehör as zubehoer
, [RC.Appointments].appointment_id
FROM [RC.CMO]
JOIN [RC.Persons] ON [RC.CMO].cmo_id = [RC.Persons_RC.CMO].cmo_id
JOIN [RC.Persons_RC.CMO] ON [RC.Persons_RC.CMO].person_id = [RC.Persons].person_id
JOIN [RC.FMO] ON [RC.Persons].person_id = [RC.Persons_RC.FMO].person_id
JOIN [RC.Persons_RC.FMO] ON [RC.Persons_RC.FMO].fmo_id = [RC.FMO].fmo_id
LEFT OUTER JOIN [RC.Appointments] ON [RC.CMO].cmo_id = [RC.Appointments_RC.CMO].cmo_id
JOIN [RC.Appointments_RC.CMO] ON [RC.Appointments_RC.CMO].appointment_id = [RC.Appointments].appointment_id
WHERE [RC.CMO].cmo_id = #cmo_id
try this. i removed duplicate tables and optimised the joins:
SELECT [RC.CMO].ci_nummer
, [RC.CMO].raum
, [RC.CMO].anzahl_monitore as cmo_mon
, [RC.CMO].gebäude as gebaeude
, [RC.CMO].bemerkung
, [RC.Persons].kostenstelle
, [RC.Persons].vorname
, [RC.Persons].nachname
, [RC.FMO].hardware_typ
, [RC.FMO].anzahl_monitore as fmo_mon
, [RC.FMO].zubehör as zubehoer
, [RC.Appointments].appointment_id
FROM [RC.CMO]
inner join
[RC.Persons_RC.CMO]
on [RC.Persons_RC.CMO].cmo_id = [RC.CMO].cmo_id
inner join
[RC.Persons]
on [RC.Persons_RC.CMO].person_id = [RC.Persons].person_id
inner join
[RC.FMO]
on [RC.Persons_RC.FMO].fmo_id = [RC.FMO].fmo_id
left join
[RC.Appointments_RC.CMO]
on [RC.CMO].cmo_id = [RC.Appointments_RC.CMO].cmo_id
left join
[RC.Appointments]
on [RC.Appointments_RC.CMO].appointment_id = [RC.Appointments].appointment_id
WHERE [RC.CMO].cmo_id = #cmo_id

SQL Server view with a 'select where x is not null' takes ages to complete

I have a complex view, which is described here: View of multiple tables. Need to remove "doubles" defined by 1 table
I used a Cross Apply in it, and the code is this: (please do check the url above to understand the view)
SELECT dbo.InstellingGegevens.INST_SUBTYPE
, dbo.InstellingGegevens.INST_BRON
, dbo.InstellingGegevens.INST_INSTELLINGSNUMMER
, dbo.InstellingGegevens.INST_NAAM
, dbo.InstellingGegevens.INST_KORTENAAM
, dbo.InstellingGegevens.INST_VESTIGINGSNAAM
, dbo.InstellingGegevens.INST_ROEPNAAM
, dbo.InstellingGegevens.INST_STATUUT
, dbo.InstellingGegevens.ONDERWIJSNIVEAU_REF
, dbo.InstellingGegevens.ONDERWIJSSOORT_REF
, dbo.InstellingGegevens.DATUM_TOT
, dbo.InstellingGegevens.DATUM_VAN
, dbo.InstellingGegevens.VERBOND_REF
, dbo.InstellingGegevens.VSKO_LID
, dbo.InstellingGegevens.NET_REF
, dbo.Instellingen.Inst_ID
, dbo.Instellingen.INST_TYPE
, dbo.Instellingen.INST_REF
, dbo.Instellingen.INST_LOC_REF
, dbo.Instellingen.INST_LOCNR
, dbo.Instellingen.Opt_KalStandaard
, dbo.InstellingTelecom.INST_TEL
, dbo.InstellingTelecom.INST_FAX
, dbo.InstellingTelecom.INST_EMAIL
, dbo.InstellingTelecom.INST_WEB
, dbo.InstellingAdressen.SOORT
, dbo.InstellingAdressen.STRAAT
, dbo.InstellingAdressen.POSTCODE
, dbo.InstellingAdressen.GEMEENTE
, dbo.InstellingAdressen.GEM_REF
, dbo.InstellingAdressen.FUSIEGEM_REF
, dbo.InstellingAdressen.FUSIEGEM
, dbo.InstellingAdressen.ALFA_G
, dbo.InstellingAdressen.PROVINCIE
, dbo.InstellingAdressen.BISDOM
, dbo.InstellingAdressen.ARRONDISSEMENT
, dbo.InstellingAdressen.GEWEST
, dbo.InstellingContPersDirecteurs.AANSPREKING
, dbo.InstellingContPersDirecteurs.CONTACTPERSOON
, dbo.InstellingContPersDirecteurs.FUNCTIE
, InstellingLogin.Inst_Gebruikersnaam
, InstellingLogin.Inst_Concode
, InstellingLogin.Inst_DirCode
, InstellingLogin.DOSSNR
, InstellingLogin.Instelling_ID
FROM dbo.InstellingGegevens
RIGHT OUTER JOIN dbo.Instellingen
ON dbo.InstellingGegevens.INST_TYPE = dbo.Instellingen.INST_TYPE
AND dbo.InstellingGegevens.INST_REF = dbo.Instellingen.INST_REF
AND dbo.InstellingGegevens.INST_LOC_REF = dbo.Instellingen.INST_LOC_REF
AND dbo.InstellingGegevens.INST_LOCNR = dbo.Instellingen.INST_LOCNR
LEFT OUTER JOIN dbo.InstellingTelecom
ON dbo.InstellingGegevens.INST_TYPE = dbo.InstellingTelecom.INST_TYPE
AND dbo.InstellingGegevens.INST_REF = dbo.InstellingTelecom.INST_REF
AND dbo.InstellingGegevens.INST_LOC_REF = dbo.InstellingTelecom.INST_LOC_REF
LEFT OUTER JOIN dbo.InstellingAdressen
ON dbo.InstellingGegevens.INST_TYPE = dbo.InstellingAdressen.INST_TYPE
AND dbo.InstellingGegevens.INST_REF = dbo.InstellingAdressen.INST_REF
AND dbo.InstellingGegevens.INST_LOC_REF = dbo.InstellingAdressen.INST_LOC_REF
LEFT OUTER JOIN dbo.InstellingContPersDirecteurs
ON dbo.InstellingGegevens.INST_TYPE = dbo.InstellingContPersDirecteurs.INST_TYPE
AND dbo.InstellingGegevens.INST_REF = dbo.InstellingContPersDirecteurs.INST_REF
AND dbo.InstellingGegevens.INST_LOC_REF = dbo.InstellingContPersDirecteurs.INST_LOC_REF
CROSS APPLY
(SELECT TOP (1) *
FROM InstellingLogin AS il
WHERE Instellingen.INST_LOC_REF = il.Inst_Loc_REF
AND Instellingen.INST_LOCNR = il.Inst_Loc_Nr
AND Instellingen.INST_REF = il.Inst_InstellingIKON_REF
AND Instellingen.INST_TYPE = il.Inst_InstellingIKONType
ORDER BY CASE
WHEN il.datum_tot IS NULL
THEN 0 ELSE 1
END
, il.datum_tot DESC) InstellingLogin
This view returns me about 5.5k rows, in about 1s. This is fast!
However!
When I call this view with a where clause:
SELECT *
FROM [Tink].[dbo].[InstellingAlleDetails]
where gemeente is not null and (DATUM_TOT is null or DATUM_TOT > GETDATE())
order by GEMEENTE, POSTCODE,STRAAT, INST_NAAM
it takes 1min 20s to return all rows.
When I drop the gemeente is not null part, it takes again 1s.
Gemeente is a varchar(255). I also tried it with Inst_Naam is not null and that also took about 1min 30s.
Why does this is not null take so much time? And more importantly: how do I fix this?
I don't know why. Probably SQL Server comes up with a query plan that is not so good.
You could try to first run the query without gemeente is not null and put the result in a temp table and then query the temp table with gemeente is not null.
select *
into #TempTable
from YourView
select *
from #TempTable
where gemeente is not null
drop table #TempTable
First check the execution plans on both the query with and without that is not null and see the differences.
BTW are any of these joins to other views? That can cause tremendous performance problems.

DB2 issue Getting -101 for 300 records but same query working for 1000 and 10000 records

I am facing strange issue in Db2 .
I am getting DB2 SQL error: SQLCODE: -101, SQLSTATE: 54001, SQLERRMC: 1
The same query works for 1000 even 10000 records but does not works for particualar sets of records(150-300).I am able to reproduce now with those records.
Query uses Select and With cause only.
So I donot think increasing STMT Heap Memory will make any sense as the query works for larger records.
Query is Big and Consists of only Select Join and few Case statement.
Query is also using With statement.
#Note Every Record is independent in its own have relation with another record.
Here is Query
WITH
itemStyle_ AS (
SELECT * FROM item.itemStyle WHERE itemSTY_SEQ
---- This Parameter gets changes to 1000 only
IN( 'awudhjqwdvwd12' )
),CustomsInfo AS
(SELECT 3 AS ReitemrtGrp, 3 AS REitemRTGRP2, HSYS_NO, NHSYS_NO, itemCUSTOMSINFO.itemDEL_SEQ,-1 AS COL_NO,
itemStyle.MyApp_item_NO, itemStyle.SIZE_TNAME, itemStyle.CS_DATE, itemStyle.SIZ1TABID, itemDELIVERY.DS_NO,
itemStyle.ABM_Date, itemStyle.RS_Date,itemStyle.CustCo_NO, itemStyle.Cust_Code, itemStyle.UCustCo_No, itemStyle.Prodiv_desc, itemStyle.Busunidesc,itemStyle.COno, itemStyle.STY_NO
FROM item.itemCUSTOMSINFO itemCUSTOMSINFO INNER JOIN item.itemDELIVERY itemDELIVERY ON itemCUSTOMSINFO.itemDEL_SEQ = itemDELIVERY.itemDEL_SEQ, itemStyle_ itemStyle
WHERE (HSYS_NO IS NOT NULL OR NHSYS_NO IS NOT NULL)
AND itemCUSTOMSINFO.itemSTY_SEQ = itemStyle.itemSTY_SEQ
),
SizeInfo AS(
SELECT 1 AS ReitemrtGrp2,
itemStyle.SIZ1TABID, itemDELCOLOR.itemDEL_SEQ, itemStyle.MyApp_item_NO, itemStyle.CS_DATE, itemStyle.STY_NO,
itemStyle.ABM_Date, itemStyle.RS_Date,itemStyle.CustCo_NO, itemStyle.Cust_Code, itemStyle.UCustCo_No, itemStyle.Prodiv_desc, itemStyle.Busunidesc,itemStyle.COno,itemDELCOLOR.DS_NO,
itemColor.COL_NO,CUSIZ_CODE,
itemDELSIZE.QUANTITY,itemDELSIZE.OCP_FACTOR,itemDELSIZE.SKU_CODE,
itemSIZE.SIZE_CODE, itemSIZE.UNIT_PRC,itemSIZE.SURCHARGE,itemSIZE.SIZNUM itemSIZE_NUM, itemSIZE.UNIT_DISC, itemSIZE.PACK_UNIT
FROM
itemStyle_ AS itemStyle
INNER JOIN item.itemColor itemColor ON itemStyle.itemSTY_SEQ = itemColor.itemSTY_SEQ
INNER JOIN item.itemDELCOLOR itemDELCOLOR ON itemDELCOLOR.itemCOL_SEQ = itemColor.itemCOL_SEQ
INNER JOIN item.itemDELSIZE itemDELSIZE ON itemDELCOLOR.itemDELCOL_SEQ = itemDELSIZE.itemDELCOL_SEQ
INNER JOIN item.itemSIZE itemSIZE ON itemDELSIZE.itemSIZ_SEQ = itemSIZE.itemSIZ_SEQ
),OrderChar AS(
SELECT 3 AS ReitemrtGrp2, 2.6 AS ReitemrtGrp,
ordchar_desc, itemStyle.COno, CS_Date, SIZ1TABID, itemStyle.MyApp_item_NO, itemStyle.STY_NO,
itemStyle.ABM_Date, itemStyle.RS_Date,itemStyle.CustCo_NO, itemStyle.Cust_Code, itemStyle.UCustCo_No, itemStyle.Prodiv_desc, itemStyle.Busunidesc,itemDELIVERY.itemDEL_SEQ FROM
item.itemORDERCHAR itemOrderChar INNER JOIN itemStyle_ itemStyle ON itemOrderChar.itemSty_Seq = itemStyle.itemSty_Seq
INNER JOIN item.itemDELIVERY itemDELIVERY ON itemStyle.itemSTY_SEQ = itemDELIVERY.itemSTY_SEQ
),
Supp_Address AS (
SELECT ADDRESS.COMPANY1 AS SuppAdd_COMPANY1,
ADDRESS.COMPANY2 AS SuppAdd_COMPANY2,
ADDRESS.COMPANY3 AS SuppAdd_COMPANY3,
ADDRESS.STREET1 AS SuppAdd_STREET1,
ADDRESS.STREET2 AS SuppAdd_STREET2,
ADDRESS.STREET3 AS SuppAdd_STREET3,
ADDRESS.CITY AS SuppAdd_CITY,
ADDRESS.STATENAME AS SuppAdd_STATENAME,
ADDRESS.COUNTRY AS SuppAdd_COUNTRY,
ADDRESS.ZIP_CODE AS SuppAdd_ZIP_CODE,
SUPPLIER.SUP1COMID,
CONTACTINF.C_PHONENO, CONTACTINF.A_PHONENO, CONTACTINF.M_PHONE ,
ADDRESS.ZitemST_BOX , ADDRESS.itemST_BOX
FROM item.ADDRESS ADDRESS,
item.ORGANZN ORGANZN
LEFT OUTER JOIN item.SUPPLIER SUPPLIER
ON ORGANZN.ORG_ID=SUPPLIER.ORG_ID,
item.CONTACTINF CONTACTINF WHERE
( ADDRESS.ORG_ID=ORGANZN.ORG_ID ) AND
( CONTACTINF.ADDRESS_ID=ADDRESS.ADDRESS_ID ) AND
(
SUPPLIER.SUPP_FLAG = 'Y'
)) ,
Cust_Address AS (
SELECT VALUE(ADDRESS.COMPANY1,'') CustAdd_COMPANY1 ,
VALUE(ADDRESS.COMPANY2,'') AS CustAdd_COMPANY2,
VALUE(ADDRESS.COMPANY3,'') AS CustAdd_COMPANY3,
VALUE(ADDRESS.STREET1,'') AS CustAdd_STREET1,
VALUE(ADDRESS.STREET2,'') AS CustAdd_STREET2,
VALUE(ADDRESS.STREET3 ,'') AS CustAdd_STREET3,
VALUE(ADDRESS.CITY,'') AS CustAdd_CITY,
VALUE(ADDRESS.STATENAME,'') AS CustAdd_STATENAME,
VALUE(ADDRESS.COUNTRY ,'') AS CustAdd_COUNTRY,
VALUE(ADDRESS.ZIP_CODE ,'')AS CustAdd_ZIPCODE,
VALUE(CUSTOMER.CUST_ID ,'')AS CustAdd_CUST_ID,
VALUE(ADDRESS.ZitemST_BOX,'') AS CustAdd_ZitemST_BOX ,
VALUE(ADDRESS.itemST_BOX,'') AS CustAdd_itemST_BOX
FROM item.ADDRESS ADDRESS,
item.CUSTOMER CUSTOMER
LEFT OUTER JOIN item.ORGANZN ORGANZN
ON ORGANZN.ORG_ID=CUSTOMER.ORG_ID,
item.ORGDEFAULTS ORGDEFAULTSWHERE
( ADDRESS.ORG_ID=ORGANZN.ORG_ID )
AND (
ADDRESS.ADDRESS_ID=ORGDEFAULTS.DEFAULT_VALUE_ID ) AND
( CUSTOMER.CUST_FLAG='Y' )
AND ORGDEFAULTS.DEFAULT_ITEM = 'MAILING_ADDRESS'
SELECT VALUE(CustomsInfo.ReitemrtGrp, OrderChar.ReitemrtGrp, 3) AS ReitemrtGrp,
VALUE(SizeInfo.ReitemrtGrp2, OrderChar.ReitemrtGrp2, CustomsInfo.ReitemrtGrp2, 4) AS ReitemrtGrp2,
VALUE(itemStyle.MyApp_item_NO, CUSTOMSINFO.MyApp_item_NO, SizeInfo.MyApp_item_NO,OrderChar .MyApp_item_NO) AS MyApp_item_NO ,
itemStyle.COno ,
itemStyle.STAT_DESC ,
itemStyle.CUST_CODE ,
itemStyle.CUST_NAME ,
itemStyle.CUSTCO_NO ,
itemStyle.UCUST_CODE ,
itemStyle.UCUST_NAME ,
itemStyle.DSHIP_FLAG ,
itemStyle.STY_NO ,
itemStyle.STY_DESC ,
itemStyle.BRAND_DESC ,
itemStyle.PRODIV_DESC ,
itemStyle.CUSTY_DESC ,
itemStyle.SEA_DESC ,
itemStyle.LINNAMDESC ,
itemStyle.SUPP_CODE ,
itemStyle.SUPP_NAME,
itemStyle.CONCEPDESC ,
itemStyle.RS_DATE ,
itemStyle.ABM_DATE ,
VALUE(itemStyle.CS_DATE, CustomsInfo.CS_DATE, SizeInfo.CS_DATE, OrderChar.CS_Date) AS CS_DATE ,
itemStyle.REMARKS ,
itemStyle.LASTUPDATE ,
itemStyle.FACT_CODE ,
itemStyle.C_ORIGIN ,
itemStyle.itemDDESC,
VALUE(itemDELIVERY.DS_NO, SizeInfo.DS_NO,CustomsInfo.DS_NO) AS DS_NO ,
itemDELIVERY.CUST_DS_NO ,
itemDELIVERY.ULTCUST_DS_NO,
itemDELIVERY.LCS_DATE ,
itemDELIVERY.DTERM_DESC ,
itemSHIPINST.SHIP_MARKS ,
itemDELIVERY.PACK_INST,
itemStyle.KEY_SIZE ,
itemSHIPINST.SMODE_DESC ,
itemSHIPINST.ORIGN_itemRT ,
itemSHIPINST.DEST_itemRT ,
itemSHIPINST.CARR_DESC ,
itemSHIPINST.SPL_INST,
itemStyle.PAYM_DESC ,
itemStyle.PAYT_DESC ,
itemStyle.UCUSTCO_NO ,
itemStyle.ORD_RDATE ,
itemStyle.ORG_NAME ,
itemStyle.CUSTY_NO ,
VALUE(itemDELADDRESS.COMPANY1,'') AS DELCOMPANY1 ,
VALUE(itemDELADDRESS.COMPANY2,'') AS DELCOMPANY2 ,
VALUE(itemDELADDRESS.COMPANY3,'') AS DELCOMPANY3 ,
VALUE(itemDELADDRESS.STREET1,'') AS DELSTREET1,
VALUE(itemDELADDRESS.STREET2,'') AS DELSTREET2,
VALUE(itemDELADDRESS.STREET3,'') AS DELSTREET3,
VALUE(itemDELADDRESS.CITY,'') AS DELCITY,
VALUE(itemDELADDRESS.STATENAME,'') AS DELSTATENAME,
VALUE(itemDELADDRESS.COUNTRY,'') AS DELCOUNTRY,
VALUE(itemDELADDRESS.ZIP_CODE,'') AS DELZIP ,
VALUE(itemDELIVERY.itemDEL_SEQ , CustomsInfo.itemDEL_SEQ, SizeInfo.itemDEL_SEQ, OrderChar.itemDEL_SEQ) AS itemDEL_SEQ ,
itemStyle.DEBTOR_CODE ,
VALUE(itemADDRESS.STATENAME,'') AS STATENAME,
VALUE(itemADDRESS.COMPANY1,'') AS COMPANY1,
VALUE(itemADDRESS.COMPANY2,'') AS COMPANY2,
VALUE(itemADDRESS.COMPANY3,'') AS COMPANY3,
VALUE(itemADDRESS.STREET1,'') AS STREET1,
VALUE(itemADDRESS.STREET2,'') AS STREET2,
VALUE(itemADDRESS.STREET3,'') AS STREET3,
VALUE(itemADDRESS.CITY,'') AS CITY,
VALUE(itemADDRESS.COUNTRY,'') AS itemADDRCOUNTRY,
VALUE(itemADDRESS.ZIP_CODE,'') AS ZIP_CODE,
itemStyle.CURRENCY ,
itemStyle.CREATEDATE ,
itemStyle.SIZE_TNAME ,
itemStyle.BUSUNIDESC ,
VALUE (itemStyle.SIZ1TABID, CustomsInfo.SIZ1TABID, SizeInfo.SIZ1TABID, OrderChar.SIZ1TABID) AS SIZ1TABID,
itemStyle.DISC_VALUE ,
HSYS_NO, NHSYS_NO ,
VALUE(itemColor.COL_NO, SizeInfo.COL_NO, CustomsInfo.COL_NO) AS COL_NO,
itemColor.PROCH_DESC ,
itemColor.PROREQDESC ,
itemColor.COL_DESC ,
VALUE (itemColor.HAND_CHARGE,0) HAND_CHARGE ,
VALUE(itemColor.OTHER_CHARGES, 0) OTHER_CHARGES ,
itemColor.PERF_CODE,
SizeInfo.QUANTITY ,
SizeInfo.OCP_FACTOR ,
SizeInfo.SKU_CODE ,
SizeInfo.SIZE_CODE ,
SizeInfo.UNIT_PRC ,
SizeInfo.SURCHARGE ,
SizeInfo.itemSIZE_NUM ,
SizeInfo.UNIT_DISC,
SizeInfo.CUSIZ_CODE,
SizeInfo.PACK_UNIT,
YOUTSOLE.ITMDES AS OUTSOLEDESC, YOUTSOLE.OUTSOLE AS OUTSOLECODE,
/* (SELECT ITMDES FROM item.YCATEGO YCATEGO WHERE YCATEGO.categoid = art1sty.categoid) AS CategoDesc,
(SELECT ITMDES FROM item.YACTGRP YACTGRP WHERE YACTGRP.actgrpid = art1sty.actgrpid) AS ACTGRPDesc,*/
LEADTM_DESC AS LeadTime,
/* (SELECT SUM(VALUE(hand_charge,0) + VALUE(other_charges,0)) AS TotalCharge FROM item.itemColor itemColor WHERE itemColor.itemSTY_SEQ = itemStyle.itemSTY_SEQ) AS TotalCharge,*/
itemDelivery.AMD_Date,
OrderChar.ordchar_desc,
YPROCON.ITMDES AS PromotionContract,
(SELECT SUM(QUANTITY) FROM SizeInfo WHERE SizeInfo.MyApp_item_NO = itemStyle.MyApp_item_NO) AS OrderTotalQty,
Supp_Address.*,
Cust_Address.*,
CASE WHEN 'MyAppitemNo' = 'csDate' THEN VALUE(itemStyle.CS_Date, CustomsInfo.CS_Date, SizeInfo.CS_Date, OrderChar.CS_Date)
WHEN 'MyAppitemNo' = 'rsDate' THEN VALUE(itemStyle.RS_Date, CustomsInfo.RS_Date,SizeInfo.RS_Date, OrderChar.RS_Date)
WHEN 'MyAppitemNo' = 'lcsDate' THEN VALUE(itemStyle.CS_Date, CustomsInfo.CS_Date, SizeInfo.CS_Date, OrderChar.CS_Date)
ELSE '2010-01-01'
END AS SortDate,
CASE WHEN 'MyAppitemNo' = 'MyAppitemNo' THEN VALUE(itemStyle.MyApp_item_NO, CustomsInfo.MyApp_item_NO, SizeInfo.MyApp_item_NO, OrderChar.MyApp_item_NO)
WHEN 'MyAppitemNo' = 'MyAppCoNo' THEN VALUE(itemStyle.COno, CustomsInfo.COno, SizeInfo.COno, OrderChar.COno)
WHEN 'MyAppitemNo' = 'custCoNo' THEN VALUE(itemStyle.CustCO_NO, CustomsInfo.CustCO_NO, SizeInfo.CustCO_NO, OrderChar.CustCO_NO)
WHEN 'MyAppitemNo' = 'custCode' THEN VALUE(itemStyle.Cust_Code, CustomsInfo.Cust_Code,SizeInfo.Cust_Code, OrderChar.Cust_Code)
WHEN 'MyAppitemNo' = 'ucustCoNo' THEN VALUE(itemStyle.UCustCO_NO, CustomsInfo.UCustCO_NO, SizeInfo.UCustCO_NO, OrderChar.UCustCO_NO)
WHEN 'MyAppitemNo' = 'proDivDesc' THEN VALUE(itemStyle.Prodiv_Desc, CustomsInfo.Prodiv_Desc, SizeInfo.Prodiv_Desc, OrderChar.Prodiv_Desc)
WHEN 'MyAppitemNo' = 'busUniDesc' THEN VALUE(itemStyle.BUSUNIDESC, CustomsInfo.BusuniDesc, SizeInfo.BusuniDesc, OrderChar.BusuniDesc)
WHEN 'MyAppitemNo' = 'styNo' THEN VALUE(itemStyle.STY_NO, CustomsInfo.STY_NO, SizeInfo.STY_NO, OrderChar.STY_NO)
ELSE 'xxx'
END AS SortField
FROM itemStyle_ AS itemStyle
INNER JOIN item.itemDELIVERY itemDELIVERY ON itemStyle.itemSTY_SEQ = itemDELIVERY.itemSTY_SEQ
INNER JOIN item.itemDELCOLOR itemDELCOLOR ON itemDELCOLOR.itemDEL_SEQ = itemDELIVERY.itemDEL_SEQ
INNER JOIN item.itemColor itemColor ON itemDELCOLOR.itemCOL_SEQ = itemColor.itemCOL_SEQ
INNER JOIN item.itemSHIPINST itemSHIPINST ON itemSHIPINST.itemDEL_SEQ = itemDELIVERY.itemDEL_SEQ
INNER JOIN item.itemDELADDRESS itemDELADDRESS ON itemDELADDRESS.itemDEL_SEQ = itemDELIVERY.itemDEL_SEQ
INNER JOIN item.itemADDRESS itemADDRESS ON itemADDRESS.itemSTY_SEQ = itemStyle.itemSTY_SEQ
INNER JOIN item.ART1STY ART1STY ON ART1STY.ART1STYID = itemStyle.ART1STYID
INNER JOIN Supp_Address ON itemStyle.SUPP_ID = Supp_Address.SUP1COMID
INNER JOIN Cust_Address ON itemStyle.CUST_ID = Cust_Address.CustAdd_Cust_ID
LEFT OUTER JOIN item.ART4COS ART4COS ON itemColor.ART4COSID = ART4COS.ART4COSID
LEFT OUTER JOIN item.ART2STS ART2STS ON ART2STS.ART2STSID = ART4COS.ART2STSID
LEFT JOIN item.YPROCON YPROCON ON ART2STS.PROCONID = YPROCON.PROCONID
LEFT OUTER JOIN item.YOUTSOLE YOUTSOLE ON YOUTSOLE.OUTSOLEID = ART1STY.OUTSOLEID
FULL OUTER JOIN CustomsInfo ON 1=2
FULL OUTER JOIN SizeInfo ON 1=2
FULL OUTER JOIN OrderChar ON 1=2
ORDER BY
SortDate DESC, SortField DESC,
MyApp_item_NO ,
VALUE(DS_NO,0),
VALUE(COL_NO,0),
itemDEL_SEQ,
ReitemrtGrp,
ReitemrtGrp2 DESC, SizeInfo.itemSIZE_NUM
FOR READ ONLY
Update:
My DB is having no triggers and procedures
The Strange part is for those set of (300) records query fails but works fine for 1000 rows/records/input parameters. Any Idea.
It is because of the query statement exceed the default size of 4KB. You can set the statement heap size as the following.
db2 update db cfg for YOUR_DATABASE_NAME using STMTHEAP 8192 AUTOMATIC