如何停止 SQL Server Management Studio 替换“SELECT *”与列列表?

发布于 2024-08-23 13:25:00 字数 593 浏览 5 评论 0原文

SQL Server Mgmt Studio 让我抓狂。

如果我创建一个视图并从表中 SELECT '*',那么一切都可以,我可以保存视图。 查看视图的 SQL(例如,通过编写 CREATE 脚本)会发现“SELECT *”确实保存到视图的 SQL 中。

但是,一旦我使用 GUI 重新打开视图(右键单击 > 修改),SELECT * 就会替换为表中所有列的列列表。

我怎样才能阻止 Management Studio 这样做?我希望我的“SELECT *”保持不变。

也许只是谷歌搜索“SELECT *”的困难阻止了我找到任何与此相关的远程内容(我确实将其放在双引号中)。

拜托,我在 Transact-SQL 方面经验丰富,所以请不要给我讲授为什么我不应该使用 SELECT *。我知道它的所有优点和缺点,而且我有时也会使用它。这是一种语言功能,就像所有语言功能一样,可以用于善意或邪恶(我强调不同意使用它是不合适的)。

编辑:我正在给马克答案,因为似乎不可能关闭此行为。问题被视为已结束。我注意到企业管理器没有做类似的事情。 解决方法是将 SQL 编辑为文本,或使用 Managment Studio 以外的产品。或者在每次编辑视图时不断编辑列列表并替换 *。叹。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

青春如此纠结 2024-08-30 13:25:00

当 SQL Server Mgmt Studio 创建视图时,我假设他们将 * 扩展为在特定时间基础表中存在的列的完整列表,正是出于以下原因:如果基础表之一发生更改怎么办?您是否希望新列只是简单地显示在引用该表的每个视图中?严重地???

我认为微软试图在这里实现“最不令人惊讶的元素”——你的视图将包含创建视图时出现的那些列——并且它会保持这种状态,除非你明确地、有意地改变它。

当基础表发生变化时,我不想让我的视图突然拥有比以前更多的列……你呢?

抱歉,我认为 Mgmt Studio 中没有任何设置可以关闭此行为。

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.

愿与i 2024-08-30 13:25:00

不要使用 GUI 编辑器。

请改用 T-SQL 编辑器。您可以通过选择“脚本查看方式”-> 来获得此信息。 “更改为”->右键菜单中的“新建查询窗口”。

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.

神也荒唐 2024-08-30 13:25:00

尝试以下任一方法:它们是使用 GUI 的替代方法,并且可以使用键盘快捷键设置为片段:

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

或者

exec sp_helptext 'viewname'

结果将保留“select *”。 (已测试)

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文