tags:

views:

478

answers:

6

See for yourself:

create table #temp ([ ] varchar(1))<br />
insert into #temp values ('1')<br />
select [ ] from #temp<br />

What in the world is the rationale for allowing this?

+7  A: 

I use it all the time as a placeholder/seperator when debugging complex queries. I might have something like this:

SELECT a.*, ' ' as [ ], b.*
FROM a
LEFT JOIN b on ...

This way I get a blank section in between the two tables so I can easily see in the results where one stops and the other starts.

Later on when I get the results and performance I need I'll change the select clause to only use the columns I care about.

That said, I suppose there's no reason I couldn't use something else for the column name.

Joel Coehoorn
+1 cool little trick for an annoyance of mine thanks
JoshBerke
+7  A: 

I think the rationale is more along the lines of:

Is it worth preventing this functionality?

I don't know how SQL is coded internally but I would suspect it would take more effort to prevent this then to allow it.

JoshBerke
now this is nice :) I like your justification +1
Peter Perháč
Fair enough. You're probably right about it taking more effort to prevent it.
Michael Todd
+2  A: 

I guess a space is just like any other character. You can even create a table named [ ]. (I tried it, and it worked.) I think there are simply no restrictions on this.

Jeroen Landheer
A table, too? Yikes. Didn't think to try that.
Michael Todd
+4  A: 

Any valid characters can be used in between [] to define the column & table names. Given that something like [A Column] is valid (with space), there is no reason to prevent a single space as a column name.

However, trailing spaces are removed so.

[ ] and [  ] (1 & 2 spaces) 
will be both treated as

[ ] (1 space).
Steven
Is the "However..." part a citation, or just formatted as one? If it is a citation, please add a link.
Tomalak
I have updated answer, the However isn't a citation, I just messed up the pre tag.
Steven
+4  A: 

Space is an acceptable character, as some people like to have spaces in their column names. And if it can be a part of a column name then why not a column name by itself?

Why do they ever do it will remain a mystery to me, as it makes programming so much more difficult (constantly enclosing everything in "" or []). I have actually seen a column name with a question mark, which is definitely something I would avoid using in any identifier, but it's possible, still.

Peter Perháč
Ack! In a way, that's even worse.
Michael Todd
+2  A: 

I kinda appreciate the assumption that I'm responsible enough not to hurt myself with potentially dangerous features. I haven't found a good reason for spaces in object names myself, but it seems to be popular in some cases. This is an extreme example of running with scissors.

le dorfier