[英]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.我在 SQL Server 2014 中工作。我运行的查询结果 2 列,一个是 ID,另一个是票号。
Now on another table which is DW-STG
there are columns ID
, Ticketnumber
and cancel_YN
.现在在另一个
DW-STG
表上有列ID
、 Ticketnumber
和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
.我想要做的是将
cancel_YN
列的状态更新为Y
对于那些由我之前运行的查询返回的票号,该查询返回ID
和Ticketnumber
。
Below is the query code which outputs ID and Ticketnumber.下面是输出 ID 和 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: DW_STG 表中的期望结果:
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
:唯一的区别是它必须只返回列
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 .我会推荐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.您可以将该查询放在 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.您可以通过以下方式使用
inner join
来尝试此操作。
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.这里我假设
firstResult
是第一个查询输出的表, secondResult
表是第二个查询输出的表。 You can replace both table name with actual query in bracket ()
.您可以用括号
()
实际查询替换两个表名。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.