作者 主題: [分享]重新編寫序號  (閱讀 2015 次)

0 會員 與 1 訪客 正在閱讀本文。

小徒兒

  • 鑽研的研究生
  • *****
  • 文章數: 622
    • 檢視個人資料
[分享]重新編寫序號
« 於: 2005-09-02 11:46 »
代碼: [選擇]

declare @Dept as varchar(10), @Cnt as int,@resan001 as varchar(10), @resan002 as varchar(4),@resan003 as varchar(10)

select @Dept=''

declare authors_cursor CURSOR
FORWARD_ONLY READ_ONLY
FOR
SELECT resan001,resan002,resan003  from resan order by resan001,resan003
OPEN authors_cursor
FETCH NEXT FROM authors_cursor
into @resan001,
@resan002,
@resan003

WHILE (@@FETCH_STATUS =  0 )
BEGIN

if (@resan001 != @Dept)
BEGIN
Print 'getresan :' + @resan001 +  '     variable Dept :' + @Dept

select @Cnt =1
select @Dept=@resan001
END

update resan set resan002=Right('00'+cast(@Cnt*10 as varchar),4) where resan001=@resan001 and resan002=@resan002 and resan003=@resan003
select @Cnt=@Cnt +1

FETCH NEXT FROM authors_cursor
into @resan001,
@resan002,
@resan003

END
CLOSE authors_cursor
DEALLOCATE authors_cursor