SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 insert dummy values using WHILE loop in SQL SERVER
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

raaj
Posting Yak Master

113 Posts

Posted - 05/14/2012 :  22:07:40  Show Profile  Reply with Quote
Hi Guys,
I have a table named PERSON.
I got this requirement that this table needs to be filled with 300 dummy values. This table has columns 'Person_ID' (which is a Identity (1,1)) column and 'Name' column.
I want the dummy values in this format :
Person_ID Name
1 Person1
2 Person2
3 Person3
4 Person4
5 Person5
..................
..................upto Person300.

Can anyone suggest How can I write a simple query to get the above result set?

I tried the below WHILE Loop :

DECLARE @intLimit AS INT = 300
DECLARE @intCounter AS INT = 1

WHILE @intCounter <= @intLimit
BEGIN
INSERT INTO Person VALUES ('Person1')
SET @intCounter = @intCounter + 1
END

SELECT * FROM Person

But When I run the above query, I am getting the following resultset :
Person_ID Name
1 Person1
2 Person1
3 Person1
4 Person1
5 Person1
.........................
.........................

So, I want the Name column to have rows Person1,Person2,Person3.....Person300.

Any ideas How to do achieve this?

Thanks,
Raaj.

visakh16
Very Important crosS Applying yaK Herder

India
47023 Posts

Posted - 05/14/2012 :  22:53:33  Show Profile  Reply with Quote

;WITH NumberTable (N)
AS
(
SELECT 1
UNION ALL
SELECT N+1
FROM NumberTable
WHERE N+1 <=300
)

INSERT PERSON (Name)
SELECT  'Person' + CAST(N AS varchar(3))
FROM NumberTable


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

vinu.vijayan
Posting Yak Master

India
227 Posts

Posted - 05/15/2012 :  05:35:04  Show Profile  Reply with Quote
quote:
Originally posted by visakh16


;WITH NumberTable (N)
AS
(
SELECT 1
UNION ALL
SELECT N+1
FROM NumberTable
WHERE N+1 <=300
)

INSERT PERSON (Name)
SELECT  'Person' + CAST(N AS varchar(3))
FROM NumberTable


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/





+1.
As Visakh showed it to you, it is best done with a NUMBERS/TALLY TABLE.
To know more about TALLY TABLE, check out the following link:

http://www.sqlservercentral.com/articles/T-SQL/62867/

N 28° 33' 11.93148"
E 77° 14' 33.66384"
Go to Top of Page

sunitabeck
Flowing Fount of Yak Knowledge

5152 Posts

Posted - 05/15/2012 :  07:08:54  Show Profile  Reply with Quote
quote:
Originally posted by visakh16


;WITH NumberTable (N)
AS
(
SELECT 1
UNION ALL
SELECT N+1
FROM NumberTable
WHERE N+1 <=300
)

INSERT PERSON (Name)
SELECT  'Person' + CAST(N AS varchar(3))
FROM NumberTable


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/



An addition to Visakh's query (in RED ).
.....
SELECT  'Person' + CAST(N AS varchar(3))
FROM NumberTable
OPTION (MAXRECURSION 300);
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000