Update with CTE does not recognize column names in the set clause - sql

I need to update an existing table with data from another. My CTE is giving me correct result, but when I'm trying to update with the CTE SSMS complains on
Msg 102, Level 15, State 1, Line 13
Incorrect syntax near '.'.
or Invalid column names at the lines below:
set cm.Action.Identifier_fk = ID
set cm.ActionRequestedAction = Action
set cm.Action.apartment_fk = apartment_fk
This is the code:
Use DB;
GO
with CTE (ID,Action,Identifier_fk,apartment_fk) AS
(select a.ID, a.Action, b.Identifier_fk, m.apartment_fk
from Project.AllSent a (nolock)
left outer join cm.Action b (nolock) on a.ID=b.Identifier_fk
left Outer Join csv.Matching m (nolock) on m.Identifier_fk = a.ID
left outer join csv.Apartment p (nolock) on m.apartment_fk=p.apartment_pk
where b.Identifier_fk is NULL)
update cm.Action
set cm.Action.Identifier_fk = ID
set cm.Action.RequestedAction = Action
set cm.Action.apartment_fk = apartment_fk
from CTE c
JOIN Project.AllSent t (nolock) on t.ID=c.ID;

The correct update statement has only one set. Also, you have no cm in the from clause. I'm going to propose getting rid of the CTE:
update b
set Identifier_fk = a.ID,
Action.RequestedAction = a.Action,
apartment_fk = mm.apartment_fk
from Project.AllSent a (nolock) left join
cm.Action b (nolock)
on a.ID = b.Identifier_fk left join
csv.Matching m (nolock)
on m.Identifier_fk = a.ID left join
csv.Apartment p (nolock)
on m.apartment_fk = p.apartment_pk
where b.Identifier_fk is NULL;
I don't think the final join is necessary.

Related

Big Query : LEFT OUTER JOIN cannot be used without a condition that is an equality of fields from both sides of the join

I am writing an equivalent logic in BQ as in my source system. In source SQL server side it is working fine. But in Big query it is failing with the OR condition in the last left outer join condition. If I am moving the OR condition in the where clause it is giving wrong count. Need help to fix this issue. How can I re write the below query ?
SELECT count(*)
FROM stprof PRO
INNER JOIN stdim DIM
ON (DIM.diSet = PRO.diSet)
INNER JOIN DQConfig CFG
ON (CFG.ConSet = PRO.ConSet)
LEFT OUTER JOIN AgSt CCT
ON (CCT.StSet = PRO.StSet)
INNER JOIN stprof SummPRO
ON (SummPRO.diSet = DIM.SummdiSet AND
SummPRO.dIntervalStart = PRO.dIntervalStart AND
SummPRO.SiteId = PRO.SiteId AND
SummPRO.nDuration = PRO.nDuration)
LEFT OUTER JOIN AgSt SummCCT
ON (SummCCT.StSet = SummPRO.StSet)
LEFT OUTER JOIN AgentStatus SummSTS
ON (
SummSTS.StSet = SummPRO.StSet
OR
SummSTS.StSet = PRO.StSet)
WHERE DIM.cType = 'A'
You can replace LEFT JOIN with CROSS JOIN and move condition from ON clause to WHERE clause as in below example
#standardSQL
SELECT COUNT(*)
FROM stprof PRO
INNER JOIN stdim DIM
ON DIM.diSet = PRO.diSet
INNER JOIN DQConfig CFG
ON CFG.ConSet = PRO.ConSet
LEFT OUTER JOIN AgSt CCT
ON CCT.StSet = PRO.StSet
INNER JOIN stprof SummPRO
ON SummPRO.diSet = DIM.SummdiSet
AND SummPRO.dIntervalStart = PRO.dIntervalStart
AND SummPRO.SiteId = PRO.SiteId
AND SummPRO.nDuration = PRO.nDuration
LEFT OUTER JOIN AgSt SummCCT
ON SummCCT.StSet = SummPRO.StSet
CROSS JOIN AgentStatus SummSTS
WHERE DIM.cType = 'A'
AND (
SummSTS.StSet = SummPRO.StSet
OR SummSTS.StSet = PRO.StSet
)

update with multiple inner joins in db2

