declare @_substoreid varchar(50)='', @_depart varchar(50)='', @_win_name varchar(50)='' --創建游標 declare @cursor cursor --設定游標欲操作的數據集 set @cursor=cursor for select substoreid,depart from cur_user group by substoreid,depart --打開游標 open @cursor --移動游標指向到第一條數據,提取第一條數據存放在變量中 fetch next from @cursor into @_substoreid,@_depart --如果上一次操作成功則繼續循環 while(@@fetch_status=0)begin --操作提出的數據 declare @cursor_new cursor set @cursor_new=cursor for select win_name from win_name open @cursor_new fetch next from @cursor_new into @_win_name while(@@fetch_status=0)begin -- begin tran insert into user_win values('',@_win_name,'Y','',@_substoreid,@_depart) -- if @@Error<>0 -- begin -- print @_win_name+','+@_substoreid+','+@_depart -- ROLLBACK TRANSACTION--事務回滾語句 -- end -- else -- begin -- COMMIT TRANSACTION--事務提交語句 -- end -- -- end fetch next from @cursor_new into @_win_name end close @cursor_new deallocate @cursor_new --繼續提下一行 fetch next from @cursor into @_substoreid,@_depart end --關閉游標 close @cursor --刪除游標 deallocate @cursor
declare @_substoreid varchar(50)='', @_date varchar(50)='2014-08-20',--開始時間 @_endDate varchar(50)='2014-08-22'--getdate()--結束時間(不包含當天) while(DATEDIFF(day,@_date,@_endDate)>0)begin --創建游標 declare @cursor cursor --設定游標欲操作的數據集 set @cursor=cursor for select substoreid from hotelid --打開游標 open @cursor --移動游標指向到第一條數據,提取第一條數據存放在變量中 fetch next from @cursor into @_substoreid --如果上一次操作成功則繼續循環 while(@@fetch_status=0)begin --操作提出的數據 if not exists(select id from receive_report where substoreid=@_substoreid and the_date=@_date and name='房型統計' and id='總房數') begin declare @curroomnums decimal(10,1) select @curroomnums=isnull(all_rooms,-1) from receive_report where substoreid=@_substoreid and the_date=@_date and name='租類' and id='合計' if @curroomnums is not null and @curroomnums>0 begin declare @the_class varchar(20)='', @total decimal(10,1) declare @cursor_new cursor set @cursor_new=cursor for select the_class,count(*) as total from home where substoreid=@_substoreid group by the_class open @cursor_new fetch next from @cursor_new into @the_class,@total while(@@fetch_status=0)begin insert into receive_report(id,substoreid,day_rooms,the_date,name,sort_flag) values(@the_class,@_substoreid,@total,@_date,'房型統計','24') fetch next from @cursor_new into @the_class,@total end close @cursor_new deallocate @cursor_new insert into receive_report(id,substoreid,day_rooms,the_date,name,sort_flag) values('總房數',@_substoreid,@curroomnums,@_date,'房型統計','24') print @_substoreid+' '+@_date end end --繼續提下一行 fetch next from @cursor into @_substoreid end --關閉游標 close @cursor --刪除游標 deallocate @cursor set @_date= convert(varchar(12), DATEADD(day,1,@_date),23) end