November 19, 2010 at 8:25 am
HI All!
This should be a simple one, but I'm having issues coming up with a solution.
I have the following dates Jan 2008-Dec 2012
(i.e. 200801
200802,
etc.)
stored into a table. When I run my query, I simply have 48 rows ( one row for each month). How can I get the 48 rows to repeat, say 100 times?
Thanks!!!:-)
November 19, 2010 at 8:27 am
Your Query
GO 100 🙂
--------------------------------------------------------------------------------------------------
I am just an another naive wannabe DBA trying to learn SQL Server
November 19, 2010 at 8:32 am
HA! That was simple enough.
Is there a way to make it all one result set, rather than 100 sets. I really need it at least 600 times and don't want to copy and paste 600+ times.:-P
November 19, 2010 at 8:55 am
Not strictly T-SQL but its a useful feature.
November 19, 2010 at 8:58 am
Cross join to a table with the number of rows equal to the number of repetitions you need. Numbers tables are useful for that.
- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread
"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
November 19, 2010 at 9:24 am
Sorry, can you elaborate a bit more. I'm still not having any luck.
November 19, 2010 at 9:29 am
Nevermind, I got it! I'm a little "Slow" on Friday's. Thanks for the help!:hehe:
November 21, 2010 at 7:03 pm
clarks (11/19/2010)
Nevermind, I got it! I'm a little "Slow" on Friday's. Thanks for the help!:hehe:
That's cool. Would you mind posting your final code so others may learn how to do it? Thanks?
--Jeff Moden
Change is inevitable... Change for the better is not.
November 22, 2010 at 10:54 am
Here is one way of doing it:
DROP TABLE #Temp
DECLARE @X int
SET @X = 1
CREATE TABLE #Temp (Phone varchar(17) null)
WHILE @X < 101
BEGIN
INSERT INTO #Temp(Phone) select Phone from People where Phone = '123-4567'
SET @X = @X + 1
END
select peop.* from People peop
LEFT JOIN #Temp on #Temp.Phone = peop.Phone
where Phone = '123-4567'
--DROP TABLE #Temp (don't forget to clean up)
November 22, 2010 at 10:56 am
churlbut (11/22/2010)
Here is one way of doing it:DROP TABLE #Temp
DECLARE @X int
SET @X = 1
CREATE TABLE #Temp (Phone varchar(17) null)
WHILE @X < 101
BEGIN
INSERT INTO #Temp(Phone) select Phone from People where Phone = '123-4567'
SET @X = @X + 1
END
select peop.* from People peop
LEFT JOIN #Temp on #Temp.Phone = peop.Phone
where Phone = '123-4567'
--DROP TABLE #Temp (don't forget to clean up)
That's very procedural. Why not do it faster, and more efficient:
select Phone
from People
cross join (select top 100 from sys.columns) as Multiplier;
- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread
"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
November 22, 2010 at 2:00 pm
GSquared (11/22/2010)
churlbut (11/22/2010)
Here is one way of doing it:DROP TABLE #Temp
DECLARE @X int
SET @X = 1
CREATE TABLE #Temp (Phone varchar(17) null)
WHILE @X < 101
BEGIN
INSERT INTO #Temp(Phone) select Phone from People where Phone = '123-4567'
SET @X = @X + 1
END
select peop.* from People peop
LEFT JOIN #Temp on #Temp.Phone = peop.Phone
where Phone = '123-4567'
--DROP TABLE #Temp (don't forget to clean up)
That's very procedural. Why not do it faster, and more efficient:
select Phone
from People
cross join (select top 100 from sys.columns) as Multiplier;
🙂
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 11 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic. Login to reply