Solved

t-sql  while loop to create test data

Posted on 2009-03-30
6
464 Views
Last Modified: 2012-08-14
I have database in sql server 2005

I have a populated table called customer ....

tblCustomer
id          int
name    varchar(255)
zip        varchar(255)


I want to add a field to the table above called dummyvalue and update the table so it is appended like

dummyvalue1
dummyvalue2
dummyvalue3

for each populated row.  How can I do this?

and so on





0
Comment
Question by:mugsey
6 Comments
 
LVL 31

Accepted Solution

by:
RiteshShah earned 350 total points
ID: 24019130
have a look
--table

create table tblCustomer

(id          int identity,

name    varchar(255),

zip        varchar(255),

dummyvalue varchar(50)

)
 

--loop

declare @icount int

set @icount =10

while @icount>0

begin

	insert into tblcustomer values('ritesh','07092','DummyValue'+convert(varchar,@icount))

	set @icount=@icount-1

end

Open in new window

0
 
LVL 16

Expert Comment

by:Auric1983
ID: 24019132
for how many records?
0
 
LVL 37

Assisted Solution

by:momi_sabag
momi_sabag earned 100 total points
ID: 24019137
try

with a as (select id, dummyvalue, row_number() over(order by id) as rown
 from tblCustomer)

update a
 set dummyvalue = 'dummyvalue' + cast(rown as varchar(10)
0
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
LVL 18

Assisted Solution

by:mdougan
mdougan earned 50 total points
ID: 24019192
Something like this should work

Declare @cnt int

set @cnt = 1

while (@cnt < 11)
   BEGIN
      UPDATE tblCustomer SET dummyvalue = 'dummyvalue' + CONVERT(varchar(2), @cnt)
        WHERE tblCustomer.dummyvalue IS NULL
            AND tblCustomer.id = (SELECT MIN(id) FROM tblCustomer WHERE dummyvalue IS NULL)

       SET @cnt = @cnt + 1

   END
0
 
LVL 31

Assisted Solution

by:RiteshShah
RiteshShah earned 350 total points
ID: 24019229
my first solution was for inserting data but if you just want to update one field with "dummyvalue", you don't need loop, you could simply do like below query.


--if your id value is 1 than your dummy value will be dummyvalue1. no need to loop and give process burden on server.
 

update tblcustomer set dummyvalue='dummyvalue'+convert(varchar,id)

Open in new window

0
 
LVL 22

Expert Comment

by:dportas
ID: 24020229
In case it helps, there are data generation tools that will do this task for you in a very efficient and easily maintainable way. For example there is a data generator built into Visual Studio Database Edition and also available in the Red Gate SQL toolkit.
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

910 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

21 Experts available now in Live!

Get 1:1 Help Now