mysql - Number of rows create new temp table -
here cursor
declare @row_id int; declare @customer_id varchar(50); declare @transid varchar(50); declare @timestamp datetime; declare @abcdt [dbo].[abcdatatype] declare @result_table table(row_id int, customer_id varchar(50)); declare cur cursor select * @abcdt open cur fetch next cur @row_id, @customer_id while @@fetch_status = 0 begin set @transid = null; select top(1) @transid=[transaction_id], @timestamp=[time_stamp] [dbo].[abc] [customer_id]=@customer_id order [time_stamp] desc if (@transid null) begin insert @result_table(row_id, customer_id) values(@row_id,@customer_id); end fetch next cur @row_id, @customer_id end close cur deallocate cur select * @result_table;
here want create new @result_table1
if @result_table
count more 5
.
i mean @result_table
contains 5
records if exceeds creat new temp table , so..
or example creat 1 temp table 17 records .. create 4 temp table conatining 5 + 5 + 5 + 2
records.. @ end return temp tables .. please me generalise logic.
this sloppy, gets done asked believe:
------------------------------------- -- code populates temp table #big_temp rows of test data ---------------------------------------- declare @counter int set @counter = 1 create table #big_temp ( row_id int, cust_id char(6) ) while @counter < 23 begin insert #big_temp values(@counter, '123456') select @counter = @counter + 1 end --now see table 22 rows in it. select * #big_temp ------------------------------------------------------ -- divide rows #big_temp #temp1, temp2, etc. necessary ----------------------------------------------------- declare @row_count int declare @tbl_count int declare @i int declare @j int select @row_count = count(*) #big_temp select @tbl_count = 1 + @row_count / 5 select @i = @tbl_count select @j = 0 select @i tbl_count ----create temp tables while @i > 0 begin --select @i i, @j j --create new temp table dynamically exec ('select row_id, cust_id #tbl' + @i + ' #big_temp row_id between ' + @j + '+1 , (' + @tbl_count + '-' + @i + '+1)* 5; select * #tbl' + @i) set @j = @j + 5 set @i = @i - 1 end
if #big_temp divisible 5 (20 rows, 25 rows, etc.) @tbl_count high 1 - can add line check if @row_count % 5 = 0 subtract 1 @tbl_count
Comments
Post a Comment