游标 @@FETCH_STATUS =0 Fetch语句成功 =-1 Fetch语句失败或不在结果集中 =-2 提取的行不存在
通过@@FETCH_STATUS做循环
create table #temp
(
activityid nvarchar(4),
acttype nvarchar(10)
)
declare my_cursor cursor
for select activityid,acttype
from activity
order by activityid desc
open my_cursor
declare
@id nvarchar(4),
@acttype nvarchar(10)
FETCH next from my_cursor
INTO @id,@acttype
while(@@fetch_status=0)
begin
if(Not Exists(select * from #temp Where activityid=@id))
insert into #temp select @id,@acttype
fetch next from my_cursor into @id,@acttype
end
CLOSE my_cursor
DEALLOCATE my_cursor
select * from #temp
drop table #temp
ps:char存取的值会乱码,nvarchar即可解决