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


Computed column


Computed column

Author
Message
Lon-860191
Lon-860191
SSCommitted
SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)

Group: General Forum Members
Points: 1985 Visits: 278
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
SanDroid
SanDroid
SSCrazy
SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)

Group: General Forum Members
Points: 2404 Visits: 1046
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.
john.arnott
john.arnott
Hall of Fame
Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)

Group: General Forum Members
Points: 3566 Visits: 3059
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)

Hugo Kornelis
Hugo Kornelis
SSCoach
SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)

Group: General Forum Members
Points: 18681 Visits: 12426
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
RichardDouglas
RichardDouglas
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1323 Visits: 707
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



Hardy21
Hardy21
SSCrazy
SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)

Group: General Forum Members
Points: 2694 Visits: 1399
Nice but easy one.

Thanks
UMG Developer
UMG Developer
SSCarpal Tunnel
SSCarpal Tunnel (4K reputation)SSCarpal Tunnel (4K reputation)SSCarpal Tunnel (4K reputation)SSCarpal Tunnel (4K reputation)SSCarpal Tunnel (4K reputation)SSCarpal Tunnel (4K reputation)SSCarpal Tunnel (4K reputation)SSCarpal Tunnel (4K reputation)

Group: General Forum Members
Points: 4006 Visits: 2204
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.
john.arnott
john.arnott
Hall of Fame
Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)

Group: General Forum Members
Points: 3566 Visits: 3059
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".
Hugo Kornelis
Hugo Kornelis
SSCoach
SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)

Group: General Forum Members
Points: 18681 Visits: 12426
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
SQLRNNR
SQLRNNR
SSC Guru
SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)

Group: General Forum Members
Points: 65989 Visits: 18570
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

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