Please start any new threads on our new site at We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Starting Member

17 Posts

Posted - 12/03/2013 :  11:32:30  Show Profile  Reply with Quote

Ho can I generate the following result using nested loop and variables :

col1 col2
1 5
1 6
1 7
2 5
2 6
2 7
3 5
3 6
3 7
4 5
4 6
4 7


Flowing Fount of Yak Knowledge

2167 Posts

Posted - 12/03/2013 :  11:41:22  Show Profile  Reply with Quote
You would not use a loop.

select * 
(select 1 as col1 union select 2 union select 3 union select 4) a
cross apply
(select 5 as col2 union select 6 union select 7) b
order by
	a.col1, b.col2 

How to ask:

Go to Top of Page

Starting Member

17 Posts

Posted - 12/03/2013 :  14:33:39  Show Profile  Reply with Quote
Thanks for the reply.
Actually I need a loop because it will be a 17M records table.
Go to Top of Page

Flowing Fount of Yak Knowledge

6065 Posts

Posted - 12/03/2013 :  14:47:47  Show Profile  Reply with Quote
17M rows is exactly why you shouldn't use a loop. When you're ready to step out of the world of procedural programming and into set-based programming (meaning: when you're ready to vastly improve your sql programming skill) tell us what you're really trying to achieve and we can help.

declare @c1 bigint
declare @c2 bigint
create table #t (col1 bigint, col2 bigint)

set @c1 = 1
set @c2 = 5

while @c1 < 5
       while @c2 < 8
              insert #t (col1, col2)
              values (@c1, @c2)
              set @c2 = @c2+1
       set @c1 = @c1+1
       set @c2 = 5

select * from #t
drop table #t

Be One with the Optimizer
Go to Top of Page

Starting Member

17 Posts

Posted - 12/04/2013 :  04:26:04  Show Profile  Reply with Quote
Thanks, it's great !
Go to Top of Page


United Kingdom
22859 Posts

Posted - 12/04/2013 :  05:13:05  Show Profile  Reply with Quote
Originally posted by zoe2003

Thanks, it's great !

No it isn't! Its utter rubbish - just like TG told you it would be, it will perform like a dog and, on 17M records, may well bring your server to its knees, or run it out of disk space extending the Log File (which, if you have a DBA, may well land you in deep doo-doo, and if you don't may well crash some other business critical database on that server).

This is the key point in TG's message:

"tell us what you're really trying to achieve and we can help"
Go to Top of Page
  Previous Topic Topic Next 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.03 seconds. Powered By: Snitz Forums 2000