0

I need to update a table tb0 based on previous CTE statements. The code is similar to the pseudo-code bellow:

with tb1 as(select tb_index from tb0 where tb_index=10),

tb2 as (select tb_index,count(orders))total from orders inner join tb1 on tb1.tb_index=orders.tb_index)

update tb0 set tb0.status='a'
from tb2
inner join tb0.tb_index=tb2.tb_index
where
tb2.total>30

question is, will the update above work considering the inner join and where clause ?

1 Answers1

-2

Way:-1

WITH TB1 AS(SELECT TB_INDEX FROM TB0 WHERE TB_INDEX=10),
TB2 AS (SELECT TB_INDEX,COUNT(ORDERS))TOTAL FROM ORDERS INNER JOIN TB1 ON TB1.TB_INDEX=ORDERS.TB_INDEX)
UPDATE T SET T.STATUS='A'
FROM TB0 T WHERE EXISTS (SELECT 1 FRM TB2 WHERE T.TB_INDEX=TB2.TB_INDEX
TB2.TOTAL>30)

Way:-2

WITH TB1 AS(SELECT TB_INDEX FROM TB0 WHERE TB_INDEX=10),
TB2 AS (SELECT TB_INDEX,COUNT(ORDERS))TOTAL FROM ORDERS INNER JOIN TB1 ON TB1.TB_INDEX=ORDERS.TB_INDEX)
UPDATE T SET T.STATUS='A'
FROM TB2,TB0 T WHERE T.TB_INDEX=TB2.TB_INDEX
TB2.TOTAL>30