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
 General SQL Server Forums
 New to SQL Server Programming
 LOOP
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

zoe2003
Starting Member

15 Posts

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

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


Tx,
Z

DonAtWork
Flowing Fount of Yak Knowledge

2161 Posts

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


select * 
from 
(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: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Go to Top of Page

zoe2003
Starting Member

15 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

TG
Flowing Fount of Yak Knowledge

USA
6062 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
begin
       while @c2 < 8
       begin
              insert #t (col1, col2)
              values (@c1, @c2)
              set @c2 = @c2+1
       end
       set @c1 = @c1+1
       set @c2 = 5
end

select * from #t
drop table #t


Be One with the Optimizer
TG
Go to Top of Page

zoe2003
Starting Member

15 Posts

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

Kristen
Test

United Kingdom
22403 Posts

Posted - 12/04/2013 :  05:13:05  Show Profile  Reply with Quote
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  
 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.06 seconds. Powered By: Snitz Forums 2000