SQL Server 的简单结果格式化
我从事 SQL 工作已经有一段时间了,而且我一直很满意使用 SSMS 中的Results to Grid
。
现在我正在运行一系列查询,我希望对结果进行一些非常简单的格式化。目前,Results to Grid
和 Results to Text
都没有达到我想要的效果。
为了让我更容易阅读,我想做的一些事情是
- 删除“# row(s)受影响”的文本(在
结果到文本
中找到) - 使列不那么在对齐的输出格式列中宽(部分问题是最大字符数似乎没有低于 30 - 这是我的数据强制执行此操作吗?)
如果我无法格式化输出(即使是文本文件)什么我还有其他选择吗?
我花了一些时间研究 SQL Server -> PHP-> HTML 以及 SQL Server ->报告服务 -> MS Report Builder,但坦率地说,放置一些间隔符并稍微美化标题似乎有点矫枉过正。
我觉得我在这里遗漏了一些东西......我宁愿不经历所有 PHP 安装的麻烦,也不只是为了能够让我的数据看起来更漂亮一点。
I have been doing SQL for a while and I've always been satisfied to use the Results to Grid
found in SSMS.
Now I have a series of queries that I am running and I would like to have some very simple formatting of the results. Currently neither the Results to Grid
nor the Results to Text
do quite what I would like to do.
A few things I would like to do so it is easier for me to read is
- Remove the text that says '# row(s) affected' (found in the
Results to Text
) - Make the columns not so wide in the column aligned Output Format (part of the problem is that the Maximum Number of Characters does not appear to go below 30 - is this my data that forces this?)
If I cannot format the output (even to a text file) what other options do I have ?
I spent some time looking at SQL Server -> PHP -> HTML as well as SQL Server -> Reporting Services -> MS Report Builder but quite frankly it seems like overkill to put a few spacers and pretty up the headings a bit.
I feel like I am missing something here ... I would rather not go through the hassle of all that installation of PHP and what not just to be able to look at my data a little bit prettier.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将此
SET NOCOUNT ON
放在 sql 的顶部是的,它是执行此操作的字段的大小。如果您知道不会丢失数据,则可以将其强制转换
cast(field as varchar(20)
以使其更小。Put this
SET NOCOUNT ON
at the top of your sqlYes its the size of the field that does this. You can cast it
cast(field as varchar(20)
to make it smaller if you know you won't lose data.这完全取决于您想要对格式化结果执行什么操作。
为了快速读取/格式化直接在 Management Studio 中查看时效果不佳的结果,我使用
Results to Grid
,选择所有带标题的内容(通过右键单击网格的左上角) ),然后复制/粘贴到 Excel 中。从那里可以轻松地对列宽和格式进行基本修改。对我来说最大的缺点是日期从来都不是开箱即用的,但这总是一个快速解决方案。当我将查询结果粘贴到电子邮件中时,Excel 还可以很好地进行基本格式设置。
在某些情况下,这可能有点矫枉过正,但我怀疑比使用 PHP 的效果要小得多 -> HTML 或报告服务 -> MS 报告生成器。
It all depends on what you want to do with the formatted results.
For quickly reading / formatting a result that isn't great when viewed directly in Management Studio, I use
Results to Grid
, select all with headers (by right-clicking on the upper-left corner of the grid), and copy/paste into Excel. From there it's easy to do basic tinkering with column widths and formatting. The biggest downside for me is dates are never quite right out-of-the-box, but it's always a quick fix.Excel also makes a good interim stop for basic formatting when I'm pasting query results into an email.
It might be overkill in some cases, but I suspect much less so than using PHP -> HTML or Reporting Services -> MS Report Builder.