C# 返回由UPDATE TOP 1修改的记录
我有一个C#应用程序,希望返回由TSQL UPDATE TOP 1更新的记录 不做第二次查询。这可能吗 您可以使用该子句C# 返回由UPDATE TOP 1修改的记录,c#,.net,tsql,C#,.net,Tsql,我有一个C#应用程序,希望返回由TSQL UPDATE TOP 1更新的记录 不做第二次查询。这可能吗 您可以使用该子句 您可以使用输出,例如: DECLARE @tmp TABLE (Id int not null) UPDATE TOP (1) [YourTable] SET [YourColumn] = newValue OUTPUT inserted.Id INTO @tmp SELECT * FROM @tmp (添加更多列以适应) 注意,在一般情况下,有必要将插入,以避免触发器出
您可以使用
输出
,例如:
DECLARE @tmp TABLE (Id int not null)
UPDATE TOP (1) [YourTable]
SET [YourColumn] = newValue
OUTPUT inserted.Id INTO @tmp
SELECT * FROM @tmp
(添加更多列以适应)
注意,在一般情况下,有必要将插入,以避免触发器出现问题;否则,通常会看到:
如果DML语句的目标表“YourTable”包含OUTPUT子句而不包含INTO子句,则该语句不能具有任何已启用的触发器
对。这是可能的
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 Server 2005或更高版本)
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