I have the following query
UPDATE mainList
INNER JOIN custToList D
ON l.custToList_id = d.custToList_id
INNER JOIN dealert dt
ON d.dealert_id = dt.dealert_id
INNER JOIN markList m
on m.markList_id = d.markList_id
SET EXPIRATION_TIMESTAMP = CURRENT_TIMESTAMP
where dt.custNum = 2;
BUt it's telling me the first 'inner' is not valid? Can you not do inner joins on updates in db2?
I don't think DB2 supports JOIN in UPDATE. In your case, this is easily fixed by moving the filtering to the WHERE clause:
UPDATE mainList l
SET EXPIRATION_TIMESTAMP = CURRENT_TIMESTAMP
WHERE EXISTS (SELECT 1
FROM custToList D INNER JOIN
dealert dt
ON d.dealert_id = dt.dealert_id INNER JOIN
markList m
ON m.markList_id = d.markList_id
WHERE l.custToList_id = d.custToList_id AND dt.custNum = 2
);

ORA-01427: single-row subquery returns more than one row - issue

update dsalesinvoicehdr set paymentterms = ( select pt.termcode from dsalesinvoicehdr dsh
inner join mg_subledger mp on mp.mg_subledgerid = dsh.customer
inner join mg_partytbill mb on mb.bsubledger = mp.subledgercode
left join payterm pt on pt.paytermid = mb.paymentterm)
ORA-01427: single-row subquery returns more than one row
how to resolve this issue. pls help
Will cause this error because your subquery returns multiple pieces of information Your subquery return multi-row data
You can use UPDATE and JOIN
update
(
select pt.termcode,dsh.paymentterms
from dsalesinvoicehdr dsh
inner join mg_subledger mp on mp.mg_subledgerid = dsh.customer
inner join mg_partytbill mb on mb.bsubledger = mp.subledgercode
left join payterm pt on pt.paytermid = mb.paymentterm
) t
set t.termcode= t.paymentterms
Use update and join mate!!
update
(
select pt.termcode,dsh.paymentterms
from dsalesinvoicehdr dsh
inner join mg_subledger mp on mp.mg_subledgerid = dsh.customer
inner join mg_partytbill mb on mb.bsubledger = mp.subledgercode
left join payterm pt on pt.paytermid = mb.paymentterm
) tab
set tab.termcode= tab.paymentterms
You need to select one of the values. To common methods are an aggregation function or where rownum = 1:
update dsalesinvoicehdr
set paymentterms = (select pt.termcode
from dsalesinvoicehdr dsh inner join
mg_subledger mp
on mp.mg_subledgerid = dsh.customer inner join
mg_partytbill mb
on mb.bsubledger = mp.subledgercode join
payterm pt
on pt.paytermid = mb.paymentterm
where rownum = 1
);
You don't need the left join in the subquery. Non-matches will result in NULL anyway.

Correct Syntax When Limiting Results in Sybase SQL Statement Query

I am trying to put a limit on my Sybase SELECT statement query, but I keep getting syntax errors. I've tried using both limit and SELECT * TOP 10, but neither seems to work. This is my SELECT statement code:
SELECT top 10 *
// column params
FROM claims c
LEFT OUTER JOIN claims_transaction as ct
ON ct.claim_id = c.id
LEFT OUTER JOIN claims_batch_listings cb
ON cb.batch_listing = c.batchl
LEFT OUTER JOIN notes_details d
ON d.id_number = c.notes_detail_id
LEFT OUTER JOIN individual_ins_xref px
ON px.pt_id = c.ind_id
AND px.ins_id = c.ins_id
LEFT OUTER JOIN individuals ind
ON ind.id_number = c.ind_id
LEFT OUTER JOIN sections sec
ON ind.sec_id = sec.id_number
LEFT OUTER JOIN contract_items cont
ON c.contract_id = cont.contract_id
WHERE ( d.date_of_visit >= px.coverage_start AND d.date_of_visit <= px.coverage_end )
AND visit_type <> 'No Visit'
ORDER BY c.datetimecreated;
The * is wrong. Just lose it and you should be OK:
SELECT top 10 -- * removed here
c.claim_problem as problem,
-- etc.

SQL Left Join Syntax Error

