Update statement failing in sql - sql

Below update statement is giving me error, please correct me where I am going wrong
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near i
Msg 102, Level 15, State 1, Line 20
Incorrect syntax near u
Code:
Update [dbo].[ManPlan_Prescriber_Roster] i
set i.territory_no = u.territory_no,
i.key_sales_force = u.key_sales_force,
i.territory_type_id = u.territory_type_id,
i.territory_type_descr = u.territory_type_descr,
i.last_name = u.last_name,
i.first_name = u.first_name,
i.mi = u.mi,
i.territory_orig_assign_dt = u.territory_orig_assign_dt,
i.territory_strt_dt = u.territory_strt_dt,
i.email_address = u.email_address,
i.cell_phone = u.cell_phone,
i.fcl_addr1 = u.fcl_addr1,
i.fcl_city = u.fcl_city,
i.fcl_st = u.fcl_st,
i.fcl_zip = u.fcl_zip
from
[dbo].[ManPlan_Prescriber_Roster] m
join
(select
territory_no,
key_sales_force,
territory_type_id,
territory_type_descr,
last_name,
first_name,
mi,
territory_orig_assign_dt,
territory_strt_dt,
email_address,
cell_phone,
fcl_addr1,
fcl_city,
fcl_st,
fcl_zip
from
[dbo].[RepRosterUpd$]
where
filedt = '2016-05-18') u on m.territory_no = u.territory_no
and m.key_sales_force = u.key_sales_force
and m.territory_type_id = u.territory_type_id
Thanks

If you use the table you want to update in the from clause and give it an alias there (here: m), then you must use that alias in your UPDATE statement, too: UPDATE m SET ..... - you cannot "redefine" the table with another alias at the top of your UPDATE statement
Try this:
Update m // use the alias you've defined on the FROM line!
set m.territory_no = u.territory_no,
m.key_sales_force = u.key_sales_force,
......
m.fcl_zip = u.fcl_zip
from
[dbo].[ManPlan_Prescriber_Roster] m // use this alias in UPDATE
join
(select
territory_no,
key_sales_force,
.....
fcl_zip
from
[dbo].[RepRosterUpd$]
where
filedt = '2016-05-18') u on m.territory_no = u.territory_no
and m.key_sales_force = u.key_sales_force
and m.territory_type_id = u.territory_type_id

Related

MaxDB SAP SQL Missing keyword WITH on update

Hi there I'm using MaxDB and trying to update a table and I get the error Missing keyword:WITH. Code: -5015.
Here is my SQL:
UPDATE agm
SET agm_confirm_stat = 'C',
agm_confirm_usr = 'MEDICWARE',
agm_confirm_obs = 'Wellon: Confirmado',
agm_obs = NVL(agm_obs, '') || '\/\/Wellon: Confirmar',
agm_confirm_dthr = TIMESTAMP
WHERE (agm_pac = '319900')
AND (
agm_obs NOT LIKE '%Wellon: Confirmar%'
OR agm_obs IS NULL
)
AND (agm_str_cod = 'FBC')
AND (agm_hini = '2022-12-07 08:00:00')
AND (agm_confirm_stat != 'C')
AND (agm_stat = 'A')

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

Error "ORA-00936: missing expression" for below SQL statement

