Hi,
For functionality purpose, i am splitting a table into two.
eg: for table1, i am splitting it as table1_v1 and table2_v2.
i have taken the count of table1 and i am splitting the rows as
count(1)/2.
Now, i have to insert the splitted rows into table1_v1 and table2_v2.
Any idea, how to do this, without using rownum ?
I am asking this question, because, i did as follows :
select rownum=identity(10), t1.* into #TMP_TABLE from table1 t1
select @count1 = @@rowcount
select @tmp_cnt = (@count1/2) + 1
insert into table1_V1 select * from #TMP_TABLE where rownum <
@tmp_cnt
insert into table_V2 select * from #TMP_TABLE where rownum >=
@tmp_cnt
But the problem here is, while compiling, i got the below errors :
Server Message: Number 213, Severity 16
Insert error: column name or number of supplied values does not match
table definition.
Server Message: Number 213, Severity 16
Insert error: column name or number of supplied values does not match
table definition.
Because the table1_V1, table1_v2 are similar to structure of table1,
which doesnt have rownum as column. so this mismatch in columns.
So any idea, how to do this, without using rownum ?
Please help, any suggestion/ideas.
Thanks in Advance.
With Regards,
Raja.
Ian Michael Gumby - 16 Sep 2008 13:28 GMT
> Hi,
>
[quoted text clipped - 38 lines]
> With Regards,
> Raja.
Why use row num?
It sounds like you're taking table A and are splitting the table in to
two tables on a random basis where every other row is put in either B
or C. The end results is that tables B&C contain the same number of
rows +-1 row.
You don't say what language you're using, but the idea would be to use
a cursor and a counter or a boolean to insert in to either table.
(A boolean could be a small int set to 0 to indicate row to be
inserted is inserted in to table B and 1 to insert in to table C. And
then reset the counter to the other value after the insert.) You can
do this in Python, Java, ESQL/C, 4GL without any problems.
If you want to use SQL only I guess you could insert the data in to a
temp table creating a rownum column and then select the rows based on
odd or even row num.
HTH
-G
Paul Watson (Oninit) - 16 Sep 2008 13:43 GMT
> Hi,
>
[quoted text clipped - 38 lines]
> With Regards,
> Raja.
You appear to just splitting a table. Just fragment the table by Round
Robin, and detach one of the fragments, the end result is two tables,
same structure half the rows in each
Cheers
Paul
Adrian_pr - 19 Sep 2008 00:08 GMT
Hi raja, do you like my idea?
fill a temp table with rowids of your table1,
insert into V1 records from table1 that have been linked with even
rowids in temp table,
delete even rowid record from temp table,
insert into V2 records from table1 linked with remaining records in
temp table
drop temp table
its all pure sql, no host variables
:D
and that's all folks !
select rowid as nrec from table1 into temp temptable;
insert into table1_V1 select * from table1 where rowid in (select nrec
from temptable where mod(rowid,2)=0 );
delete from temptable where mod(rowid,2)=0;
insert into table1_V1 select * from table1 where rowid in (select nrec
from temptable );
drop table temptable;
> Hi,
>
[quoted text clipped - 38 lines]
> With Regards,
> Raja.