如何实现用游标实现批处理修改SQL表的一列数据
表有3列,分别为A,B,C.
A列中有许多重复项,例如:a,a,a,a,c,c,c,b,r,f,f,f.....这样
B列没有重复项.
C列是a,a,a,a们的一个序列号1,2,3,4...
A B C
a . 1
a . 2
a . 3
a . 4
c . 1
c . 2
c . 3
b . 1
r . 1
f . 1
f . 2
f . 2
. . .
.
请问如和使用游标修改C列....谢谢达人指教
[解决办法]
declare @a table(A char(1), B varchar(10), C int)
insert @a select 'a ', 'wq ', 1
union all select 'a ', 'ee ',2
union all select 'a ', 'r3 ',3
union all select 'a ', 'sdr ',4
union all select 'c ', 'fas ',1
union all select 'c ', 'fd ',2
union all select 'c ', '2df ',3
union all select 'b ', '2fdsd ',1
union all select 'r ', 'fasd ',1
union all select 'f ', 'gs ',1
union all select 'f ', '2sdf ',2
union all select 'f ', 'hgds ',2
select *,id=identity(int,1,1) into # from @a
select a,b,c=(select count(1) from # where a=a.a and id <=a.id) from # a
drop table #
[解决办法]
在鸟儿大哥基础上,try:
CREATE table #T(A char(1), B varchar(10), C int)
insert #T select 'a ', 'wq ', 1
union all select 'a ', 'ee ',2
union all select 'a ', 'r3 ',3
union all select 'a ', 'sdr ',4
union all select 'c ', 'fas ',1
union all select 'c ', 'fd ',2
union all select 'c ', '2df ',3
union all select 'b ', '2fdsd ',1
union all select 'r ', 'fasd ',1
union all select 'f ', 'gs ',1
union all select 'f ', '2sdf ',2
union all select 'f ', 'hgds ',2
DECLARE @A char(1) ,@C int
SELECT @A= ' ',@C=0
UPDATE #T SET
@C=CASE @A WHEN A THEN @C ELSE 0 END +1
,C=@C
,@A= A
SELECT * FROM #T
DROP TABLE #T