CREATEtable userinfo ( name char(10), sex char(4), phone char(10) ) insert userinfo select'aaa','男','12345'unionall select'bbb','男','12345'unionall select'aaa','男','12345'unionall select'bbb','男','12345'unionall select'aaa','男','12345'unionall select'ccc','男','12345'unionall select'aaa','男','1235' --(1)定义 declare first cursor forselectcount(*) as num ,name from userinfo groupby name --(2)打开 open first --(3)操作 declare@numint,@namevarchar(10) fetchnextfrom first into@num,@name while(@@fetch_status=0) --返回被 FETCH 语句执行的最后游标的状态,而不是任何当前被连接打开的游标的状态。 --=0表示FETCH 语句成功 begin declare second cursorforselect*from userinfo where name=@name open second fetchnextfrom second while(@num>1) begin deletefrom userinfo wherecurrentof second set@num=@num-1 fetchnextfrom second end close second deallocate second --删除游标引用 fetchnextfrom first into@num,@name end close first deallocate first SELECT*FROM USERINFO