Solved

t-sql  while loop to create test data

Posted on 2009-03-30
6
472 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 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

740 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