MS SQL Case nesting error but not nested - sql

I got a problem on MS SQL :
I'm using a CASE with many statements in my view, like this :
select
e.[Nom d'annuaire],
e.[Prénom annuaire],
f.[Nom d'annuaire en UTF-8] AS login,
e.Usagers as [Numéro interne],
d.[Numéro externe],
s.[Préfixe No abr direct],
v.fonction,
CASE
WHEN v.lib_delegation = 'Service informatique' THEN 'Euralille'
WHEN v.delegation = 'test_national' THEN 'Maillot'
WHEN v.delegation = 'test_tpe' THEN 'Maillot'
WHEN v.delegation = 'test_paris-idf' THEN 'Maillot'
WHEN v.delegation = 'test_grands-comptes' THEN 'Maillot'
WHEN v.delegation = 'cotestrm' THEN 'Marcq en baroeul'
WHEN v.delegation = 'test_sud-ouest' THEN 'Bruges'
WHEN v.delegation = 'test_normandie' THEN 'Caen'
WHEN v.delegation = 'test_bourgogne' THEN 'Dijon'
WHEN v.delegation = 'test_ouest' THEN 'Ouest'
WHEN v.lib_delegation = 'Centre Limousin' THEN 'Tours'
WHEN v.delegation = 'test_est' THEN 'Nancy'
WHEN v.delegation = 'test_mediterranee' THEN 'Aix'
WHEN v.delegation = 'test_rhone-alpes' THEN 'Lyon'
ELSE v.delegation
END AS Délégation,
v.lib_delegation
from ale e
INNER JOIN ale f
on e.Usagers = f.Usagers AND f.[No alias] = 1 and e.[No Alias] = 0
INNER JOIN Delegation d
on f.Usagers = d.[No annuaire]
INNER JOIN V_users v
on v.LOGIN = f.[Nom d'annuaire en UTF-8]
LEFT OUTER JOIN svi s
on e.Usagers = s.[No d'appel]
order by e.Usagers
As you can see, my CASE statements aren't nasted, because I'm using WHEN... THEN ... syntax and then I simply put ELSE ... END statement.
However, when I try to filter my output using for example filter :
WHERE [Délégation] = 'Euralille'
I just get 2 errors :
Msg 8180, Level 16, State 1, Line 1
Msg 125, Level 15, State 4, Line 1
Explanation of the Error Numbers:
Msg 8180 = Statement(s) could not be prepared.
Msg 125 = Case expressions may only be nested to level %d.
(Sorry for the poor french traduction)
I've been looking for the answer on MS SQL Website, and on StackOverflow but the problem is always that the CASE statements are nasted. But to me, I'm not in this situation...
Thanks in advance, I'm quite a beginner in this world.

Did you filter like this? Note that I put all of your query into a subquery and then applied the where clause afterwards:
select * from
(select
e.[Nom d'annuaire],
e.[Prénom annuaire],
f.[Nom d'annuaire en UTF-8] AS login,
e.Usagers as [Numéro interne],
d.[Numéro externe],
s.[Préfixe No abr direct],
v.fonction,
CASE
WHEN v.lib_delegation = 'Service informatique' THEN 'Euralille'
WHEN v.delegation = 'test_national' THEN 'Maillot'
WHEN v.delegation = 'test_tpe' THEN 'Maillot'
WHEN v.delegation = 'test_paris-idf' THEN 'Maillot'
WHEN v.delegation = 'test_grands-comptes' THEN 'Maillot'
WHEN v.delegation = 'cotestrm' THEN 'Marcq en baroeul'
WHEN v.delegation = 'test_sud-ouest' THEN 'Bruges'
WHEN v.delegation = 'test_normandie' THEN 'Caen'
WHEN v.delegation = 'test_bourgogne' THEN 'Dijon'
WHEN v.delegation = 'test_ouest' THEN 'Ouest'
WHEN v.lib_delegation = 'Centre Limousin' THEN 'Tours'
WHEN v.delegation = 'test_est' THEN 'Nancy'
WHEN v.delegation = 'test_mediterranee' THEN 'Aix'
WHEN v.delegation = 'test_rhone-alpes' THEN 'Lyon'
ELSE v.delegation
END AS Délégation,
v.lib_delegation
from ale e
INNER JOIN ale f
on e.Usagers = f.Usagers AND f.[No alias] = 1 and e.[No Alias] = 0
INNER JOIN Delegation d
on f.Usagers = d.[No annuaire]
INNER JOIN V_users v
on v.LOGIN = f.[Nom d'annuaire en UTF-8]
LEFT OUTER JOIN svi s
on e.Usagers = s.[No d'appel]
order by e.Usagers) as subTotalQuery
WHERE [Délégation] = 'Euralille'

Related

Expression.Error Power Query EXCEL

I have a problem with a dynamic parameter in Power Query. There's the code:
let
Parametro = Excel.CurrentWorkbook(){[Name="Parametro"]}[Content],
InicioExec_Valor = Parametro{0}[Valor],
FimExec_Valor = Parametro{1}[Valor],
Fonte = Sql.Database("DATABASE", "TABLE", [Query="select#(lf)#(lf)o.cd_controle, exe.nm_pessoa AS Executante, o.numero AS OM, #(lf)CONVERT(nvarchar(10), o.dt_abertura, 103) AS Abertura,#(lf)o.medidor Horimetro_OM,#(lf)p.nm_pessoa AS Cliente, #(lf)e.nm_equipto AS Equipamento, pat.nr_patrimonio AS Patrimonio, #(lf)CONVERT(nvarchar(10), o.dt_autoriz_execucao, 103) AS Inicio_Exec, #(lf)CONVERT(nvarchar(10), o.dt_encos_oficina, 103) AS Fim_Exec, Z.nm_apelido AS Unidade, #(lf)CASE WHEN fl_preventiva = 'C' THEN 'Corretiva' #(lf)WHEN fl_preventiva = 'P' then 'PREVENTIVA'#(lf)WHEN fl_preventiva = 'R' then 'INSPEÇÃO RESUMIDA'#(lf)WHEN fl_preventiva = 'V' then 'INSPEÇÃO PREVENTIVA'#(lf)WHEN fl_preventiva = 'E' then 'ENTREGA TÉCNICA'#(lf)else 'Indefinido' end AS 'Corret_Preven'#(lf),CONVERT(nvarchar(10), fl_remessa.dt_saida, 103) AS DataSaida#(lf),fl_rem_equ.vl_medidor Horimetro_Remessa#(lf),CONVERT(nvarchar(10), o.dt_abertura, 103) AS Abertura#(lf),o.medidor Horimetro_OM#(lf)#(lf)from orcos o#(lf)inner join controle c on (c.cd_controle = o.cd_controle)#(lf)inner join wcore_oid oid on (oid.cd_oid = c.cd_oid)#(lf)left outer join empresa AS Z ON Z.cd_empresa = o.cd_empresa #(lf)left outer join equipto e on (e.cd_equipto = o.cd_equipto)#(lf)left outer join pessoa f on (f.cd_pessoa = o.cd_pessoa_tec)#(lf)left outer join pessoa p on (p.cd_pessoa = o.cd_pessoa)#(lf)left outer join pessoa exe on (exe.cd_pessoa = o.cd_pessoa_exe)#(lf)left outer join patrimon pat on (pat.cd_patrimonio = o.cd_patrimonio)#(lf)left outer join est_almox x on x.cd_almox = pat.cd_almox#(lf)left outer join empresa emp on emp.cd_empresa = o.cd_empresa_origem #(lf)#(lf)left outer JOIN dbo.fich_loc ON (fich_loc.cd_controle= o.cd_controle_loc)#(lf)left outer JOIN dbo.fl_remessa fl_remessa ON (fl_remessa.cd_controle = dbo.fich_loc.cd_controle)#(lf)#(lf)INNER JOIN(select max(fl_remessa.cd_flremessa)cd_flremessa, A.cd_controle #(lf)#(tab)#(tab)#(tab)#(tab)#(tab) from dbo.fl_remessa#(lf)#(tab)#(tab)#(tab)#(tab)#(tab) inner join dbsislocsalvador..fich_loc on (fl_remessa.cd_controle = dbo.fich_loc.cd_controle)#(lf)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab) inner join dbsislocsalvador..orcos a on (fich_loc.cd_controle= A.cd_controle_loc#(lf)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab) and fl_remessa.dt_saida<=a.dt_abertura) #(lf)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab) inner join controle c on (c.cd_controle = A.cd_controle)#(lf)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab) #(lf)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab) left outer join patrimon pat on (pat.cd_patrimonio = a.cd_patrimonio)#(lf)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab) LEFT OUTER JOIN dbo.fl_rem_equ AS fl_rem_equ ON pat.cd_patrimonio = fl_rem_equ.cd_patrimonio AND fl_remessa.cd_flremessa = fl_rem_equ.cd_flremessa#(lf) #(tab)#(tab) left outer JOIN dbo.loc_flremequ_xplano AS loc_flremequ_xplano ON loc_flremequ_xplano.cd_flremequ = fl_rem_equ.cd_flremequ #(lf) #(tab)#(tab)#(tab) left outer JOIN dbo.equipto AS equipto ON fl_rem_equ.cd_equipto = equipto.cd_equipto#(lf)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab) #(lf)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab) where #(lf)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab) #(lf)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab) ((NOT EXISTS(select top 1 * from config_tag_xoid)) OR (c.cd_oid not in (select txo.cd_oid from #(lf)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab) (select * from ( select cd_tag, nm_tag , (30) as fl_acesso from config_tag t where t.fl_ativo in ('S') ) tags #(lf)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab) WHERE (fl_acesso = 10) ) tags inner join config_tag_xoid txo on tags.cd_tag = txo.cd_tag where 3=3 /*filter_tag_clause*/ #(lf)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab) group by txo.cd_oid))) and ( ( (a.cd_controle_loc is not null and a.cd_fldevolucao is null) ) ) and#(lf)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab) (a.cd_empresa IN (24,45,5,46,20,29,43,15,48,10,1,22,8,34,49,9,47,52,7)) and ( ( a.cd_local is null or a.cd_local in (0,1,2,3,5,6,7,8,9) ) ) #(lf)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab) AND (a.cd_controle_loc IS NOT NULL) AND (a.cd_fldevolucao IS NULL) #(lf)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab) and (a.fl_preventiva in ('C', 'P', 'R', 'V', 'E')) #(lf)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab) and (equipto.cd_grupo in (1478, 1480, 1481, 1482, 1483, 1484, 1485, 1486, 1487, 1488, 1491, 1492, 1548, 1549))#(lf)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab) #(lf)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab) group by a.cd_controle) AS fl_remessa_max #(lf)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab) ON fl_remessa.cd_flremessa = fl_remessa_max.cd_flremessa#(lf)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab) and fl_remessa_max.cd_controle = o.cd_controle #(lf)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab) #(lf)LEFT OUTER JOIN dbo.fl_rem_equ AS fl_rem_equ ON pat.cd_patrimonio = fl_rem_equ.cd_patrimonio AND fl_remessa.cd_flremessa = fl_rem_equ.cd_flremessa#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab) #(lf)left outer JOIN dbo.equipto AS equipto ON fl_rem_equ.cd_equipto = equipto.cd_equipto#(lf) #(tab)#(tab)#(tab)#(tab)#(tab) #(lf)WHERE ((NOT EXISTS(select top 1 * from config_tag_xoid)) OR (c.cd_oid not in (select txo.cd_oid from #(lf)(select * from ( select cd_tag, nm_tag , (30) as fl_acesso from config_tag t where t.fl_ativo in ('S') ) tags #(lf)WHERE (fl_acesso = 10) ) tags inner join config_tag_xoid txo on tags.cd_tag = txo.cd_tag where 3=3 /*filter_tag_clause*/ #(lf)group by txo.cd_oid))) and ( ( (o.cd_controle_loc is not null and o.cd_fldevolucao is null) ) ) and#(lf)(o.cd_empresa IN (24,45,5,46,20,29,43,15,48,10,1,22,8,34,49,9,47,52,7)) and ( ( o.cd_local is null or o.cd_local in (0,1,2,3,5,6,7,8,9) ) ) #(lf)AND (o.cd_controle_loc IS NOT NULL) AND (o.cd_fldevolucao IS NULL) #(lf)and (o.fl_preventiva in ('C', 'P', 'R', 'V', 'E')) #(lf)and (equipto.cd_grupo in (1478, 1480, 1481, 1482, 1483, 1484, 1485, 1486, 1487, 1488, 1491, 1492, 1548, 1549))"]),
#"Tipo Alterado" = Table.TransformColumnTypes(Fonte,{{"Abertura", type date}, {"Inicio_Exec", type date}, {"Fim_Exec", type date}, {"DataSaida", type date}}),
#"Colunas Removidas" = Table.RemoveColumns(#"Tipo Alterado",{"cd_controle", "Horimetro_OM2", "Abertura2"}),
#"Filtro Datas" = Table.SelectRows(#"Colunas Removidas", each [Fim_Exec] >= InicioExec_Valor and [Fim_Exec] <= FimExec_Valor)
in
#"Filtro Datas"
And occur that error:
Expression.Error: Não conseguimos aplicar o operador < aos tipos
Number e Date. Detalhes:
Operator=<
Left=42795
Right=01/06/2007
How can I solve that?
Note: MY PARAMETER (01/03/2017) ARE FORMATED AS TEXT.
Although I don't read Spanish, looks like it says you cannot compare number and date with < operator.
It is good practice, by the way, to first convert parameters to the proper type:
InicioExec_Valor = Date.From(Parametro{0}[Valor]),
FimExec_Valor = Date.From(Parametro{1}[Valor]),`
Try this.
If it won't work, determine step that generates error by clicking them one-by-one.
There is more I wonder about. Why do you convert Fim_Exec to nvarchar and then to date?
1. #(lf)CONVERT(nvarchar(10), o.dt_encos_oficina, 103) AS Fim_Exec
2. {"Fim_Exec", type date},
Why don't use Fim_Exec = CAST(o.dt_encos_oficina as date)? (or datetime, depending on SQL Server version) in the query?
Same applies to other columns.
Next, it is best practice not to use native queries unless absolutely required.
And, yes, this is largest and most complex query I've seen up to date. :)

ORA-00918: column ambiguously defined when choose a list of paramlistpa

I know this question has been asked multiple times before, but I've seen every discussion about this here but none could help me to solve my problem.
So my problem is I got this error. From what I understand is this error was caused by column could not determine from which table it would pull the data, hence we can solve it by giving the column name variable such as s.sampleid as ssampleid. Right?
Here are my codes
select * from(select S.S_SAMPLEID,s.collectiondt,SDI.PARAMLISTID||'|'||SDI.PARAMID as paramlistparam,s.u_samcondition,SDI.enteredtext,sdi.displayunits As Unit, sd.s_datasetstatus
case when SDIS.condition = 'Pass' then 'Pass'
when SDIS.condition = 'Fail' then 'Fail'
when SDIS.condition = 'Warning' then 'Warning'
when SDIS.waivedflag = 'Y' then 'Waived'
else 'No Spec' end as quality
from s_sample s left join sdidata sd on s.s_sampleid=sd.keyid1 AND SD.SDCID = 'Sample'
LEFT JOIN SDIDATAITEM SDI ON sd.keyid1 = SDI.KEYID1 AND SDI.SDCID = 'Sample' AND SD.PARAMLISTVERSIONID = SDI.PARAMLISTVERSIONID AND SD.VARIANTID = SDI.VARIANTID AND SD.DATASET = SDI.DATASET AND SD.PARAMLISTID = SDI.PARAMLISTID
LEFT JOIN SDIDATAITEMSPEC SDIS ON SDI.KEYID1 = SDIS.KEYID1 AND SDI.PARAMLISTVERSIONID = SDIS.PARAMLISTVERSIONID AND SDI.VARIANTID = SDIS.VARIANTID AND SDI.DATASET = SDIS.DATASET AND SDI.PARAMLISTID = SDIS.PARAMLISTID AND SDI.PARAMID = SDIS.PARAMID AND SDI.PARAMTYPE = SDIS.PARAMTYPE AND SDIS.SDCID = 'Sample'
where s.samplepointid='"+samplepoint+"' and sdi.paramlistid in ('"+paramlistid+"') and sdi.paramid in ('"+paramid+"') and s.collectiondt between '"+startdt+"' and '"+enddt+"' and sd.s_datasetstatus in ('"+statussql+"') and SDIS.condition in ('"+qualitysql+"'))
pivot(max(enteredtext) as result, max(Unit) as unit for paramlistparam in ('"+paramlistpa+"')) order by collectiondt
And here are what I did by giving variables to the column
select * from(select s.S_SAMPLEID as ssampleid,s.collectiondt,SDI.PARAMLISTID||'|'||SDI.PARAMID as paramlistparam,s.u_samcondition as scondition,SDI.enteredtext as sdienteredtext,SDI.displayunits As Unit, sd.s_datasetstatus as sddataset
case when SDIS.condition = 'Pass' then 'Pass'
when SDIS.condition = 'Fail' then 'Fail'
when SDIS.condition = 'Warning' then 'Warning'
when SDIS.waivedflag = 'Y' then 'Waived'
else 'No Spec' end as quality
from s_sample s left join sdidata sd on s.s_sampleid=sd.keyid1 AND SD.SDCID = 'Sample'
LEFT JOIN SDIDATAITEM SDI ON sd.keyid1 = SDI.KEYID1 AND SDI.SDCID = 'Sample' AND SD.PARAMLISTVERSIONID = SDI.PARAMLISTVERSIONID AND SD.VARIANTID = SDI.VARIANTID AND SD.DATASET = SDI.DATASET AND SD.PARAMLISTID = SDI.PARAMLISTID
LEFT JOIN SDIDATAITEMSPEC SDIS ON SDI.KEYID1 = SDIS.KEYID1 AND SDI.PARAMLISTVERSIONID = SDIS.PARAMLISTVERSIONID AND SDI.VARIANTID = SDIS.VARIANTID AND SDI.DATASET = SDIS.DATASET AND SDI.PARAMLISTID = SDIS.PARAMLISTID AND SDI.PARAMID = SDIS.PARAMID AND SDI.PARAMTYPE = SDIS.PARAMTYPE AND SDIS.SDCID = 'Sample'
where s.samplepointid='"+samplepoint+"' and sdi.paramlistid in ('"+paramlistid+"') and sdi.paramid in ('"+paramid+"') and s.collectiondt between '"+startdt+"' and '"+enddt+"' and sd.s_datasetstatus in ('"+statussql+"') and SDIS.condition in ('"+qualitysql+"')
or s.samplepointid='"+samplepoint+"' and sdi.paramlistid in ('"+paramlistid+"') and sdi.paramid in ('"+paramid+"') and s.collectiondt between '"+startdt+"' and '"+enddt+"' and sd.s_datasetstatus in ('"+statussql+"') and SDIS.condition is null)
pivot(max(sdienteredtext) as result, max(Unit) as unit for paramlistparam in ('"+paramlistpa+"')) order by collectiondt
From what I test, the error was caused by paramlistpa

Updating multiple tables inside cursor not working

I have two records in a table A. This helps me to update table 2 however the update is not been applied. This is the SQL code where I added a validation section that allow me to determinate the problem.
DECLARE abc CURSOR FOR
select d.cod_suc, d.cod_ramo_comercial, d.nro_pol, d.cod_item, d.cod_ramo_tecnico, d.cod_tarifa,
d.id_stro, d.nro_stro, d.fec_hora_reclamo, d.fec_aviso, d.fec_registro, d.fec_ingreso_contable,
d.txt_place_of_accident, d.id_substro, d.txt_nombre_cober, d.txt_direccion_bien_siniestrado, d.txt_descripcion_perdida, d.txt_cheque_a_nom, d.cod_manager_code, d.importe_pago_eq
from table1 d
where d.fec_hora_reclamo between '20140801' and '20150731'
and (d.cod_suc = 2 and d.cod_ramo_comercial = 255 and d.nro_pol = 1000001 and d.cod_item = 5)
OPEN abc
FETCH abc INTO #cod_suc, #cod_ramo_comercial, #nro_pol, #cod_item, #cod_ramo_tecnico, #cod_tarifa,
#id_stro, #nro_stro, #fec_hora_reclamo, #fec_aviso, #fec_registro, #fec_ingreso_contable,
#txt_place_of_accident, #id_substro, #txt_nombre_cober, #txt_direccion_bien_siniestrado, #txt_descripcion_perdida, #txt_cheque_a_nom, #cod_manager_code, #importe_pago_eq
WHILE (##FETCH_STATUS = 0)
BEGIN
select #varIDPV = min(id_pv), #varCodTarifa = min(cod_tarifa)
from #portfolio p
where p.cod_suc = #cod_suc and p.cod_ramo_comercial = #cod_ramo_comercial and p.Poliza = #nro_pol and p.Item = #cod_item and p.cod_ramo_tecnico = #cod_ramo_tecnico
and p.[ID Incident] IS NULL
/**************************************************************************************************************
-- Validation section
-- First record:
mid(id_pv) = 100, min(cod_tarifa) = 1
-- Second record:
mid(id_pv) = 100, min(cod_tarifa) = 1
--> Should be mid(id_pv) = 100, min(cod_tarifa) = 2
*/
select min(id_pv), min(cod_tarifa)
from #portfolio p
where p.cod_suc = #cod_suc and p.cod_ramo_comercial = #cod_ramo_comercial and p.Poliza = #nro_pol and p.Item = #cod_item and p.cod_ramo_tecnico = #cod_ramo_tecnico
and p.[ID Incident] IS NULL
/**************************************************************************************************************/
update p set p.[ID Incident] = #id_stro, p.[No. Incident] = #nro_stro,
p.[Fecha Accidente] = #fec_hora_reclamo, p.[Fecha Notificacion] = #fec_aviso, p.[Fecha Registro] = #fec_registro, p.[Fecha Pago] = #fec_ingreso_contable,
p.[Lugar Accidente] = #txt_place_of_accident, p.[ID Subsiniestro] = #id_substro, p.[Cobertura Amparo] = #txt_nombre_cober, p.[Direccion Bien Siniestrado] = #txt_direccion_bien_siniestrado, p.[Descripcion Perdida] = #txt_descripcion_perdida, p.[Pago A] = #txt_cheque_a_nom, p.[Referida] = #cod_manager_code,
[Incurrido R12] = #importe_pago_eq, [Incurrido Cerrados R12] = #importe_pago_eq
from #portfolio p
where p.cod_suc = #cod_suc and p.cod_ramo_comercial = #cod_ramo_comercial and p.Poliza = #nro_pol and p.Item = #cod_item and p.cod_ramo_tecnico = #cod_ramo_tecnico
and p.[ID Incident] IS NULL and p.id_pv = #varIDPV and p.cod_tarifa = #varCodTarifa
FETCH abc INTO #cod_suc, #cod_ramo_comercial, #nro_pol, #cod_item, #cod_ramo_tecnico, #cod_tarifa,
#id_stro, #nro_stro, #fec_hora_reclamo, #fec_aviso, #fec_registro, #fec_ingreso_contable,
#txt_place_of_accident, #id_substro, #txt_nombre_cober, #txt_direccion_bien_siniestrado, #txt_descripcion_perdida, #txt_cheque_a_nom, #cod_manager_code, #importe_pago_eq
END
CLOSE abc
DEALLOCATE abc
When I see the result, only the first record is updated.
I found the problem. I forgot to add one condition more in update (inside where). Thanks for the support

WITH clause causing ORA-32044

I have a query which is used in an OBIEE report which erroring out for "ora-32044 while executing recursive WITH query". Tried running in SQL and it is erroring out in there as well. Can anyone take a look at the query and suggest how it can be run without the WITH clause. I have seen examples but without any level, for a case where WITH has been used in the middle of the query, how can it be rewritten without WITH clause so no ora-32044 occurs
`SELECT 1 dummy, oh.order_number, ol.line_number, ol.shipment_number,
oh.ordered_date, ot.NAME order_type, items.concatenated_segments item,
items.description item_description
,bom.assembly_item
,bom.component_item
,bom.implementation_date
,bom.disable_date as date_disabled
,bom.component_item_description
,ol.flow_status_code line_status
,ol.ordered_quantity,
ol.shipped_quantity,
ol.unit_selling_price,
ol.fulfilled_quantity,
ol.invoiced_quantity,
ol.cancelled_quantity,
ol.schedule_ship_date,
ol.promise_date,
ol.actual_shipment_date,
ol.request_date,
cust_acct.account_number customer_number,
party.party_name customer,
ct.trx_number invoice_number,
ctl.line_number inv_line_number,
ctl.extended_amount,
ct.trx_date invoice_date,
ct.creation_date invoice_creation_date,
wnd.NAME delivery_number,
wdd.released_status,
hp2.party_name endcustomer,
oh.attribute6 business_unit,
TO_CHAR (ol.actual_shipment_date, 'MM/YY') shipped_month,
TO_CHAR (ol.actual_shipment_date, '"Q"Q YY') shipped_qtr,
TO_CHAR (ol.actual_shipment_date, 'YYYY') shipped_year,
mc.segment1 product_group, mc.segment2 product_subgroup,
items.attribute7 drive_capacity,
items.attribute10 drive_form,
items.attribute8 drive_speed,
items.attribute6 drive_type,
items.attribute12 AgileParttype,
items.organization_id,
case when dis.arithmetic_operator = 'NEWPRICE' then operand
when dis.arithmetic_operator = '%'
then (ol.unit_selling_price* ol.ordered_quantity)*dis.operand/100.0
else NVL((NVL (ol.pricing_quantity, 0) * NVL (dis.adjusted_amount, 0)),0) end
discount_amount
FROM apps.oe_order_headers_all oh,
apps.oe_order_lines_all ol,
apps.wsh_new_deliveries wnd,
apps.wsh_delivery_assignments wda,
apps.wsh_delivery_details wdd,
apps.ra_customer_trx_all ct,
apps.ra_customer_trx_lines_all ctl,
apps.oe_transaction_types_tl ot,
(SELECT *
FROM apps.mtl_system_items_vl
WHERE organization_id = 84
) items,
apps.hz_parties party,
apps.hz_cust_accounts cust_acct,
apps.hz_parties hp2,
apps.hz_cust_accounts hca2,
apps.mtl_item_categories mic,
(SELECT *
FROM apps.mtl_category_sets
WHERE category_set_name = 'Inventory') ms,
apps.mtl_categories mc,
(select a.header_id, a.line_id ,a.list_header_id,a.creation_date,
a.arithmetic_operator,a.unit_selling_price,a.operand,
a.ordered_quantity, a.adjusted_amount
from
(select oh.header_id,
ol.line_id,pl.list_header_id,pl.creation_date,opa.arithmetic_operator,
ol.unit_selling_price,opa.operand,
ol.ordered_quantity, opa.adjusted_amount
from apps.oe_price_adjustments opa,
apps.oe_order_lines_all ol,
apps.oe_order_headers_all oh,
apps.qp_list_headers_vl pl
where oh.header_id = opa.header_id
and ol.line_id = opa.line_id
and opa.list_header_id = pl.list_header_id
and opa.applied_flag = 'Y'
AND opa.list_line_type_code = 'DIS') a,
(select oh.header_id, ol.line_id,max(pl.creation_date) creation_date, min(operand)
operand
from apps.oe_price_adjustments opa,
apps.oe_order_lines_all ol,
apps.oe_order_headers_all oh,
apps.qp_list_headers_vl pl
where oh.header_id = opa.header_id
and ol.line_id = opa.line_id
and opa.list_header_id = pl.list_header_id
and opa.applied_flag = 'Y'
AND opa.list_line_type_code = 'DIS'
group by oh.header_id, ol.line_id) b
where
a.header_id = b.header_id
and a.line_id = b.line_id
and a.creation_date = b.creation_date
and a.operand = b.operand ) dis
,(
WITH recursiveBOM (parent_item_id,parent_item,parent_org_id,assembly_item_id,
assembly_item,assembly_org_id,
component_item_id,component_item, component_item_description,implementation_date,
disable_date) AS
(
SELECT item.inventory_item_id parent_item_id
,item.segment1 parent_item
,item.organization_id parent_org_id
,bom.assembly_item_id
,msi.segment1 assembly_item
,bom.organization_id assembly_org_id
,bic.component_item_id
,msi2.segment1 component_item
,msi2.description component_item_description
,bic.implementation_date
,bic.disable_date
FROM apps.mtl_system_items_b item, apps.bom_bill_of_materials_v bom,
apps.bom_inventory_components_v bic, apps.mtl_system_items msi,
apps.mtl_system_items msi2
WHERE 1=1
AND bom.assembly_item_id = item.inventory_item_id
AND bom.organization_id = item.organization_id
AND bic.bill_sequence_id = bom.common_bill_sequence_id
AND msi.inventory_item_id = bom.assembly_item_id
AND msi.organization_id = bom.organization_id
AND msi2.inventory_item_id = bic.component_item_id
AND msi2.organization_id = msi.organization_id
AND bic.implementation_date IS NOT NULL
AND item.organization_id = 84
AND item.inventory_item_id > 0
UNION ALL
SELECT parent.parent_item_id
,parent.parent_item
,parent.parent_org_id
,bom.assembly_item_id
,msi.segment1 assembly_item
,bom.organization_id assembly_org_id
,bic.component_item_id
,msi2.segment1 component_item
,msi2.description component_item_description
,bic.implementation_date
,bic.disable_date
FROM recursiveBOM parent, apps.bom_bill_of_materials_v bom,
apps.bom_inventory_components_v bic, apps.mtl_system_items msi,
apps.mtl_system_items msi2
WHERE 1=1
AND bic.bill_sequence_id = bom.common_bill_sequence_id
AND msi.inventory_item_id = bom.assembly_item_id
AND msi.organization_id = bom.organization_id
AND msi2.inventory_item_id = bic.component_item_id
AND msi2.organization_id = msi.organization_id
AND bic.implementation_date IS NOT NULL
AND bom.assembly_item_id = parent.component_item_id
AND bom.organization_id = parent.assembly_org_id
AND bom.organization_id = 84
AND parent.assembly_org_id = 84
)
SELECT DISTINCT parent_item_id,parent_item,parent_org_id
,assembly_item_id
,assembly_item
,assembly_org_id
,component_item_id
,component_item
,component_item_description
,implementation_date
,disable_date
FROM recursiveBOM
) bom
WHERE oh.header_id = ol.header_id
AND ol.line_id = wdd.source_line_id
AND wdd.source_code = 'OE'
AND wda.delivery_detail_id = wdd.delivery_detail_id
AND wda.delivery_id = wnd.delivery_id
AND ol.line_id = ctl.interface_line_attribute6(+)
AND ctl.customer_trx_id = ct.customer_trx_id(+)
AND oh.order_type_id = ot.transaction_type_id
AND ol.inventory_item_id = items.inventory_item_id
AND ol.sold_to_org_id = cust_acct.cust_account_id
AND cust_acct.party_id = party.party_id
AND oh.end_customer_id = hca2.cust_account_id(+)
AND hca2.party_id = hp2.party_id(+)
AND items.inventory_item_id = mic.inventory_item_id(+)
AND items.organization_id = mic.organization_id(+)
AND mic.category_id = mc.category_id(+)
AND mic.category_set_id = ms.category_set_id(+)
and ol.line_id = dis.line_id(+)
and ol.header_id = dis.header_id(+)
AND bom.parent_item_id (+) = items.inventory_item_id
AND bom.parent_org_id (+) = items.organization_id `

Oracle trigger pl/sql errors

Here is a trigger I want to create on one of my tables that should move certaint data from 3 tables to 4th table either via update or insert depending if it exists or not. Here is what I came up with so far.
CREATE or REPLACE TRIGGER UMELD_PWEL_TRIG
AFTER INSERT OR UPDATE ON USCADM.USC_UMELD
REFERENCING NEW AS NEW OLD AS OLD
FOR EACH ROW WHEN (NEW.PWEL_STAT = 1)
DECLARE
ILOSC INT;
BEGIN
select count(*) INTO ILOSC from USCADM.USC_PESELU where RK = :NEW.RK AND DZ = :NEW.DZ AND NR = :NEW.NR AND NRP = :NEW.NRP AND IDUSC = :NEW.IDUSC;
if ILOSC = 1 then
update PESEL set PESEL.IDUSC = ODPSKR.IDUSC, PESEL.RK = ODPSKR.RK, PESEL.DZ = ODPSKR.DZ, PESEL.NR = ODPSKR.NR, PESEL.NRP = ODPSKR.NRP, PESEL.DZPL = ODPSKR.DZPL,
PESEL.NRO = ODPSKR.NRO, PESEL.NAZWPL = ODPSKR.NAZWPL, PESEL.NAZW = ODPSKR.NAZW, PESEL.IMIE1PL = ODPSKR.IMIE1PL, PESEL.IMIE1 = ODPSKR.IMIE1, PESEL.IMIER = ODPSKR.IMIER,
PESEL.UR_DATAR = ODPSKR.UR_DATAR, PESEL.UR_DATAM = ODPSKR.UR_DATAM, PESEL.UR_DATAD = ODPSKR.UR_DATAD, PESEL.UR_MSC = ODPSKR.UR_MSC, PESEL.IDUR_MSC = ODPSKR.IDUR_MSC,
PESEL.KTUR_MSC = USCADM.fnNrStatToTeryt(ODPSKR.IDUR_MSC), PESEL.UR_KRAJ = ODPSKR.UR_KRAJ, PESEL.OJ_NAZW = ODPSKR.OJ_NAZW, PESEL.OJ_NAZWR = ODPSKR.OJ_NAZWR,
PESEL.OJ_IMIE1 = ODPSKR.OJ_IMIE1, PESEL.OJ_IMIER = ODPSKR.OJ_IMIER, PESEL.MA_NAZW = ODPSKR.MA_NAZW, PESEL.MA_NAZWRPL = ODPSKR.MA_NAZWRPL, PESEL.MA_NAZWR = ODPSKR.MA_NAZWR,
PESEL.MA_IMIE1 = ODPSKR.MA_IMIE1, PESEL.MA_IMIER = ODPSKR.MA_IMIER, PESEL.PESEL = MELD.PESEL, PESEL.A_MSC = MELD.A_MSC, PESEL.IDA_MSC = MELD.IDA_MSC,
PESEL.KRA_MSC = USCADM.fnNrStatToTeryt(MELD.IDA_MSC), PESEL.A_MSC_KP = MELD.A_MSC_KP, PESEL.A_ULICA = MELD.A_ULICA, PESEL.A_DOM = MELD.A_DOM, PESEL.A_LOKAL = MELD.A_LOKAL,
PESEL.A_KRAJ = MELD.A_KRAJ, PESEL.TYP_ADR = MELD.TYP_ADR, PESEL.MPESEL = MELD.MPESEL, PESEL.OPESEL = MELD.OPESEL, PESEL.M_DOW_TYP = MELD.M_DOW_TYP, PESEL.M_DOW_NR = MELD.M_DOW_NR,
PESEL.M_DOW_WYD = MELD.M_DOW_WYD, PESEL.M_DOW_MSC = MELD.M_DOW_MSC, PESEL.IDM_DOW_MSC = MELD.IDM_DOW_MSC, PESEL.M_DOW_DATAR = MELD.M_DOW_DATAR, PESEL.M_DOW_DATAM = MELD.M_DOW_DATAM,
PESEL.M_DOW_DATAD = MELD.M_DOW_DATAD, PESEL.M_DOW_KRAJ = MELD.M_DOW_KRAJ, PESEL.O_DOW_TYP = MELD.O_DOW_TYP, PESEL.O_DOW_NR = MELD.O_DOW_NR, PESEL.O_DOW_WYD = MELD.O_DOW_WYD,
PESEL.O_DOW_MSC = MELD.O_DOW_MSC, PESEL.IDO_DOW_MSC = MELD.IDO_DOW_MSC, PESEL.O_DOW_DATAR = MELD.O_DOW_DATAR, PESEL.O_DOW_DATAM = MELD.O_DOW_DATAM, PESEL.O_DOW_DATAD = MELD.O_DOW_DATAD,
PESEL.O_DOW_KRAJ = MELD.O_DOW_KRAJ, PESEL.PLEC = ODPSKR.PLEC, PESEL.WA_DATAR = AKT.WA_DATAR, PESEL.WA_DATAM = AKT.WA_DATAM, PESEL.WA_DATAD = AKT.WA_DATAD, PESEL.ODP_KTO = ODPSKR.IDMR_KTO,
PESEL.ODP_KIEDY = ODPSKR.IDMR_KIEDYS, PESEL.MEL_KTO = MELD.IDMR_KTO, PESEL.MEL_KIEDY = ODPSKR.IDMR_KIEDYS, PESEL.ZD_DATAR = ODPSKR.ZD_DATAR, PESEL.ZD_DATAM = ODPSKR.ZD_DATAM,
PESEL.ZD_DATAD = ODPSKR.ZD_DATAD, PESEL.ZD_UPR_DATAR = ODPSKR.ZD_UPR_DATAR, PESEL.ZD_UPR_DATAM = ODPSKR.ZD_UPR_DATAM, PESEL.ZD_UPR_DATAD = ODPSKR.ZD_UPR_DATAD, PESEL.ZD_N = ODPSKR.ZD_N,
PESEL.ZD_KOD = ODPSKR.ZD_KOD, PESEL.ZD_DOK = ODPSKR.ZD_DOK, PESEL.ZD_ORG = ODPSKR.ZD_ORG, PESEL.ZD_ORG_OPIS = ODPSKR.ZD_ORG_OPIS, PESEL.ZD_MSC = ODPSKR.ZD_MSC, PESEL.IDZD_MSC = ODPSKR.IDZD_MSC,
PESEL.AKT_KIEDY = AKT.IDMR_KIEDYS
from USCADM.USC_UODPSKR as ODPSKR
join USCADM.USC_UMELD as MELD on ODPSKR.RK = MELD.RK AND ODPSKR.DZ = MELD.DZ AND ODPSKR.NR = MELD.NR AND ODPSKR.NRP = MELD.NRP AND ODPSKR.IDUSC = MELD.IDUSC
join USCADM.USC_UAKT as AKT on ODPSKR.RK = AKT.RK AND ODPSKR.DZ = AKT.DZ AND ODPSKR.NR = AKT.NR AND ODPSKR.NRP = AKT.NRP AND ODPSKR.IDUSC = AKT.IDUSC
join USCADM.USC_PESELU as PESEL on ODPSKR.RK = PESEL.RK AND ODPSKR.DZ = PESEL.DZ AND ODPSKR.NR = PESEL.NR AND ODPSKR.NRP = PESEL.NRP AND ODPSKR.IDUSC = PESEL.IDUSC
WHERE ODPSKR.RK = :NEW.RK AND ODPSKR.DZ = :NEW.DZ AND ODPSKR.NR = :NEW.NR AND ODPSKR.NRP = :NEW.NRP AND ODPSKR.IDUSC = :NEW.IDUSC;
elsif ILOSC = 0 then
insert into USCADM.USC_PESELU (IDUSC, RK, DZ, NR, NRP, DZPL, NRO, NAZWPL, NAZW, IMIE1PL, IMIE1, IMIER, UR_DATAR, UR_DATAM, UR_DATAD, UR_MSC, IDUR_MSC, KTUR_MSC,PESEL.UR_KRAJ,
OJ_NAZW, OJ_NAZWR, OJ_IMIE1, OJ_IMIER, MA_NAZW, MA_NAZWRPL, MA_NAZWR, MA_IMIE1, MA_IMIER, PESEL, A_MSC, IDA_MSC, KRA_MSC, A_MSC_KP, A_ULICA, A_DOM, A_LOKAL,
A_KRAJ, TYP_ADR,MPESEL,OPESEL,M_DOW_TYP,M_DOW_NR, M_DOW_WYD, M_DOW_MSC, IDM_DOW_MSC, M_DOW_DATAR,M_DOW_DATAM, M_DOW_DATAD, M_DOW_KRAJ, O_DOW_TYP, O_DOW_NR, O_DOW_WYD,O_DOW_MSC,
IDO_DOW_MSC,O_DOW_DATAR,O_DOW_DATAM,O_DOW_DATAD, O_DOW_KRAJ, PLEC, WA_DATAR,WA_DATAM, WA_DATAD, ODP_KTO, ODP_KIEDY, MEL_KTO, MEL_KIEDY, ZD_DATAR, ZD_DATAM, ZD_DATAD, ZD_UPR_DATAR,
ZD_UPR_DATAM, ZD_UPR_DATAD, ZD_N, ZD_KOD, ZD_DOK, ZD_ORG, ZD_ORG_OPIS, ZD_MSC, IDZD_MSC, AKT_KIEDY)
VALUES(ODPSKR.IDUSC,ODPSKR.RK,ODPSKR.DZ,ODPSKR.NR,ODPSKR.NRP,ODPSKR.DZPL,ODPSKR.NRO,ODPSKR.NAZWPL,ODPSKR.NAZW,ODPSKR.IMIE1PL,ODPSKR.IMIE1,ODPSKR.IMIER,ODPSKR.UR_DATAR,ODPSKR.UR_DATAM,
ODPSKR.UR_DATAD, ODPSKR.UR_MSC,ODPSKR.IDUR_MSC, USCADM.fnNrStatToTeryt(ODPSKR.IDUR_MSC), ODPSKR.UR_KRAJ, ODPSKR.OJ_NAZW,ODPSKR.OJ_NAZWR, ODPSKR.OJ_IMIE1, ODPSKR.OJ_IMIER,ODPSKR.MA_NAZW,
ODPSKR.MA_NAZWRPL,ODPSKR.MA_NAZWR,ODPSKR.MA_IMIE1,ODPSKR.MA_IMIER,MELD.PESEL,MELD.A_MSC,MELD.IDA_MSC, USCADM.fnNrStatToTeryt(MELD.IDA_MSC), MELD.A_MSC_KP, MELD.A_ULICA, MELD.A_DOM,
MELD.A_LOKAL, MELD.A_KRAJ,MELD.TYP_ADR,MELD.MPESEL,MELD.OPESEL,MELD.M_DOW_TYP,MELD.M_DOW_NR, MELD.M_DOW_WYD,MELD.M_DOW_MSC,MELD.IDM_DOW_MSC,MELD.M_DOW_DATAR,MELD.M_DOW_DATAM,
MELD.M_DOW_DATAD,MELD.M_DOW_KRAJ,MELD.O_DOW_TYP,MELD.O_DOW_NR,MELD.O_DOW_WYD, MELD.O_DOW_MSC,MELD.IDO_DOW_MSC,MELD.O_DOW_DATAR,MELD.O_DOW_DATAM,MELD.O_DOW_DATAD, MELD.O_DOW_KRAJ,
ODPSKR.PLEC,AKT.WA_DATAR,AKT.WA_DATAM,AKT.WA_DATAD,ODPSKR.IDMR_KTO, ODPSKR.IDMR_KIEDYS,MELD.IDMR_KTO,ODPSKR.IDMR_KIEDYS,ODPSKR.ZD_DATAR,ODPSKR.ZD_DATAM, ODPSKR.ZD_DATAD, ODPSKR.ZD_UPR_DATAR,
ODPSKR.ZD_UPR_DATAM,ODPSKR.ZD_UPR_DATAD,ODPSKR.ZD_N, ODPSKR.ZD_KOD,ODPSKR.ZD_DOK,ODPSKR.ZD_ORG,ODPSKR.ZD_ORG_OPIS,ODPSKR.ZD_MSC,ODPSKR.IDZD_MSC, AKT.IDMR_KIEDYS)
from USCADM.USC_UODPSKR as ODPSKR
join USCADM.USC_UMELD as MELD on ODPSKR.RK = MELD.RK AND ODPSKR.DZ = MELD.DZ AND ODPSKR.NR = MELD.NR AND ODPSKR.NRP = MELD.NRP AND ODPSKR.IDUSC = MELD.IDUSC
join USCADM.USC_UAKT as AKT on ODPSKR.RK = AKT.RK AND ODPSKR.DZ = AKT.DZ AND ODPSKR.NR = AKT.NR AND ODPSKR.NRP = AKT.NRP AND ODPSKR.IDUSC = AKT.IDUSC
WHERE ODPSKR.RK = :NEW.RK AND ODPSKR.DZ = :NEW.DZ AND ODPSKR.NR = :NEW.NR AND ODPSKR.NRP = :NEW.NRP AND ODPSKR.IDUSC = :NEW.IDUSC;
end if;
END UMELD_PWEL_TRIG;
I get following errors:
Error(7,5): PL/SQL: SQL Statement ignored
Error(23,5): PL/SQL: ORA-00933: polecenie SQL niepoprawnie zakończone (not finished correctly)
Error(29,5): PL/SQL: SQL Statement ignored
Error(41,5): PL/SQL: ORA-00933: polecenie SQL niepoprawnie zakończone (not finished correctly)
You have confused INSERT...VALUES and INSERT...SELECT syntax. Your INSERT should be:
insert into USCADM.USC_PESELU (IDUSC, RK, DZ, NR, NRP, DZPL, NRO, NAZWPL, NAZW, IMIE1PL, IMIE1, IMIER, UR_DATAR, UR_DATAM, UR_DATAD, UR_MSC, IDUR_MSC, KTUR_MSC,PESEL.UR_KRAJ,
OJ_NAZW, OJ_NAZWR, OJ_IMIE1, OJ_IMIER, MA_NAZW, MA_NAZWRPL, MA_NAZWR, MA_IMIE1, MA_IMIER, PESEL, A_MSC, IDA_MSC, KRA_MSC, A_MSC_KP, A_ULICA, A_DOM, A_LOKAL,
A_KRAJ, TYP_ADR,MPESEL,OPESEL,M_DOW_TYP,M_DOW_NR, M_DOW_WYD, M_DOW_MSC, IDM_DOW_MSC, M_DOW_DATAR,M_DOW_DATAM, M_DOW_DATAD, M_DOW_KRAJ, O_DOW_TYP, O_DOW_NR, O_DOW_WYD,O_DOW_MSC,
IDO_DOW_MSC,O_DOW_DATAR,O_DOW_DATAM,O_DOW_DATAD, O_DOW_KRAJ, PLEC, WA_DATAR,WA_DATAM, WA_DATAD, ODP_KTO, ODP_KIEDY, MEL_KTO, MEL_KIEDY, ZD_DATAR, ZD_DATAM, ZD_DATAD, ZD_UPR_DATAR,
ZD_UPR_DATAM, ZD_UPR_DATAD, ZD_N, ZD_KOD, ZD_DOK, ZD_ORG, ZD_ORG_OPIS, ZD_MSC, IDZD_MSC, AKT_KIEDY)
SELECT ODPSKR.IDUSC,ODPSKR.RK,ODPSKR.DZ,ODPSKR.NR,ODPSKR.NRP,ODPSKR.DZPL,ODPSKR.NRO,ODPSKR.NAZWPL,ODPSKR.NAZW,ODPSKR.IMIE1PL,ODPSKR.IMIE1,ODPSKR.IMIER,ODPSKR.UR_DATAR,ODPSKR.UR_DATAM,
ODPSKR.UR_DATAD, ODPSKR.UR_MSC,ODPSKR.IDUR_MSC, USCADM.fnNrStatToTeryt(ODPSKR.IDUR_MSC), ODPSKR.UR_KRAJ, ODPSKR.OJ_NAZW,ODPSKR.OJ_NAZWR, ODPSKR.OJ_IMIE1, ODPSKR.OJ_IMIER,ODPSKR.MA_NAZW,
ODPSKR.MA_NAZWRPL,ODPSKR.MA_NAZWR,ODPSKR.MA_IMIE1,ODPSKR.MA_IMIER,MELD.PESEL,MELD.A_MSC,MELD.IDA_MSC, USCADM.fnNrStatToTeryt(MELD.IDA_MSC), MELD.A_MSC_KP, MELD.A_ULICA, MELD.A_DOM,
MELD.A_LOKAL, MELD.A_KRAJ,MELD.TYP_ADR,MELD.MPESEL,MELD.OPESEL,MELD.M_DOW_TYP,MELD.M_DOW_NR, MELD.M_DOW_WYD,MELD.M_DOW_MSC,MELD.IDM_DOW_MSC,MELD.M_DOW_DATAR,MELD.M_DOW_DATAM,
MELD.M_DOW_DATAD,MELD.M_DOW_KRAJ,MELD.O_DOW_TYP,MELD.O_DOW_NR,MELD.O_DOW_WYD, MELD.O_DOW_MSC,MELD.IDO_DOW_MSC,MELD.O_DOW_DATAR,MELD.O_DOW_DATAM,MELD.O_DOW_DATAD, MELD.O_DOW_KRAJ,
ODPSKR.PLEC,AKT.WA_DATAR,AKT.WA_DATAM,AKT.WA_DATAD,ODPSKR.IDMR_KTO, ODPSKR.IDMR_KIEDYS,MELD.IDMR_KTO,ODPSKR.IDMR_KIEDYS,ODPSKR.ZD_DATAR,ODPSKR.ZD_DATAM, ODPSKR.ZD_DATAD, ODPSKR.ZD_UPR_DATAR,
ODPSKR.ZD_UPR_DATAM,ODPSKR.ZD_UPR_DATAD,ODPSKR.ZD_N, ODPSKR.ZD_KOD,ODPSKR.ZD_DOK,ODPSKR.ZD_ORG,ODPSKR.ZD_ORG_OPIS,ODPSKR.ZD_MSC,ODPSKR.IDZD_MSC, AKT.IDMR_KIEDYS
from USCADM.USC_UODPSKR as ODPSKR
join USCADM.USC_UMELD as MELD on ODPSKR.RK = MELD.RK AND ODPSKR.DZ = MELD.DZ AND ODPSKR.NR = MELD.NR AND ODPSKR.NRP = MELD.NRP AND ODPSKR.IDUSC = MELD.IDUSC
join USCADM.USC_UAKT as AKT on ODPSKR.RK = AKT.RK AND ODPSKR.DZ = AKT.DZ AND ODPSKR.NR = AKT.NR AND ODPSKR.NRP = AKT.NRP AND ODPSKR.IDUSC = AKT.IDUSC
WHERE ODPSKR.RK = :NEW.RK AND ODPSKR.DZ = :NEW.DZ AND ODPSKR.NR = :NEW.NR AND ODPSKR.NRP = :NEW.NRP AND ODPSKR.IDUSC = :NEW.IDUSC;
CREATE OR replace TRIGGER umeld_pwel_trig
BEFORE INSERT OR UPDATE ON uscadm.usc_umeld
REFERENCING NEW AS NEW OLD AS OLD
FOR EACH ROW
WHEN (NEW.pwel_stat = 1)
DECLARE
ilosc INT := 0;
BEGIN
SELECT Count(*)
INTO ilosc
FROM uscadm.usc_peselu
WHERE rk = :NEW.rk
AND dz = :NEW.dz
AND nr = :NEW.nr
AND nrp = :NEW.nrp
AND idusc = :NEW.idusc;
IF ilosc = 1 THEN
UPDATE uscadm.usc_peselu
SET ( idusc, rk, dz, nr,
nrp, dzpl, nro, nazwpl,
nazw, imie1pl, imie1, imier,
ur_datar, ur_datam, ur_datad, ur_msc,
idur_msc, ktur_msc, ur_kraj, oj_nazw,
oj_nazwr, oj_imie1, oj_imier, ma_nazw,
ma_nazwrpl, ma_nazwr, ma_imie1, ma_imier,
pesel, a_msc, ida_msc, kta_msc,
a_msc_kp, a_ulica, a_dom, a_lokal,
a_kraj, typ_adr, mpesel, opesel,
m_dow_typ, m_dow_nr, m_dow_wyd, m_dow_msc,
idm_dow_msc, m_dow_datar, m_dow_datam, m_dow_datad,
m_dow_kraj, o_dow_typ, o_dow_nr, o_dow_wyd,
o_dow_msc, ido_dow_msc, o_dow_datar, o_dow_datam,
o_dow_datad, o_dow_kraj, plec, wa_datar,
wa_datam, wa_datad, odp_kto, odp_kiedy,
mel_kto, mel_kiedy, zd_datar, zd_datam,
zd_datad, zd_upr_datar, zd_upr_datam, zd_upr_datad,
zd_n, zd_kod, zd_dok, zd_org,
zd_org_opis, zd_msc, idzd_msc ) = (SELECT ODPSKR.idusc,
ODPSKR.rk,
ODPSKR.dz,
ODPSKR.nr,
ODPSKR.nrp,
ODPSKR.dzpl,
ODPSKR.nro,
ODPSKR.nazwpl,
ODPSKR.nazw,
ODPSKR.imie1pl,
ODPSKR.imie1,
ODPSKR.imier,
ODPSKR.ur_datar,
ODPSKR.ur_datam,
ODPSKR.ur_datad,
ODPSKR.ur_msc,
ODPSKR.idur_msc,
uscadm.Fnnrstattoteryt(ODPSKR.idur_msc),
ODPSKR.ur_kraj,
ODPSKR.oj_nazw,
ODPSKR.oj_nazwr,
ODPSKR.oj_imie1,
ODPSKR.oj_imier,
ODPSKR.ma_nazw,
ODPSKR.ma_nazwrpl,
ODPSKR.ma_nazwr,
ODPSKR.ma_imie1,
ODPSKR.ma_imier,
:NEW.pesel,
:NEW.a_msc,
:NEW.ida_msc,
uscadm.Fnnrstattoteryt(:NEW.ida_msc),
:NEW.a_msc_kp,
:NEW.a_ulica,
:NEW.a_dom,
:NEW.a_lokal,
:NEW.a_kraj,
:NEW.typ_adr,
:NEW.mpesel,
:NEW.opesel,
:NEW.m_dow_typ,
:NEW.m_dow_nr,
:NEW.m_dow_wyd,
:NEW.m_dow_msc,
:NEW.idm_dow_msc,
:NEW.m_dow_datar,
:NEW.m_dow_datam,
:NEW.m_dow_datad,
:NEW.m_dow_kraj,
:NEW.o_dow_typ,
:NEW.o_dow_nr,
:NEW.o_dow_wyd,
:NEW.o_dow_msc,
:NEW.ido_dow_msc,
:NEW.o_dow_datar,
:NEW.o_dow_datam,
:NEW.o_dow_datad,
:NEW.o_dow_kraj,
ODPSKR.plec,
AKT.wa_datar,
AKT.wa_datam,
AKT.wa_datad,
ODPSKR.idmr_kto,
ODPSKR.idmr_kiedys,
:NEW.idmr_kto,
:NEW.idmr_kiedys,
ODPSKR.zd_datar,
ODPSKR.zd_datam,
ODPSKR.zd_datad,
ODPSKR.zd_upr_datar,
ODPSKR.zd_upr_datam,
ODPSKR.zd_upr_datad,
ODPSKR.zd_n,
ODPSKR.zd_kod,
ODPSKR.zd_dok,
ODPSKR.zd_org,
ODPSKR.zd_org_opis,
ODPSKR.zd_msc,
ODPSKR.idzd_msc
FROM uscadm.usc_uodpskr ODPSKR
join uscadm.usc_uakt AKT
ON ODPSKR.rk = AKT.rk
AND ODPSKR.dz = AKT.dz
AND ODPSKR.nr = AKT.nr
AND ODPSKR.nrp = AKT.nrp
AND ODPSKR.idusc = AKT.idusc
WHERE ODPSKR.rk = :NEW.rk
AND ODPSKR.dz = :NEW.dz
AND ODPSKR.nr = :NEW.nr
AND ODPSKR.nrp = :NEW.nrp
AND ODPSKR.idusc = :NEW.idusc);
:NEW.pwel_stat := 2;
ELSIF ilosc = 0 THEN
INSERT INTO uscadm.usc_peselu
(idusc,
rk,
dz,
nr,
nrp,
dzpl,
nro,
nazwpl,
nazw,
imie1pl,
imie1,
imier,
ur_datar,
ur_datam,
ur_datad,
ur_msc,
idur_msc,
ktur_msc,
ur_kraj,
oj_nazw,
oj_nazwr,
oj_imie1,
oj_imier,
ma_nazw,
ma_nazwrpl,
ma_nazwr,
ma_imie1,
ma_imier,
pesel,
a_msc,
ida_msc,
kta_msc,
a_msc_kp,
a_ulica,
a_dom,
a_lokal,
a_kraj,
typ_adr,
mpesel,
opesel,
m_dow_typ,
m_dow_nr,
m_dow_wyd,
m_dow_msc,
idm_dow_msc,
m_dow_datar,
m_dow_datam,
m_dow_datad,
m_dow_kraj,
o_dow_typ,
o_dow_nr,
o_dow_wyd,
o_dow_msc,
ido_dow_msc,
o_dow_datar,
o_dow_datam,
o_dow_datad,
o_dow_kraj,
plec,
wa_datar,
wa_datam,
wa_datad,
odp_kto,
odp_kiedy,
mel_kto,
mel_kiedy,
zd_datar,
zd_datam,
zd_datad,
zd_upr_datar,
zd_upr_datam,
zd_upr_datad,
zd_n,
zd_kod,
zd_dok,
zd_org,
zd_org_opis,
zd_msc,
idzd_msc)
SELECT ODPSKR.idusc,
ODPSKR.rk,
ODPSKR.dz,
ODPSKR.nr,
ODPSKR.nrp,
ODPSKR.dzpl,
ODPSKR.nro,
ODPSKR.nazwpl,
ODPSKR.nazw,
ODPSKR.imie1pl,
ODPSKR.imie1,
ODPSKR.imier,
ODPSKR.ur_datar,
ODPSKR.ur_datam,
ODPSKR.ur_datad,
ODPSKR.ur_msc,
ODPSKR.idur_msc,
uscadm.Fnnrstattoteryt(ODPSKR.idur_msc),
ODPSKR.ur_kraj,
ODPSKR.oj_nazw,
ODPSKR.oj_nazwr,
ODPSKR.oj_imie1,
ODPSKR.oj_imier,
ODPSKR.ma_nazw,
ODPSKR.ma_nazwrpl,
ODPSKR.ma_nazwr,
ODPSKR.ma_imie1,
ODPSKR.ma_imier,
:NEW.pesel,
:NEW.a_msc,
:NEW.ida_msc,
uscadm.Fnnrstattoteryt(:NEW.ida_msc),
:NEW.a_msc_kp,
:NEW.a_ulica,
:NEW.a_dom,
:NEW.a_lokal,
:NEW.a_kraj,
:NEW.typ_adr,
:NEW.mpesel,
:NEW.opesel,
:NEW.m_dow_typ,
:NEW.m_dow_nr,
:NEW.m_dow_wyd,
:NEW.m_dow_msc,
:NEW.idm_dow_msc,
:NEW.m_dow_datar,
:NEW.m_dow_datam,
:NEW.m_dow_datad,
:NEW.m_dow_kraj,
:NEW.o_dow_typ,
:NEW.o_dow_nr,
:NEW.o_dow_wyd,
:NEW.o_dow_msc,
:NEW.ido_dow_msc,
:NEW.o_dow_datar,
:NEW.o_dow_datam,
:NEW.o_dow_datad,
:NEW.o_dow_kraj,
ODPSKR.plec,
AKT.wa_datar,
AKT.wa_datam,
AKT.wa_datad,
ODPSKR.idmr_kto,
ODPSKR.idmr_kiedys,
:NEW.idmr_kto,
ODPSKR.idmr_kiedys,
ODPSKR.zd_datar,
ODPSKR.zd_datam,
ODPSKR.zd_datad,
ODPSKR.zd_upr_datar,
ODPSKR.zd_upr_datam,
ODPSKR.zd_upr_datad,
ODPSKR.zd_n,
ODPSKR.zd_kod,
ODPSKR.zd_dok,
ODPSKR.zd_org,
ODPSKR.zd_org_opis,
ODPSKR.zd_msc,
ODPSKR.idzd_msc
FROM uscadm.usc_uodpskr ODPSKR
join uscadm.usc_uakt AKT
ON ODPSKR.rk = AKT.rk
AND ODPSKR.dz = AKT.dz
AND ODPSKR.nr = AKT.nr
AND ODPSKR.nrp = AKT.nrp
AND ODPSKR.idusc = AKT.idusc
WHERE ODPSKR.rk = :NEW.rk
AND ODPSKR.dz = :NEW.dz
AND ODPSKR.nr = :NEW.nr
AND ODPSKR.nrp = :NEW.nrp
AND ODPSKR.idusc = :NEW.idusc;
:NEW.pwel_stat := 2;
END IF;
END umeld_pwel_trig;
Seems like i messed up the syntax real bad. this works.
Edit:
Had some problems with mutable error in trigger fixed it with the updated script above.