sql server 2005 存储过程

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
drop proc proc_remove_same_stu
go
create proc proc_remove_same_stu as
begin
declare @stuid varchar(500)
declare @stuNum int
declare stuid_cursor cursor for select stuid from student_graduate  group by stuid having count(stuid)>=2
open stuid_cursor
	fetch next from stuid_cursor into @stuid
	while @@fetch_status = 0
	begin
		select @stuNum=count(1) from student_graduate where stuid=@stuid
		if @stuNum>1
		begin
			exec('delete from student_graduate where byid=(select top 1 byid  from student_graduate where stuid='''+@stuid+''')')
		end
		fetch next from stuid_cursor into @stuid
	end
	
	close stuid_cursor
	deallocate stuid_cursor
end
go
exec proc_remove_same_stu
go

猜你喜欢

转载自zwxiaole.iteye.com/blog/1671179