Solved

t-sql  while loop to create test data

Posted on 2009-03-30
6
463 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
Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
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, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

758 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