Can someone please help resolve an issue, I am left joining two CTE's?
Msg 102, Level 15, State 1, Line 19
Incorrect syntax near '('.
Msg 156, Level 15, State 1, Line 26
Incorrect syntax near the keyword 'left'.
Query:
with Cte_LatestPatInfo as
(select max(a.LogID) as LastPatRec, p.unitnum, p.PatNum, a.iplan from rrscsql2.arhighdollar.dbo.tblarinfoHistory a
inner join rrscsql2.arhighdollar.dbo.tblPatInfoHistory p
on a.patID=p.patID and a.LogID=p.LogID
where a.Active = 1
and a.ReasonCode is not null
group by p.unitnum, p.PatNum, a.iplan)
select *,rc.Description from rrscsql2.arhighdollar.dbo.tblPatInfoHistory p
inner join cte_LatestPatInfo li
on p.PatNum=li.PatNum and p.UnitNum=li.UnitNum and p.LogID = li.LastPatRec
inner join rrscsql2.arhighdollar.dbo.tblarinfoHistory ah
on ah.LogID=p.LogID and ah.iplan = li.iplan and p.PatID=ah.PatID
left join rrscsql2.ARHighDollar.dbo.tblReasonCodes rc
on ah.ReasonCode=rc.ReasonCode,
cte_EOMDenials as
(select d.* from rrscsql2.Denials.dbo.tblDenialMonthEnd d
Inner join rrscsql3.Facilities.dbo.vwFacilities f
on d.UnitNum = f.UnitNum and f.Owner like '%LifePoint%'
Inner join rrscsql2.Denials.dbo.tblDispositionDictionary t
on d.disposition=t.disposition and DispositionType like'O%'
Where datediff(mm, monthending, GETDATE()) = 1
and DATEDIFF(mm,DischDate,monthending)>2)
left join cte_EOMDenials d
on p.unitnum=d.unitnum and p.patnum = d.patnum and p.insplan=d.iplan
You have to introduce all of your CTEs first via a single WITH, and then you can use them in a final query:
with Cte_LatestPatInfo as
(select max(a.LogID) as LastPatRec, p.unitnum, p.PatNum, a.iplan from
rrscsql2.arhighdollar.dbo.tblarinfoHistory a
inner join rrscsql2.arhighdollar.dbo.tblPatInfoHistory p
on a.patID=p.patID and a.LogID=p.LogID
where a.Active = 1
and a.ReasonCode is not null
group by p.unitnum, p.PatNum, a.iplan),
cte_EOMDenials as
(select d.* from rrscsql2.Denials.dbo.tblDenialMonthEnd d
Inner join rrscsql3.Facilities.dbo.vwFacilities f
on d.UnitNum = f.UnitNum and f.Owner like '%LifePoint%'
Inner join rrscsql2.Denials.dbo.tblDispositionDictionary t
on d.disposition=t.disposition and DispositionType like'O%'
Where datediff(mm, monthending, GETDATE()) = 1
and DATEDIFF(mm,DischDate,monthending)>2)
select *,rc.Description from rrscsql2.arhighdollar.dbo.tblPatInfoHistory p
inner join cte_LatestPatInfo li
on p.PatNum=li.PatNum and p.UnitNum=li.UnitNum and p.LogID = li.LastPatRec
inner join rrscsql2.arhighdollar.dbo.tblarinfoHistory ah
on ah.LogID=p.LogID and ah.iplan = li.iplan and p.PatID=ah.PatID
left join rrscsql2.ARHighDollar.dbo.tblReasonCodes rc
on ah.ReasonCode=rc.ReasonCode
left join cte_EOMDenials d
on p.unitnum=d.unitnum and p.patnum = d.patnum and p.insplan=d.iplan
The statement does not seem to join two CTEs. If you want to join two ctes you can achieve it like the following example
;WITH CTE_1
as
( select field1, field2... from sometable),
CTE_2
as
(select field1, field2... from sometable),
select a.field1, a.field2, b.field1, b.field2
from CTE_1 a left join CTE_2 b on a.field1 = b.field1;
You will notice that the two ctes were defined first, separated by comma(,) then use in the select statement.
Please see http://msdn.microsoft.com/en-us/library/ms175972(v=sql.90).aspx for reference.