views:

175

answers:

3

SQL Server Mgmt Studio is driving me crazy.

If I create a view and SELECT '*' from a table, it's all OK and I can save the view. Looking at the SQL for the view (eg.by scripting a CREATE) reveals that the 'SELECT *' really is saved to the view's SQL.

But as soon as I reopen the view using the GUI (right click > modify), SELECT * is replaced with a column list of all the columns in the table.

How can I stop Management Studio from doing this ? I want my 'SELECT *' to remain just that.

Perhaps it's just the difficulty of googling 'SELECT *' that prevented me from finding anything remotely relevant to this (i did put it in double quotes).

Please, I am highly experienced in Transact-SQL, so please DON'T give me a lecture on why I shouldn't be using SELECT *. I know all the pros and cons and I do use it at times. It's a language feature, and like all language features can be used for good or evil (I emphatically do NOT agree that it is never appropriate to use it).

Edit: I'm giving Marc the answer, since it seems it is not possible to turn this behaviour off. Problem is considered closed. I note that Enterprise Manager did no similar thing. The workaround is to either edit SQL as text, or go to a product other than Managment Studio. Or constantly edit out the column list and replace the * every time you edit a view. Sigh.

+2  A: 

When SQL Server Mgmt Studio creates a view, I assume they're expanding the * to the complete list of columns that are present in the underlying table(s) at that particular time exactly for this reason: what if one of the underlying tables changes? Do you want the new columns to just simply show up in every view that references that table?? Seriously???

I think Microsoft tries to impmenent the "element of least surprise" here - your view will contain those columns that are present at the time the view gets created - and it stays that way, unless you explicitly and knowingly change that.

I for one wouldn't want to have my views suddenly having more columns than before, when an underlying table changes..... do you??

And I don't think there's any setting in Mgmt Studio to turn this behavior off, sorry.

marc_s
+1: Sums up my opinion as well. The described behavior is, for once, a feature.
OMG Ponies
1. He explicitly asked to not get any lectures about Select *. 2. You can avoid the auto-expansion by picking "Script View As" instead of "Design" from the right-click menu. 3. It is acceptable to use "Create View dbo.TableName AS Select * From OtherDatabase.dbo.TableName" when you need to link two databases together.
Jonathan Allen
@Jonathan Allen: ok, thanks for your viewpoint. I don't agree, but that's ok.
marc_s
Thanks for answers. I think the consensus so far is 'no'. <rant> Yes, I DO want new columns to simply show up in my view. That's exactly what I want. I DO realise that the presence of new columns will 'break' my view, until I do an sp_refreshview to regenerate the metadata. If I MEANT 'SELECT Col1, Col2, Col3, ... , ColN', I'd type it, or use one of the many methods of autogenerating the list. What I MEAN is 'SELECT *', but it seems M$ have decided I'm not allowed to use this valuable language feature any more. It's like SQL-McCarthyism. </rant>
Ben McIntyre
Sorry if I get emotional, but SQL Enterprise Manager was a good tool and I'm very fond of it. MSSMS lacks sooo many of its good features, is slow and very prescriptive. Unfortunately EM does not play with SQL2005+, so I'm being dragged kicking and screaming to using this 'management tool'. It quite significantly affects my productivity. I don't have any issues with text-editing SQL in the new tool, but that's a no-brainer. Sometimes the GUI is way quicker and that's what I asking about here.
Ben McIntyre
@Ben: well, I for one do **not** long to go back to the days of EM/QA - having two tools, always having to go back and forth between them, is no fun. I much prefer SSMS despite it being "speed challenged" - but things like Intellisense (in 2008) and other goodies make up for that many times over!
marc_s
@marc: hmmm, I've always thought the new tool has a Frankenstein-like quality about it, in that it tries to do too many things. I'd prefer two tools that work quickly and well. The intellisense will be good (if I ever make it to 2008 :-) but issues like the cause of this question are just plain boneheaded. I find it really hard to suppress the instinct to flee in horror from this product.
Ben McIntyre
@Ben McIntyre: maybe I can interest you in "MiniSQL" ? http://www.pksoftware.net/MiniSqlQuery/
marc_s
@marc: thanks for the link ! I'll certainly look into it. I've always liked the graphical view, especially when I am dealing with cascades of 10 or more tables, and its one-click column selection. But I suppose a good SQL intellisense enabled tool might cut it. I note Redgate also have a swathe of excellent SQL products. I investigated all the products about 5 years ago and 'Toad for SQL Server' looked like the best EM/QA alternative at the time; I installed it but never got around to actually firing up and using it.
Ben McIntyre
+2  A: 

Don't use the GUI editor.

Instead use the T-SQL editor. You get this by selecting "Script View As" -> "ALTER to" -> "New Query Window" from the right-click menu.

Jonathan Allen
Thanks; with respect I can edit the SQL raw, but that's not what I'm talking about here. There are some times it's best to do that, and sometimes the GUI is way quicker.
Ben McIntyre
Sorry, but unless I missed somethings that's the closest you are going to get.
Jonathan Allen
A: 

Try either of these: they are alternatives to using the GUI and can be setup as snippets with keyboard shortcuts:

select view_definition 
from information_schema.views
where table_name = 'viewname'

or

exec sp_helptext 'viewname'

The results will retain the "select *". (Tested)

Jeff Meatball Yang
See above comment.
Ben McIntyre