|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Thursday, June 13, 2013 9:02 AM
Points: 1,068,
Visits: 221
|
|
Only one small comment, the answer depends on collation for the database where the commands are issued. Some collations are case-sensitive so only the create statement would work and the rest would fail because the table doesn't exist. Other than that absolutely perfect.
Lon
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Thursday, January 31, 2013 8:01 AM
Points: 1,232,
Visits: 1,046
|
|
Lon-860191 (11/8/2010) Only one small comment, the answer depends on collation for the database where the commands are issued. Some collations are case-sensitive so only the create statement would work and the rest would fail because the table doesn't exist. Other than that absolutely perfect.
Lon
I treat questions here like I do when taking M$ Exams. The DB collation is the SQL server install default unless otherwise specified.
|
|
|
|
|
UDP Broadcaster
      
Group: General Forum Members
Last Login: Monday, June 10, 2013 7:59 PM
Points: 1,491,
Visits: 3,010
|
|
SanjayAttray (11/8/2010)
Create Table #Test (a int, b int, C as a+b ) go insert into #test values (3,4) insert into #test values (1,7) go select * from #test order by 1 go drop table #test go
Isn't it funny that when you say order by 1, sql server assumes 1 as column and gives result order by column 1 but at the same time when you execute
select 1 from #test
you get result as 1
Hmmm. When I run this code to build the table, "Select 1 from #test" returns two rows, each with a "1". It's an implied join, isn't it?
Create Table #Test (a int, b int, C as a+b ) go insert into #test values (3,4) insert into #test values (1,7) go select 1 from #test
go drop table #test
(1 row(s) affected)
(1 row(s) affected)
----------- 1 1
(2 row(s) affected)
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 3:50 PM
Points: 5,289,
Visits: 7,219
|
|
john.arnott (11/8/2010)
Hmmm. When I run this code to build the table, "Select 1 from #test" returns two rows, each with a "1". It's an implied join, isn't it? Create Table #Test (a int, b int, C as a+b ) go insert into #test values (3,4) insert into #test values (1,7) go select 1 from #test
go drop table #test Nope, no join at all. The logic of the result can be foundd by following the query evaluation steps in their logical order: 1. FROM clause - refers to a single table, so intermediate result set is a copy of that table; two rows, with values {(3,4,7),(1,7,8)}. 2. WHERE clause - not present; intermediate result set unchanged. 3. GROUP BY clause - not present; intermediate result set unchanged. 4. HAVING clause - not present; intermediate result set unchanged. 5. SELECT clause - for each row in intermediate result set, built row in final result set. The intermediate result set has two rows, so the final result set will have two rows as well. The SELECT list includes one column, so the final result set will have one column. And since that single column is defined as the constant value 1, the final result set will consist of two rows, and one column; the colunm is unnamed, typed as integer, and filled with the constant value 1 in both rows.
It's basically the same as when you use SELECT a, b, c, 1 FROM #test - except that you now leave out the a, b, and c columns - and when you have no column left that refers to the table, the results suddenly look a lot weirder than usual.
Hugo Kornelis, SQL Server MVP Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
|
|
|
|
|
SSC Eights!
      
Group: General Forum Members
Last Login: Thursday, June 13, 2013 3:50 AM
Points: 969,
Visits: 652
|
|
|
|
|
|
SSC Eights!
      
Group: General Forum Members
Last Login: Wednesday, May 22, 2013 7:11 AM
Points: 877,
Visits: 1,159
|
|
Nice but easy one.
Thanks
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Monday, June 10, 2013 1:20 PM
Points: 2,163,
Visits: 2,149
|
|
RichardDouglas (11/9/2010) I thought the title gave it away
I don't normally pay much attention to the titles, since they sometimes are designed to be misleading. But yeah, that would have given it away too.
|
|
|
|
|
UDP Broadcaster
      
Group: General Forum Members
Last Login: Monday, June 10, 2013 7:59 PM
Points: 1,491,
Visits: 3,010
|
|
Hugo Kornelis (11/9/2010)
john.arnott (11/8/2010) Hmmm. When I run this code to build the table, "Select 1 from #test" returns two rows, each with a "1". It's an implied join, isn't it? . . . . Nope, no join at all. . . . . Thanks, Hugo. That's certainly clear and I did misuse the term "join". But my main intent in posting was to ask Sanjay (or anyone who could explain) why he got a 1 for his result set where I (and presumably you) would get multiple rows returned, each with the constant 1.
He posted that after the two inserts,
...when you execute
select 1 from #test
you get result as 1 Perhaps he meant to say "you get results as multiple rows of 1".
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 3:50 PM
Points: 5,289,
Visits: 7,219
|
|
I interpreted Sanjay's post to refer to the double meaning of 1 - either an ordinal column position when used in ORDER BY, or an integer value when used somewhere else.
But maybe Sanjay will return and add some clarification?
Hugo Kornelis, SQL Server MVP Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
|
|
|
|
|
SSCoach
         
Group: General Forum Members
Last Login: Today @ 10:09 PM
Points: 18,848,
Visits: 12,433
|
|
|
|
|