gpt4 book ai didi

sql - 有没有办法同时选择和更新行?

转载 作者:行者123 更新时间:2023-12-01 18:25:11 26 4
gpt4 key购买 nike

我想根据一个简单的标准更新一组行并获取已更改的 PK 列表。我以为我可以做这样的事情,但担心可能的并发问题:

SELECT Id FROM Table1 WHERE AlertDate IS NULL;
UPDATE Table1 SET AlertDate = getutcdate() WHERE AlertDate IS NULL;

如果将其包含在事务中,是否会出现并发问题?或者有更好的方法吗?

最佳答案

考虑查看OUTPUT clause :

USE AdventureWorks2012;  
GO

DECLARE @MyTableVar table(
EmpID int NOT NULL,
OldVacationHours int,
NewVacationHours int,
ModifiedDate datetime);

UPDATE TOP (10) HumanResources.Employee
SET VacationHours = VacationHours * 1.25,
ModifiedDate = GETDATE()
OUTPUT inserted.BusinessEntityID,
deleted.VacationHours,
inserted.VacationHours,
inserted.ModifiedDate
INTO @MyTableVar;

--Display the result set of the table variable.
SELECT EmpID, OldVacationHours, NewVacationHours, ModifiedDate
FROM @MyTableVar;
GO
--Display the result set of the table.
SELECT TOP (10) BusinessEntityID, VacationHours, ModifiedDate
FROM HumanResources.Employee;
GO

关于sql - 有没有办法同时选择和更新行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/497464/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com