Please help to check why below SQL statement got error of "ORA-00936: missing expression" when executed on Oracle 11g R2.
select part.*,
decode(pv.dspuom, null, part.untqty, part.untqty / prtftp_dtl.untqty) dsp_untqty,
decode(pv.dspuom, null, pv.stkuom, pv.dspuom) untqty_uom,
decode(pv.dspuom, null, cast(null as int), pv.stkuom, cast(null as int), mod(part.untqty, prtftp_dtl.untqty)) rem_untqty,
decode(pv.dspuom, null, null, pv.stkuom, null, pv.stkuom) rem_untqty_uom
from (select #select_on:raw sum(untqty) untqty,
sum(catch_qty) catch_qty,
invadj.prtnum fixed_prtnum,
invadj.prt_client_id fixed_prt_client_id,
invadj.wh_id fixed_wh_id,
invadj.play_prc_id
from invadj
where exists(select 'x'
from prtmst_view
where prtmst_view.prtnum = invadj.prtnum
and prtmst_view.prt_client_id = invadj.prt_client_id
and prtmst_view.wh_id = invadj.wh_id
and prtmst_view.prdflg = 1)
and #+moddte:date
and #+invadj.play_prc_id
and #+invadj.prtnum
and #+invadj.prt_client_id
and wh_id = #wh_id
and #*
group by #result_string:raw,
invadj.prtnum,
invadj.prt_client_id,
invadj.wh_id,
invadj.play_prc_id
having sum(untqty) <> 0
or sum(catch_qty) <> 0) part left
join prtmst_view pv
on pv.prtnum = part.fixed_prtnum
and pv.prt_client_id = part.fixed_prt_client_id
and pv.wh_id = part.fixed_wh_id left
join prtftp
on prtftp.prtnum = pv.prtnum
and prtftp.prt_client_id = pv.prt_client_id
and prtftp.wh_id = pv.wh_id
and prtftp.defftp_flg = 1 left
join prtftp_dtl
on prtftp.prtnum = prtftp_dtl.prtnum
and prtftp.ftpcod = prtftp_dtl.ftpcod
and prtftp.prt_client_id = prtftp_dtl.prt_client_id
and prtftp.wh_id = prtftp_dtl.wh_id
and prtftp_dtl.uomcod = nvl(pv.dspuom, pv.stkuom)
I think the syntax may be off a bit.
Look at this line below
FROM (SELECT # select_on:raw sum(untqty) untqty,
The # character seems odd

The multi-part identifier could not be bound

SELECT
TOP (100) PERCENT
dbo.bARCM.CustGroup, dbo.bARCM.Customer,
CASE WHEN udJobType IN ('Scheduled Maintenance', 'Unscheduled Emergency',
'Unscheduled Call Out') THEN 'Maintenance'
WHEN udJobType IN ('Scheduled Special Projects', 'UPS Internal Capital Exp')
THEN 'Capital'
WHEN udJobType LIKE '%Turnaround%' THEN 'T/A'
END AS JobType,
CASE WHEN Factor = 1.0 THEN 'ST'
WHEN Factor = 1.5 THEN 'OT'
WHEN Factor = 2.0 THEN 'OT'
END AS STOT,
SUM(dbo.bJBID.Hours) AS Hours,
DATEADD(MONTH, DATEDIFF(MONTH, 0, dbo.bJBID.JCDate), 0) as SortMonth
FROM
dbo.bJBID
INNER JOIN
dbo.bJBIN ON dbo.bJBID.JBCo = dbo.bJBIN.JBCo AND dbo.bJBID.BillMonth = dbo.bJBIN.BillMonth AND dbo.bJBID.BillNumber = dbo.bJBIN.BillNumber
INNER JOIN
dbo.bARCM
INNER JOIN
dbo.bJCCM ON dbo.bARCM.CustGroup = dbo.bJCCM.CustGroup AND dbo.bARCM.Customer = dbo.bJCCM.Customer
INNER JOIN
dbo.JCJMPM ON dbo.bJCCM.JCCo = dbo.JCJMPM.JCCo AND dbo.bJCCM.Contract = dbo.JCJMPM.Contract ON dbo.bJBIN.JBCo = dbo.JCJMPM.JCCo AND
dbo.bJBIN.Contract = dbo.JCJMPM.Contract
INNER JOIN
dbo.bJCCT ON dbo.bJBID.CostType = dbo.bJCCT.CostType AND dbo.bJBID.PhaseGroup = dbo.bJCCT.PhaseGroup
INNER JOIN
dbo.budAcctMonths ON dbo.budAcctMonths.Month = dbo.bJBIN.BillMonth
WHERE
(dbo.bJCCM.JCCo = 1)
AND (dbo.bJBID.CostType IN (1, 41, 42, 43, 44, 45, 46))
AND (dbo.bJBID.CostTypeCategory = 'L')
AND (dbo.JCJMPM.udPlantLocation LIKE 'Deer%')
AND (dbo.bARCM.Name LIKE 'Dow%' OR dbo.bARCM.Name LIKE 'Rohm%')
GROUP BY
dbo.bARCM.CustGroup, dbo.bARCM.Customer,
dbo.JCJMPM.udJobType, dbo.bJBID.Factor, dbo.SortMonth
HAVING
(dbo.bARCM.CustGroup = 1) AND (SUM(dbo.bJBID.Hours) <> 0)
When I excute this query, I get
The multi-part identifier "dbo.SortMonth" could not be bound
error message. I am new to SQL, need some help.
Your SELECT is assigning a alias of SortMonth to the following DATEADD(MONTH, DATEDIFF(MONTH, 0, dbo.bJBID.JCDate), 0) but you cannot use an alias in GROUP BY unless it was named in a subquery.
You will need to change the code to:
GROUP BY dbo.bARCM.CustGroup,
dbo.bARCM.Customer,
dbo.JCJMPM.udJobType,
dbo.bJBID.Factor,
DATEADD(MONTH, DATEDIFF(MONTH, 0, dbo.bJBID.JCDate), 0) -- use the DATEADD code here not the alias

How to join three tables?

SELECT
PC_SL_ACNO, -- DB ITEM
SLNAME, -- ACCOUNT NAME:
SL_TOTAL_AMOUNT -- TOTAL AMOUNT:
FROM GLAS_PDC_CHEQUES
WHERE PC_COMP_CODE=:parameter.COMP_CODE
AND pc_bank_from = :block02.pb_bank_code
AND pc_due_date between :block01.date_from
AND :block01.date_to
AND nvl(pc_discd,'X') IN(‘X’, 'R')
GROUP BY
pc_comp_code, pc_sl_ldgr_code, pc_sl_acno
ORDER BY pc_sl_acno
ACCOUNT NAME:
BEGIN
SELECT COAD_PTY_FULL_NAME INTO :BLOCK03.SLNAME
FROM GLAS_PTY_ADDRESS,GLAS_SBLGR_MASTERS
WHERE SLMA_COMP_CODE = :PARAMETER.COMP_CODE
AND SLMA_ADDR_ID = COAD_ADDR_ID
AND SLMA_ADDR_TYPE = COAD_ADDR_TYPE
AND SLMA_ACNO = :BLOCK03.PC_SL_ACNO
AND SLMA_COMP_CODE = COAD_COMP_CODE;
EXCEPTION WHEN OTHERS THEN NULL;
END;
TOTAL AMOUNT:
BEGIN
SELECT SUM(PC_AMOUNT) INTO :SL_TOTAL_AMOUNT
FROM GLAS_PDC_CHEQUES
WHERE PC_DUE_DATE BETWEEN :BLOCK01.DATE_FROM AND :BLOCK01.DATE_TO
AND PC_BANK_FROM = :block02.PB_BANK_CODE
AND PC_SL_ACNO = :BLOCK03.PC_SL_ACNO
AND NVL(PC_DISCD,'X') = 'R'
AND PC_COMP_CODE = :PARAMETER.COMP_CODE;
EXCEPTION WHEN OTHERS THEN :block03.SL_TOTAL_AMOUNT := 0;
END;
How can I join the three tables?
You'll have to adjust depending on precisely what criteria and required fields you have for your query or queries.
SELECT
c.PC_SL_ACNO,
a.COAD_PTY_FULL_NAME,
SUM(c.PC_AMOUNT)
FROM
GLAS_PDC_CHEQUES c
LEFT JOIN
GLAS_SBLGR_MASTERS m
ON ( c.PC_SL_ACNO = m.SLMA_ACNO
AND c.PC_COMP_CODE = m.SLMA_COMP_CODE
)
LEFT JOIN
GLAS_PTY_ADDRESS a
ON ( m.SLMA_ADDR_ID = a.COAD_ADDR_ID
AND m.SLMA_COMP_CODE = a.COAD_COMP_CODE
AND m.SLMA_ADDR_TYPE = a.COAD_ADDR_TYPE
)
WHERE
c.PC_COMP_CODE = :PARAMETER.COMP_CODE
AND c.PC_SL_ACNO = :BLOCK03.PC_SL_ACNO
AND c.PC_BANK_FROM = :BLOCK02.PB_BANK_CODE
AND NVL(c.PC_DISCD,'X') IN (‘X’, 'R')
AND c.PC_DUE_DATE BETWEEN :BLOCK01.DATE_FROM AND :BLOCK01.DATE_TO
GROUP BY
c.PC_SL_ACNO, -- not sure which grouping exactly you need.
a.COAD_PTY_FULL_NAME
ORDER BY
c.PC_SL_ACNO
I notice that in the first query you have pc_comp_code as a search criterion, and on the leading edge of your grouping - is that what you need?
This is a bit of an 'estimate' due to the enigmatic nature of your question!