简体   繁体   中英

How to update a column status in another table from previous query result

I am working in SQL Server 2014. The query that I run results 2 columns, one is ID and another is Ticket number.

Now on another table which is DW-STG there are columns ID , Ticketnumber and cancel_YN .

What I want to do is to update the status of a cancel_YN column to Y for those ticket numbers which were returned by the query that I ran earlier which is returning ID and Ticketnumber .

Below is the query code which outputs ID and Ticketnumber.

select 
    STG2_SBR_PNR_HEADER.HID, DW_SBR_FARES_FAFH.Ticketnumber
from 
    STG2_SBR_PNR_HEADER
join
    DW_SBR_FARES_FAFH on STG2_SBR_PNR_HEADER.HID = DW_SBR_FARES_FAFH.HID

except

select 
    STG2_SBR_FARES_FAFH.HID, STG2_SBR_FARES_FAFH.Ticketnumber 
from 
    STG2_SBR_FARES_FAFH

Sample of data returned by query:

ID     Ticketnumber
-------------------
1      123
2      456

Desired result in DW_STG table:

ID    Ticket    Cancel_YN
---------------------------------------------------
1     123         Y (previously by default it is N)
2     456         Y (previously by default it is N)
3     745         N

If the query you posted gives the result you want, you can use it in your update statement
with the only difference that it must return only the column DW_SBR_FARES_FAFH.Ticketnumber :

update DW-STG
set cancel_YN = 'y'
where Ticketnumber IN (
  select DW_SBR_FARES_FAFH.Ticketnumber
  from STG2_SBR_PNR_HEADER
  JOIN DW_SBR_FARES_FAFH ON STG2_SBR_PNR_HEADER.HID=DW_SBR_FARES_FAFH.HID
  except
  select STG2_SBR_FARES_FAFH.HID,STG2_SBR_FARES_FAFH.Ticketnumber from 
  STG2_SBR_FARES_FAFH
)

I would recommend MERGE . See following sample:

DECLARE @DWSTG TABLE (
    ID int,
    Ticketnumber int,
    cancel_YN char(1)
);

INSERT @DWSTG VALUES
(1,10,'N'),
(2,20,'N'),
(3,30,'N'),
(4,40,'N');

MERGE @DWSTG Dest
USING (
    --here comes your query
    SELECT * FROM (VALUES (2, 20), (3,30)) T(ID, TicketNumber)
) Src
ON Dest.ID=Src.ID AND Dest.TicketNumber = Src.TicketNumber
WHEN MATCHED THEN UPDATE SET cancel_YN = 'Y';

SELECT * FROM @DWSTG;

Result:

ID          Ticketnumber cancel_YN
----------- ------------ ---------
1           10           N
2           20           Y
3           30           Y
4           40           N

You can put that query in a CTE.
Then in the update, join the table to the unique ticket numbers in it.

WITH CTE AS
(
   SELECT fafh.Ticketnumber, fafh.HID
   FROM DW_SBR_FARES_FAFH fafh
   JOIN STG2_SBR_PNR_HEADER head ON head.HID = fafh.HID

   EXCEPT

   SELECT Ticketnumber, HID
   FROM STG2_SBR_FARES_FAFH
)
UPDATE t
SET cancel_YN = 'Y'
FROM DW-STG t
JOIN (SELECT DISTINCT Ticketnumber FROM CTE) q
  ON q.Ticketnumber = t.Ticketnumber;

You can try this using inner join in the following way.

create table firstResult (ID int, Ticketnumber int)
insert into firstResult values (1, 123), (2, 456)

create table secondResult (ID int, Ticketnumber int, Cancel_YN char(1) default 'N')
insert into secondResult (Id, TicketNumber) values(1, 123),(2, 456),(3,745)

update s 
set s.Cancel_YN = 'Y'
from firstResult as f
inner join secondResult s on s.Ticketnumber = f.TicketNumber

select * from secondResult

Here I have assumed firstResult is the table of first query output and secondResult table is for second query output. You can replace both table name with actual query in bracket () .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM