如何将脚本文本从 SSMS 复制并粘贴到 Outlook 或 Word 中而不会出现乱码?

问题描述:

比如说,我有一个在 SSMS 中格式化得很好的脚本,它用不同语言的各种注释进行了注释.但是,当我将这个好东西复制并粘贴到 Word 并突出显示语法时,我将收到一条语法突出显示的消息,其中的注释是乱码,就好像使用一个代码页阅读源文本并使用另一个代码页粘贴一样.非常讨厌的bug.有谁知道如何一劳永逸地解决这个问题?

Say, I have a script nicely formatted in SSMS and it's annotated with all kinds of comments in different languages. But when I copy and paste this nice thingy into Word with syntax highlighted I will get a syntax-highlighted message with those comments garbled, as if reading the source text with one code page and pasting it using another code page. Very nasty kinda bug. Does anyone know how to solve this issue once and for all?

谢谢!

[更新]

[解决方案]

保存 → 使用编码保存... → 编码:Unicode(带签名的 UTF-8).

Save → Save with Encoding... → Encoding : Unicode (UTF-8 with signature).

[相关论坛]

  • Need a way to set the default encoding for query files in SMSS. by ChrisMay @Microsoft Connect (Go and upvote this issue at Microsoft Connect)
  • SQL Server Management Studio - File Encoding @SqlDev
  • SSMS : Option to set default save as encoding for CSV by AaronBertrand @Microsoft Connect

经过一些测试,我仍然无法重现该问题.而且我不知道为什么将文本从一个 Unicode 兼容应用复制粘贴到另一个 Unicode 兼容应用会产生这样的结果.

After some tests, I'm still unable to reproduce the issue. And I have no idea why copy-pasting text from one Unicode-compatible to another Unicode-compatible app can give such results.

您可以尝试多种方法:

  1. 在 SSMS 中,将脚本保存为 Unicode 文件:SaveSave with Encoding...Encoding : Unicode (UTF-8签名).然后您将能够在 Word 中正确打开它.问题是语法高亮将丢失.

  1. Inside SSMS, save the script as an Unicode file: SaveSave with Encoding...Encoding : Unicode (UTF-8 with signature). You will then be able to open it probably correctly in Word. The problem is that syntax highlighting will be lost.

将脚本另存为 Unicode 文件,然后重新打开它并复制粘贴.也许 SSMS 出于某些原因假设默认情况下有一些花哨的编码,因此这将强制它改用 UTF-8.

Save the script as an Unicode file, than reopen it and copy-paste. Maybe SSMS assume for some reasons that there is some fancy encoding by default, so this will force it to use UTF-8 instead.

尝试粘贴到不同的应用程序(例如浏览器)中.查看屏幕截图的第一行,我记得曾经在*上描述的某些浏览器渲染中看到同样的问题(找不到链接).

Try to paste in different applications (for example the browser). Looking at the first line at your screenshot, I remember once seeing the same problem with some browser renderings described on Wikipedia (can't find the link).

尝试从 Visual Studio(如果已安装)复制相同的文本.将源代码从 Visual Studio 复制到 Office 程序会保留语法突出显示,因此如果您观察到相同的问题,则可能来自此语法突出显示功能.

Try copying the same text from Visual Studio (if installed). Copying source code from Visual Studio to Office programs preserve syntax highlighting, so if you observe the same issue, it may come from this syntax highlighting feature.

如果没有任何效果,请将问题报告给 Microsoft Connect,准确描述情况,以便Microsoft 的人员将能够重现此问题.

If nothing works, report the problem to Microsoft Connect, describing precisely the situation so the people at Microsoft will be able to reproduce this issue.