我一直在查看其他条目,看起来我需要一个游标?我比较新,但我用这个很高兴。欢迎任何建议或帮助。
UPDATE top (1) dbo.table
SET [Status] = 1
WHERE [OrderId] = '1337' and [Status] = 0;
GO
如果记录的OrderID为1337且状态为0,我希望将其更改为1,但这必须一次完成一个。
编辑:我想循环直到没有[状态] = 0
答案 0 :(得分:10)
WHILE 1=1
BEGIN
UPDATE top (1) dbo.table
SET [Status] = 1
WHERE [OrderId] = '1337' and [Status] = 0
IF @@ROWCOUNT = 0
BREAK
END
答案 1 :(得分:2)
我也会把我的答案扔进去。我在生产系统中使用它,我需要小批量更新。如果您想分批批量处理,可以将select top 100 PKid
更改为top 1
。此方法允许您将批量更新扩展到合理的位置,在最小锁定和基于集合的更新之间找到良好的折衷方案。显然,这会增加一个基于集合的语句的开销,但是OP问。
注意:我猜测桌子上有一个PK字段,我只是给它一个假定的名字PKid
declare @done bit = 0x0;
declare @inputs table (PKid int primary key)
while @done = 0x0
begin
-- clear the temp table variable
delete from @inputs
-- build the small batch up updates into table variable
insert into @inputs (PKid)
select top 100 PKid from dbo.table where [Status] = 0 and OrderId = '1337'
-- if we inserted zero records, set our @done bit to 'true' so the while loop breaks
if @@rowcount = 0
begin
select @done = 0x1
end
-- make the update to the real table, constrained by the temp table variable
update t
set t.[Status] = 1
from dbo.table as t
join @inputs as i
on i.PKid = t.PKid
end
答案 2 :(得分:0)
如果您只想使用此句子更新一条记录,建议您执行以下操作:
UPDATE dbo.table
SET [Status] = 1
WHERE rowId in (Select top(1) rowId from dbo.Table where [OrderId] = '1337' and Status = 0);
我不知道您是否正在使用Transact或PL,因此您可能需要通过ROW_NUMBER更改rowId。您也可以通过唯一标识符更改rowId。 Where子句将匹配“select”子句返回的唯一行。
你可以使用光标进行更新(就像其他答案一样),但如果这只是一次,这是一个快速的解决方案。
答案 3 :(得分:-1)
请避免使用while或游标循环来执行此操作。
这是一个更新“顶部”........解决方法:
http://granadacoder.wordpress.com/2009/07/06/update-top-n-order-by-example/
/* START TSQL */
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Television]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
DROP TABLE [dbo].[Television]
END
GO
CREATE TABLE [dbo].[Television] (
TelevisionUUID [uniqueidentifier] not null default NEWSEQUENTIALID() ,
TelevisionName varchar(64) not null ,
TelevisionKey int not null ,
IsCheckedOut bit default 0
)
GO
ALTER TABLE dbo.Television ADD CONSTRAINT PK_Television_TelevisionUUID
PRIMARY KEY CLUSTERED (TelevisionUUID)
GO
ALTER TABLE dbo.Television ADD CONSTRAINT CK_Television_TelevisionName_UNIQUE
UNIQUE (TelevisionName)
GO
set nocount on
declare @counter int
select @counter = 11000
declare @currentTVName varchar(24)
declare @TopSize int
select @TopSize = 10
while @counter > 10000 /* this loop counter is ONLY here for fake data,….do not use this syntax for production code */
begin
select @currentTVName = 'TV: '+ convert(varchar(24) , @counter)
INSERT into dbo.Television ( TelevisionName , TelevisionKey ) values ( @currentTVName , @counter)
select @counter = @counter - 1
end
select count(*) as TV_Total_COUNT from dbo.Television
/*
–Does not Work!
Update TOP (10) dbo.Television
Set IsCheckedOut = 1
FROM
dbo.Television tv
ORDER BY tv.TelevisionKey
*/
declare @AuditTrail table ( TelevisionUUID uniqueidentifier , OldIsCheckedOut bit , NewIsCheckedOut bit )
;
WITH cte1 AS
( SELECT
TOP (@TopSize)
TelevisionUUID , /* <<Note, the columns here must be available to the output */
IsCheckedOut
FROM
dbo.Television tv
WITH ( UPDLOCK, READPAST , ROWLOCK ) /* <<Optional Hints, but helps with concurrency issues */
WHERE
IsCheckedOut = 0
ORDER BY
tv.TelevisionKey DESC
)
UPDATE cte1
SET IsCheckedOut = 1
output inserted.TelevisionUUID , deleted.IsCheckedOut , inserted.IsCheckedOut into @AuditTrail ( TelevisionUUID , OldIsCheckedOut , NewIsCheckedOut )
;
print ''
print 'Newly Checked Out Items'
select * from dbo.Television tv where tv.IsCheckedOut <> 0
print 'Output AuditTrail'
select * from @AuditTrail
print 'Not checked out items'
select count(*) as TVCOUNTIsNOTCheckedOut from dbo.Television tv where tv.IsCheckedOut = 0