34,590
社区成员
发帖
与我相关
我的任务
分享
create table ta(empNo varchar(8) ,empName varchar(20),nIndex int)
insert into ta
select 'A0001','張三',null
union
select 'A0002','李四',null
union
select 'A0003','王二',null
union
select 'A0004','趑六',null
declare @tmpIndex int
set @tmpIndex=1
update ta set nIndex=@tmpIndex,@tmpIndex=@tmpIndex+1
select * from ta
empNo empName nIndex
-------- -------------------- -----------
A0001 張三 2
A0002 李四 3
A0003 王二 4
A0004 趑六 5
(4 row(s) affected)
--我的本意是要如下的結果
empNo empName nIndex
-------- -------------------- -----------
A0001 張三 1
A0002 李四 2
A0003 王二 3
A0004 趑六 4
(4 row(s) affected)
declare @tmpIndex int
set @tmpIndex=0
update ta set @tmpIndex=@tmpIndex+1,nIndex=@tmpIndex
这样更好理解if OBJECT_ID('ta') is not null drop table ta
create table ta(empNo varchar(8) ,empName varchar(20),nIndex int) --drop table ta
insert into ta
select 'A0001','張三',null
union
select 'A0002','李四',null
union
select 'A0003','王二',null
union
select 'A0004','趑六',null
declare @tmpIndex int
set @tmpIndex=0
update ta set nIndex=@tmpIndex,@tmpIndex=@tmpIndex+1
select * from ta
/*
empNo empName nIndex
A0001 張三 1
A0002 李四 2
A0003 王二 3
A0004 趑六 4
*/
爲什麽不是
empNo empName nIndex
-------- -------------------- -----------
A0001 張三 2
A0002 李四 2
A0003 王二 2
A0004 趑六 2
結果,語句中沒有循環語句呀
declare
@tmpIndex int
@s int
set s = select count(*) from ta
set @tmpIndex=1
while(@tmpIndex<@s)
begin
update ta set nIndex=@tmpIndex
set @tmpIndex = @tmpIndex +1
end