SQL Server 2008 列选择
在 SQL Server 中,如果我有一个包含大约 20 列的表,而我想要其中的 18 列,我可以说类似 * minus columnname1, columnname2,
当然现在我把它们全部写下来。
但如果可以的话那就容易多了。
In SQL Server, if I got a table with like 20 columns and I want 18 of them, can I say something like * minus columnname1, columnname2,
course right now I write them all.
But if you could it would be much easier.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
在没有任何花哨插件的情况下,在 SQL Management Studio 中立即用列名替换星号的小提示:
星号现在应该已扩展到列名:)
Ofc 可以在查询编辑器中选择/取消选择任何列..
Hth
Little hint to replace the asterisk with column names in SQL Management Studio in no time without any fancy plugin:
The asterisk should have been expanded to column names now :)
Ofc it's possible to select/deselect any column in the query editor..
Hth
这是不可能的。但是,如果您使用的是 SQL Server Management Studio 2008 / 2005,则可以右键单击表并选择“将表脚本编写为 > SELECT To”菜单选项。这将节省您输入列名的时间,或者购买 Red-Gate 的 SQL 提示符
It is not possible. However if you are using SQL Server Management Studio 2008 / 2005 you can right click on the table and select the "Script Table as > SELECT To" menu option. This will save you typing the column names, or purchase Red-Gate's SQL Prompt
开箱即用 - 不,这是不可能的。您必须明确拼出您想要的所有列。
使用 SQL Server Management Studio 2008,智能感知可以帮助您从表中选择列 - 因此这无疑是帮助缓解痛苦的一个步骤。
SQL Prompt 等附加工具提供更多帮助 -在 SQL 提示符中,您可以键入
,如果光标位于星号符号 (
*
) 后面,则可以按
并将星号展开到列表中该表的所有列(然后删除您不需要的两列) - 或者您可以弹出一个窗口并选择您真正想要的那些列。非常方便,非常有用,非常加快开发速度 - 但它不是一个免费工具......
Out of the box - no, it's not possible. You have to spell out all the columns you want explicitly.
With SQL Server Management Studio 2008, there is intellisense which can help you select columns from a table - so that's certainly one step to help ease the pain.
Add-in tools like SQL Prompt offer more help - in SQL Prompt, you can type
and if you have the cursor just after the asterisk symbol (
*
), you can press<TAB>
and expand the asterisk into the list of all columns for that table (and then remove the two you don't want) - or you can popup a window and pick those columns you really want.Very handy, very useful, very much speeding up development - but it's not a free tool.....
如果您想要 18 行,您可以使用
select TOP (18) * from GiveTable
。对于列没有这样的方法。事实上,列名存储在主数据库中,您可以提取它们并构建类似于您所要求的查询,但这并不比从 blaBlaBla 选择 field1,field2 ... field18 更容易。
会给你你的专栏列表。您可以根据此查询编写选择生成器。
You can use
select TOP (18) * from givenTable
is you want 18 rows.There is no such method for columns. In fact column names are stored in master db and you can extract them and consruct query looking like what you are asking for BUT it would not be easier than just select field1,field2 ... field18 from blaBlaBla.
will give you the list of your columns. You can write select generator based on this query.
我想补充一下“不,不可能直接在 SQL 中”的答案。我也喜欢拥有这个功能!当您尝试对具有 varbinary(max) 的 10+ 列表进行快速调试时,这很糟糕。
但我真的只是想指出 Kane 的 SSMS 2008 (Sql Server Management Studio) 技巧的替代方案。
如果打开对象资源管理器(在查询窗口中右键单击并选择“在对象资源管理器中打开服务器”),请导航到相关表的节点。展开节点,以便您可以看到“列”节点。现在将“列”节点“拖”到查询窗口并“放下”它。它将粘贴表的所有列名——您可以直接在 SELECT 子句中使用它。
I'd like to add to the answer of, "No, it's not possible directly in SQL". I would love to have that feature too! It sucks when you're trying to do some quick debugging on a 10+ column table that has a varbinary(max).
But I really just want to point out an alternative to Kane's tip for SSMS 2008 (Sql Server Management Studio).
If you open the Object Explorer (right-click in the query window and choose "Open Server in Object Explorer"), navigate to the node for the table in question. Expand the node so you can see the "Columns" node. Now "drag" the Columns node over to your query window and "drop" it. It will paste in all the column names for the table--and you can use it directly in a SELECT clause.
据我所知这是不可能的。
It is not possible as far as I know.
我创建了一个用于轻松复制/粘贴多列的脚本,您可能会发现它很有用。请参阅:
http://www.sqlservercentral.com/scripts/102375/
脚本说明那里有详细信息,但对于那些在 sqlservercentral 上没有帐户的人来说,简而言之:
这是一个可以使用快捷方式运行的存储过程。输入您的表名(也适用于临时表和视图),突出显示它,点击快捷键,它将显示表的列。从那里您可以轻松复制多个列(这些列在列名称前面也显示有逗号,这样也可以节省您的打字时间)并将其粘贴到查询屏幕中。
I have created a script for easy copy/pasting multiple columns, you might find it useful. See:
http://www.sqlservercentral.com/scripts/102375/
The script is explained in detail there, but in short for those who do not have an account on sqlservercentral:
It's a stored procedure that i can run using a shortcut. Type in your tablename (also works with temp tables and views), highlight it, hit the shortcut and it will display the columns of the table. From there you can easily copy multiple columns (the columns are also shown with a comma in front of the column name, so that also saves you some typing) and paste it in your query screen.