Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««12

Computed column Expand / Collapse
Author
Message
Posted Monday, November 8, 2010 11:52 AM
UDP Broadcaster

UDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP Broadcaster

Group: General Forum Members
Last Login: Tuesday, September 2, 2014 11:51 AM
Points: 1,464, Visits: 254
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
Post #1017374
Posted Monday, November 8, 2010 12:14 PM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, January 31, 2013 8:01 AM
Points: 1,228, 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.
Post #1017380
Posted Monday, November 8, 2010 2:17 PM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, August 28, 2014 8:53 PM
Points: 1,388, Visits: 3,039
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)
Post #1017471
Posted Tuesday, November 9, 2010 12:58 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Wednesday, October 29, 2014 1:18 PM
Points: 6,056, Visits: 8,342
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
Post #1017647
Posted Tuesday, November 9, 2010 3:08 AM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Friday, October 24, 2014 3:48 AM
Points: 969, Visits: 692
UMG Developer (11/8/2010)
Nice question, I would have gotten it wrong had the option for the problem with + not been there. (I didn't see the computed column so once I went looking for the + I immediately knew what was going to happen.

Thanks!


I thought the title gave it away

Very topical, I was reading up on persistance last night


Hope this helps,
Rich



Post #1017706
Posted Tuesday, November 9, 2010 5:33 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Tuesday, October 14, 2014 4:43 AM
Points: 1,130, Visits: 1,391
Nice but easy one.

Thanks
Post #1017786
Posted Tuesday, November 9, 2010 10:15 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Friday, September 5, 2014 2:00 PM
Points: 2,160, Visits: 2,191
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.
Post #1018067
Posted Tuesday, November 9, 2010 10:16 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, August 28, 2014 8:53 PM
Points: 1,388, Visits: 3,039
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".
Post #1018068
Posted Tuesday, November 9, 2010 5:29 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Wednesday, October 29, 2014 1:18 PM
Points: 6,056, Visits: 8,342
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
Post #1018325
Posted Wednesday, November 24, 2010 11:46 AM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Yesterday @ 1:58 PM
Points: 17,854, Visits: 15,803
Thanks for the question.



Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server, MVP


SQL RNNR

Posting Performance Based Questions - Gail Shaw
Post #1026104
« Prev Topic | Next Topic »

Add to briefcase ««12

Permissions Expand / Collapse