SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


selecting same columns


selecting same columns

Author
Message
balasaukri
balasaukri
Old Hand
Old Hand (339 reputation)Old Hand (339 reputation)Old Hand (339 reputation)Old Hand (339 reputation)Old Hand (339 reputation)Old Hand (339 reputation)Old Hand (339 reputation)Old Hand (339 reputation)

Group: General Forum Members
Points: 339 Visits: 301
Hi All - I have come across a query in a site like below:

if OBJECT_ID('tempdb..#t1') is not null
drop table #t1

SELECT TOP 11000
IDENTITY(INT,1,1) AS N INTO #t1
FROM Master.dbo.SysColumns sc1,
Master.dbo.SysColumns sc2

I have a doubt on the part: FROM Master.dbo.SysColumns sc1, Master.dbo.SysColumns sc2 - How it works? what is the logic behind this?

I tried to select like: select * from Master.dbo.SysColumns sc1, Master.dbo.SysColumns sc2 - seems endless loop. Is it so? Can someone please clarify how it works. Thanks!
J Livingston SQL
J Livingston SQL
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: General Forum Members
Points: 12329 Visits: 37592
this article may help you...(J Moden)

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

________________________________________________________________
you can lead a user to data....but you cannot make them think
and remember....every day is a school day

balasaukri
balasaukri
Old Hand
Old Hand (339 reputation)Old Hand (339 reputation)Old Hand (339 reputation)Old Hand (339 reputation)Old Hand (339 reputation)Old Hand (339 reputation)Old Hand (339 reputation)Old Hand (339 reputation)

Group: General Forum Members
Points: 339 Visits: 301
Yes I got it from this link only. But I unable to understand the concept behind this query. Please clarify.
How it works when the same table name mentioned two times
What would be the output? - seems it runs endlessly...
J Livingston SQL
J Livingston SQL
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: General Forum Members
Points: 12329 Visits: 37592
http://msdn.microsoft.com/en-us/library/ms190690(v=sql.105).aspx

try this



if OBJECT_ID('tempdb..#t') is not null
drop table #t1

SELECT TOP 100000
IDENTITY(INT,1,1) AS N INTO #t
FROM Master.dbo.SysColumns sc1

SELECT count(n) FROM #t

--==================================================

if OBJECT_ID('tempdb..#t1') is not null
drop table #t1

SELECT TOP 100000
IDENTITY(INT,1,1) AS N INTO #t1
FROM Master.dbo.SysColumns sc1,
Master.dbo.SysColumns sc2

SELECT count(n) FROM #t1



do you see the difference in rows returned?

________________________________________________________________
you can lead a user to data....but you cannot make them think
and remember....every day is a school day

balasaukri
balasaukri
Old Hand
Old Hand (339 reputation)Old Hand (339 reputation)Old Hand (339 reputation)Old Hand (339 reputation)Old Hand (339 reputation)Old Hand (339 reputation)Old Hand (339 reputation)Old Hand (339 reputation)

Group: General Forum Members
Points: 339 Visits: 301
Hi Livingston - I understood now! Great Thanks!!!
Sean Lange
Sean Lange
SSC Guru
SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)

Group: General Forum Members
Points: 63442 Visits: 17966
There are some faster ways to generate a tally table. The code you are looking at is selecting data from actual tables and inserting into a temp table. You can instead use some trickery to avoid ANY physical reads.

This code will produce 10,000 records nearly instantly.


WITH
E1(N) AS (select 1 from (values (1),(1),(1),(1),(1),(1),(1),(1),(1),(1))dt(n)),
E2(N) AS (SELECT 1 FROM E1 a, E1 b), --10E+2 or 100 rows
E4(N) AS (SELECT 1 FROM E2 a, E2 b), --10E+4 or 10,000 rows max
cteTally(N) AS
(
SELECT ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) FROM E4
)
select N from cteTally



I have a couple versions of this that I keep around in different formats. One is a view and the other one is an iTVF. They both get used pretty frequently.

Here is code to turn this into a view.


create View [dbo].[cteTally] as

WITH
E1(N) AS (select 1 from (values (1),(1),(1),(1),(1),(1),(1),(1),(1),(1))dt(n)),
E2(N) AS (SELECT 1 FROM E1 a, E1 b), --10E+2 or 100 rows
E4(N) AS (SELECT 1 FROM E2 a, E2 b), --10E+4 or 10,000 rows max
cteTally(N) AS
(
SELECT ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) FROM E4
)
select N from cteTally
GO



OK so this only generates 10,000 rows and yours generates 11,000. No problem, we can still do this amazingly fast.


select ROW_NUMBER() over(order by (select null))
from
(
select * from dbo.cteTally c
union all
select top 1000 * from dbo.cteTally t
) x



This type of thing will help your system because it requires far less resources than pulling then sys.columns and inserting into a temp table. No need to check for the existence or drop any objects.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
balasaukri
balasaukri
Old Hand
Old Hand (339 reputation)Old Hand (339 reputation)Old Hand (339 reputation)Old Hand (339 reputation)Old Hand (339 reputation)Old Hand (339 reputation)Old Hand (339 reputation)Old Hand (339 reputation)

Group: General Forum Members
Points: 339 Visits: 301
Excellent - Thank you Sean!!!
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search