首页 > 代码库 > SQL循环索引
SQL循环索引
declare @tbl table(order_id varchar(20),dates datetime)insert @tbl(order_id,dates)select ‘A‘,‘2014-1-1‘ unionselect ‘A‘,‘2014-2-1‘ unionselect ‘A‘,‘2014-2-2‘ unionselect ‘B‘,‘2014-1-1‘ unionselect ‘C‘,‘2014-1-1‘ unionselect ‘D‘,‘2014-1-1‘ unionselect ‘D‘,‘2014-1-2‘ unionselect ‘E‘,‘2014-12-1‘ unionselect ‘E‘,‘2014-12-2‘ unionselect ‘E‘,‘2014-12-3‘ declare @index int,@count int,@row int,@item_order varchar(20),@current_order varchar(20)select identity(int,1,1) as id,order_id,dates,cast(0 as int) as row_index into #tbl from @tbl order by order_id,datesselect @index=1,@row=1,@count=@@rowcountwhile @row<=@countbegin --首次循环 if isnull(@item_order,‘‘)=‘‘ select @item_order=order_id from #tbl where id=@row --已经有做循环 else begin select @current_order=order_id from #tbl where id=@row if @current_order<>@item_order begin set @index=1 set @item_order=@current_order end end --更新序列号 update #tbl set row_index=@index where id=@row set @index=@index+1 set @row=@row+1endselect * from #tbldrop table #tbl
SQL循环索引
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。