Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

t-sql  while loop to create test data

Posted on 2009-03-30
6
Medium Priority
?
481 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 31

Accepted Solution

by:
RiteshShah earned 1400 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 400 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 18

Assisted Solution

by:mdougan
mdougan earned 200 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 1400 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

604 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