Solved

Duplicate rows in Datatable

Posted on 2008-06-21
3
1,996 Views
Last Modified: 2008-06-23
I hava a datatable with 10 rows.  I need to duplicate those rows. How is best to do this?
(Then I will add another column to identify each row. ).
0
Comment
Question by:johnkainn
3 Comments
 
LVL 2

Expert Comment

by:Deepika_Rastogi
ID: 21837252
U want to do that through code or your database query
0
 

Author Comment

by:johnkainn
ID: 21837351
I need actually to duplicate 5 times. I need also to create a new column so that each duplicate has its own number in that column, ranging from 1 to 5.
Originally I planned to do this in code, but maybe it is better to do this in SPROC. What do you think?
 Can you help me with that or should I post the question again for the sql group in this forum?
0
 
LVL 11

Accepted Solution

by:
CMYScott earned 125 total points
ID: 21839365
the SPROC would be like this...  (you'll need to add the CREATE PROCEDURE heading etc)

select * into #temp from yourtable
alter table yourtable add idx int
go
update yourtable set idx = 1
insert into yourtable select #temp.*,2 from #temp
insert into yourtable select #temp.*,3 from #temp
insert into yourtable select #temp.*,4 from #temp
insert into yourtable select #temp.*,5 from #temp
drop table #temp
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video discusses moving either the default database or any database to a new volume.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now