如果你是要取出cid不重复的话,不需要用光标如果有id字段 select * from t1 a where exists( select 1 from t1 where id < a.id and cid = a.cid ) 如果没有id字段select identity(int, 1, 1) as id, * into #tmp from t1select * from #tmp a where exists( select 1 from #tmp where id < a.id and cid = a.cid )drop table #tmp
楼上的,看他这句 select top 1 * from t1 where cid=@aa fetch next from cur into @aa其中cid=@aa 而@aa是哪来的, @aa是从t1 的cid来的.所以只需要一句,那就是:select * from t1就足够了.!!!
楼主可以在查询分析器里试一下就知道了, select * from t1 和你上面用的cursor返回的结果有什么不同?唯一的不同就是 select * from t1 把你用cursor返回的n条结果集放在了一个结果集中,而这正是你所需要的.
龟大三色郎! 要是一句我还问什么啊!! 如果Cid有重复的,而我只要取重复的第一个怎么办? select * from t1能行么? 用top 1是因为虽然cid有重复,但是其他字段没有重复 懂没?
select * from t1 as a where 某不会重复的字段 = (select top 1 某不会重复的字段 from t1 where cid=a.cid)
哈哈哈哈,原来你要的是这个目的呀select * from t1 a where not exists (select 1 from t where cid=a.cid and 主键<a.主键) 也是一句就可以了,看来你还不动cursor用在什么地方啊
replace('也是一句就可以了,看来你还不动cursor用在什么地方啊','动','懂')
select * from t1 a where not exists (select 1 from t1 where cid=a.cid and 主键<a.主键)
select * from t1 a
where exists( select 1 from t1 where id < a.id and cid = a.cid )
如果没有id字段select identity(int, 1, 1) as id, * into #tmp from t1select * from #tmp a
where exists( select 1 from #tmp where id < a.id and cid = a.cid )drop table #tmp
select top 1 * from t1 where cid=@aa fetch next from cur into @aa其中cid=@aa
而@aa是哪来的,
@aa是从t1 的cid来的.所以只需要一句,那就是:select * from t1就足够了.!!!
select * from t1
和你上面用的cursor返回的结果有什么不同?唯一的不同就是
select * from t1
把你用cursor返回的n条结果集放在了一个结果集中,而这正是你所需要的.
要是一句我还问什么啊!!
如果Cid有重复的,而我只要取重复的第一个怎么办?
select * from t1能行么?
用top 1是因为虽然cid有重复,但是其他字段没有重复
懂没?
select * from t1 as a where 某不会重复的字段 = (select top 1 某不会重复的字段 from t1 where cid=a.cid)
not exists
(select 1 from t where cid=a.cid and 主键<a.主键)
也是一句就可以了,看来你还不动cursor用在什么地方啊
not exists
(select 1 from t1 where cid=a.cid and 主键<a.主键)