任何人都有更新记录但只更新了1行

时间:2014-07-17 09:00:43

标签: sql-server

很奇怪!我正在使用sql server 2008,当我在查询编辑器中执行更新时,它只更新该表中的第一行,我尝试了这个命令SET rowcount 0,但仍然只有一行会更新。

之前有人遇到过这种情况,语法很直接

UPDATE table
SET    status_one = 0

enter image description here

1 个答案:

答案 0 :(得分:2)

我看到写得很糟糕的触发器(像这样),例如:

create table User_Profile /* no Tbl, because **why**? */ (
  ID int IDENTITY(1,1) not null,
  tstatus int not null, /* no int, because again, **why** */
)
go
insert into User_Profile (tstatus) values (0)
go 8
create trigger T_User_Profile_U
on User_Profile
instead of update
as
    /* This is a broken trigger, for answering this question

    DO NOT COPY THIS TRIGGER CODE

    if you're trying to write an actual trigger */
declare @ID int
declare @status int
select @ID = ID,@status = tstatus from inserted
    /* The above was broken because despite me knowing to query
    inserted as a table, I've assumed that it contains one row

    whereas, in fact, it may contain 0, 1 or multiple rows.

    I'm not even guaranteed that the @ID and @status values
    will have been retrieved from the same row */

    /* Do important things */

update User_Profile set tstatus = @status where ID = @ID

现在,您的查询:

set rowcount 0
select tstatus from User_Profile

update User_Profile set tstatus = 1

select tstatus from User_Profile

结果:

tstatus
-------
0
0
0
0
0
0
0
0

tstatus
-------
0
0
0
0
0
0
0
1