SQL Server Management Studio 中的默认编码

问题描述 投票:0回答:6

我最近在我的笔记本电脑上重新安装了 SSMS 2008

当我单击“新查询..”时,系统现在提示我选择文件的编码,这是我以前从未被要求做的事情

我在选项的文本编辑器部分将 sql 与“带编码的 SQL 查询编辑器”关联起来,并重新启动了 SSMS

我可以做什么来与编码关联,因为我不想每次执行新查询或打开现有 sql 文件时都进行选择

sql-server sql-server-2008 ssms
6个回答
8
投票

根据 Fakher Halim 在 Microsoft Azure 反馈论坛上发布的解决方法。

我也有同样的问题。 Git 不会显示历史记录中的任何差异 (除了“二进制文件 a/x.sql 和 b/x.sql 不同)。所以我单击 工具=>选项=>环境=>国际设置。改变了 语言从“英语”到“与 Microsoft Windows 相同”。现在 GIT 差异 工作得很好——正常报告版本差异!


4
投票

以前的答案建议使用工具→选项→环境→国际设置,但SSMS 17.2中不存在此选项。

相反,您可以转到文件→另存为...,单击“保存”按钮上的箭头,然后选择“使用编码保存”。


4
投票

基于其他地方给出的建议(https://feedback.azure.com/forums/908035-sql-server/suggestions/32892454-need-a-way-to-set-the-default-encoding-for-query -f)我编写了一个“命令脚本”来尝试使用 BOM 将“默认编码”更改为 UTF-8。该脚本的工作原理是将带 BOM 的 UTF-8 模板写入 SQL Server 程序文件树中的 SQLFile.sql 文件。它在我的机器上运行过一次,但这并不意味着它没有错误或 100% 安全。使用风险自负!

根据该线程中“微软”的回复:

文件的默认保存方式由位于 C:\Program Files\Microsoft SQL Server\90\Tools\Binn\VSShell\Common7\IDE\sqlworkbenchprojectitems\Sql 的主模板文件 SQLFile.sql 的方式确定。如果您使用 ANSI 编码保存此文件,则后续新的查询会话将使用此编码。

请注意,在我的计算机上,我在不同的路径中找到了模板,因此我编写了脚本来查找任一程序文件下与该名称匹配的any文件。这似乎是一个愚蠢的名字,错误的碰撞应该很少见。

如果没有传递任何参数(或除预期之外的任何参数),那么它应该进行空运行。确保首先在没有管理权限的情况下运行它。它至少会找到任何适用的文件,然后您可以决定是否要继续执行脚本或自己修改文件。

要真正运行脚本,请传递第一个参数“force”。这是危险的部分。请注意。要运行脚本完成而不暂停用户输入,请传递第二个参数“dontpause”(可以传递一个空的第一个参数以在空运行期间跳过暂停)。

我选择 UTF-8 因为它与 7 位 ASCII 兼容,它支持您需要编写的绝大多数语言和字符,它非常有效地存储西方文本和源代码,并且它对源代码控制/文本补丁友好。脚本底部的模板包含一个字节顺序标记 (BOM),向编辑器发出信号,表明它不是 ISO-8859-1 或其他一些不兼容的默认 8 位编码。确保复制/粘贴时不会丢失。将实际批处理脚本保存为 ASCII(或兼容的;没有 BOM!)。命令处理器因 UTF-8 BOM 而阻塞。

@echo off
setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION || exit /b 1

if "x%1" == "xforce" (
    echo Force enable. Danger Will Robinson! This means that I'm going to
    echo attempt to move files and write a new file to replace them. This
    echo is potentially destructive if there are any bugs in this script.
    echo In particular, if there are any unrelated files with the name
    echo sqlfile.sql under your Program Files directories then they'll be
    echo corrupted.
    echo.
    echo This script is going to require elevated privileges to write to
    echo Program Files. Execute the Command Prompt application as an
    echo administrator to proceed ^(should be harmless to try without
    echo elevation first^).
    echo.
    echo I RECOMMEND YOU _DO NOT_ RUN THIS SCRIPT FROM WINDOWS EXPLORER
    echo BECAUSE YOU MAY HAVE A HARDER TIME READING THE OUTPUT. Start a
    echo Command Prompt and run it there.
    echo.

    if not "x%2" == "xdontpause" (
        echo Now is a good time to Ctrl+C ^(and Y^).
        pause
    )
) else (
    echo Dry-run enabled. Pass a lone argument of "force" to go for real. 1>&2
    echo Be careful running the script several times. Your template backup
    echo will be overwritten and lost.
)

set paths="C:\Program Files (x86)\SQLFile.sql" "C:\Program Files\SQLFile.sql"

for /f "tokens=*" %%f in ('dir /a /b /s %paths% 2^>NUL') do @(
    echo.
    echo Found: %%f

    if "x%1" == "xforce" (
        echo Moving to: %%f.orig
        echo.
        echo If you ^(or anything else^) has made any changes to your template
        echo then you should be able to recover your template from the backup
        echo file above ^(not yet, once you continue^) ^(alternatively,
        echo recover the template from the source file now^).

        if not "x%2" == "xdontpause" (
            pause
        )

        move "%%f" "%%f.orig" || exit /b 1
    )

    echo.
    echo Writing a standard UTF-8 template with byte-order mark ^(BOM^).
    echo Feel free to open this file up afterward and manually set the
    echo encoding preferred. You can also replace it with your own
    echo template text.

    if not "x%2" == "xdontpause" (
        pause
    )

    set ok=0

    rem Read in myself, look for the __BEGIN__ marker and write
    rem subsequent lines to the file.
    for /f "tokens=*" %%g in (%~dpf0) do @(
        if !ok! == 1 (
            if "x%1" == "xforce" (
                if "x%%g" == "x." (
                    echo.
                    echo.>>"%%f"|| exit /b 1
                ) else (
                    echo %%g
                    echo %%g>>"%%f"|| exit /b 1
                )
            )
        ) else (
            if "%%g" == "__BEGIN__" (
                echo.
                echo Found marker. Starting write on next ^(non-empty^) line... 1>&2
                echo.
                set ok=1
            )
        )
    )
)

exit /b 0

Below is the SQL template. Lines containing only a dot (.) represent
blank lines. Actual blank lines will be lost.

__BEGIN__
BEGIN TRANSACTION;
.
SET XACT_ABORT ON;
.
.
.
ROLLBACK;
--COMMIT;

3
投票

在 SQL Server Management Studio 中,选择“工具/选项”,然后展开“文本编辑器”并添加扩展名“sql”并将其映射到“SQL 查询编辑器”。

如果您使用的是 ASCII 字符,则该解决方案有效。如果 SQL 包含需要显式编码的字符(由 SQL 编辑器确定),那么您必须选择一种编码。当在 SQL 编辑器和其他编辑器 (NotePad++) 中创建文件,然后在其他编辑器中编辑这些文件时,就会出现此问题。 NotePad++保存没有标题的文件并且可以猜测编码。另一方面,一旦使用某些字符,SQL 编辑器总是需要编码标头。


1
投票

我真是个白痴 - 我将关联更改为“SQL 查询编辑器”,现在一切正常

“使用编码”这一点真的应该给我提示!


0
投票

我面临这个问题,我已经完成了上述所有提供的解决方案,请帮助我。

© www.soinside.com 2019 - 2024. All rights reserved.