Solved

t-sql  while loop to create test data

Posted on 2009-03-30
6
467 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

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…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

776 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