SQL Server Management Studio中的默认编码

时间:2022-07-24 16:59:48

I have recently had SSMS 2008 re-installed on my laptop

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

When I click "New Query..", I am now prompted to select the encoding for the file, something I was never asked to do before

当我点击“New Query ..”时,我现在被提示选择文件的编码,这是我以前从未被要求做的事情

I have associated sql againt "SQL Query Editor with Encoding" in the Text Editor section of the Option and restarted SSMS

我在选项的文本编辑器部分中关联了SQL“带有编码的SQL查询编辑器”并重新启动了SSMS

What can I do to associate with an encoding as I don't want to select everytime I do a new query or open an existing sql file

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

5 个解决方案

#1


6  

As per the workaround posted by Fakher Halim on Microsoft Connect.

根据Fakher Halim在Microsoft Connect上发布的解决方法。

I had the same issue. Git wouldn't show any differences in history (besides "Binary files a/x.sql and b/x.sql differ). So I clicked Tools=>Options=>Environments=>International Settings. Changed the Language from "English" to "Same as Microsoft Windows". Now GIT DIFF works perfectly well -- reports version difference normally!

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

#2


3  

Based on the advice given elsewhere (https://connect.microsoft.com/SQLServer/feedback/details/288677/unicode-defaults-on-ssms-save) I've hacked up a "command script" to attempt to "change the default encoding" to UTF-8 with a BOM. The script works by writing a UTF-8 w/ BOM template to a SQLFile.sql file within the Program Files tree of SQL Server. It has worked once on my machine, but that doesn't mean that it's bugfree or 100% safe. USE AT OWN RISK!

基于其他地方给出的建议(https://connect.microsoft.com/SQLServer/feedback/details/288677/unicode-defaults-on-ssms-save)我已经破解了一个“命令脚本”试图“改变”默认编码为“带有BOM的UTF-8”。该脚本的工作原理是将UTF-8 w / BOM模板写入SQL Server程序文件树中的SQLFile.sql文件。它在我的机器上运行过一次,但这并不意味着它没有bug或100%安全。使用风险自负!

According to "Microsoft's" response in that thread:

根据“微软”在该主题中的回应:

The default for how files are saved is determined by the way the main template file SQLFile.sql located at C:\Program Files\Microsoft SQL Server\90\Tools\Binn\VSShell\Common7\IDE\sqlworkbenchprojectitems\Sql. If you save this file using ANSI encoding, then subsequent new query sessions will use this encoding.

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

Note that on my machine I found the template at a different path so I wrote the script to find any file matching that name under either Program Files. It seems like a silly enough name that false collisions should be rare.

请注意,在我的机器上,我发现模板位于不同的路径,因此我编写了脚本以在Program Files下找到与该名称匹配的任何文件。这似乎是一个愚蠢的名称,错误的碰撞应该是罕见的。

If passed no arguments (or any arguments other than what it expects) then it should do a dry-run. Make sure to run it without administrative privileges first. It will at least find any applicable files and you can decide then if you want to proceed with the script or modify the files yourself.

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

To run the script for real then pass a first argument of "force". This is the dangerous part. Heed caution. To run the script to completion without pausing for user input then pass a second argument of "dontpause" (this can be passed with an empty first argument to skip pausing during a dry-run).

要为real运行脚本,请传递第一个“force”参数。这是危险的部分。注意谨慎。要在不暂停用户输入的情况下运行脚本,然后传递第二个参数“dontpause”(这可以通过空的第一个参数传递以在干运行期间跳过暂停)。

I choose UTF-8 because it's 7-bit ASCII compatible, it supports the vast majority of languages and characters you'll ever care to script, it stores Western text and source code very efficiently, and it's source control/text patch friendly. The template at the bottom of the script contains a byte-order mark (BOM) to signal editors that it is not ISO-8859-1 or some other incompatible default 8-bit encoding. Be sure it isn't lost when you copy/paste. Save the actual batch script as ASCII (or compatible; without a BOM!). The command processor chokes on a UTF-8 BOM.

我之所以选择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


3  

Previous answers have suggested using Tools→Options→Environments→International Settings, but this option is not present in SSMS 17.2.

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

Instead, you can go to File→Save As..., click the arrow on the "Save" button, and select "Save with Encoding".

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

#4


1  

I am such an idiot - I changed the association to "SQL Query Editor" and it now all works fine

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

The "with Encoding" bit should really have tipped me off!

“with Encoding”位应该真的让我失望!

#5


1  

In SQL Server Management Studio, select Tools/Options then expand "Text Editor" and add the extension "sql" and map it to "SQL Query Editor".

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

SQL Server Management Studio中的默认编码

The solution works if you are working with ASCII characters. If the sql contains characters that require explicit encoding (as determined by the SQL editor) then you will must select an encoding. The issue comes into play when creating files in both the SQL editor and other editor (NotePad++), then editing those files in the other editor. NotePad++ saves files without the header and can guess the encoding. The SQL editor on the other hand always requires the encoding header once certain characters are used.

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

#1


6  

As per the workaround posted by Fakher Halim on Microsoft Connect.

根据Fakher Halim在Microsoft Connect上发布的解决方法。

I had the same issue. Git wouldn't show any differences in history (besides "Binary files a/x.sql and b/x.sql differ). So I clicked Tools=>Options=>Environments=>International Settings. Changed the Language from "English" to "Same as Microsoft Windows". Now GIT DIFF works perfectly well -- reports version difference normally!

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

#2


3  

Based on the advice given elsewhere (https://connect.microsoft.com/SQLServer/feedback/details/288677/unicode-defaults-on-ssms-save) I've hacked up a "command script" to attempt to "change the default encoding" to UTF-8 with a BOM. The script works by writing a UTF-8 w/ BOM template to a SQLFile.sql file within the Program Files tree of SQL Server. It has worked once on my machine, but that doesn't mean that it's bugfree or 100% safe. USE AT OWN RISK!

基于其他地方给出的建议(https://connect.microsoft.com/SQLServer/feedback/details/288677/unicode-defaults-on-ssms-save)我已经破解了一个“命令脚本”试图“改变”默认编码为“带有BOM的UTF-8”。该脚本的工作原理是将UTF-8 w / BOM模板写入SQL Server程序文件树中的SQLFile.sql文件。它在我的机器上运行过一次,但这并不意味着它没有bug或100%安全。使用风险自负!

According to "Microsoft's" response in that thread:

根据“微软”在该主题中的回应:

The default for how files are saved is determined by the way the main template file SQLFile.sql located at C:\Program Files\Microsoft SQL Server\90\Tools\Binn\VSShell\Common7\IDE\sqlworkbenchprojectitems\Sql. If you save this file using ANSI encoding, then subsequent new query sessions will use this encoding.

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

Note that on my machine I found the template at a different path so I wrote the script to find any file matching that name under either Program Files. It seems like a silly enough name that false collisions should be rare.

请注意,在我的机器上,我发现模板位于不同的路径,因此我编写了脚本以在Program Files下找到与该名称匹配的任何文件。这似乎是一个愚蠢的名称,错误的碰撞应该是罕见的。

If passed no arguments (or any arguments other than what it expects) then it should do a dry-run. Make sure to run it without administrative privileges first. It will at least find any applicable files and you can decide then if you want to proceed with the script or modify the files yourself.

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

To run the script for real then pass a first argument of "force". This is the dangerous part. Heed caution. To run the script to completion without pausing for user input then pass a second argument of "dontpause" (this can be passed with an empty first argument to skip pausing during a dry-run).

要为real运行脚本,请传递第一个“force”参数。这是危险的部分。注意谨慎。要在不暂停用户输入的情况下运行脚本,然后传递第二个参数“dontpause”(这可以通过空的第一个参数传递以在干运行期间跳过暂停)。

I choose UTF-8 because it's 7-bit ASCII compatible, it supports the vast majority of languages and characters you'll ever care to script, it stores Western text and source code very efficiently, and it's source control/text patch friendly. The template at the bottom of the script contains a byte-order mark (BOM) to signal editors that it is not ISO-8859-1 or some other incompatible default 8-bit encoding. Be sure it isn't lost when you copy/paste. Save the actual batch script as ASCII (or compatible; without a BOM!). The command processor chokes on a UTF-8 BOM.

我之所以选择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


3  

Previous answers have suggested using Tools→Options→Environments→International Settings, but this option is not present in SSMS 17.2.

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

Instead, you can go to File→Save As..., click the arrow on the "Save" button, and select "Save with Encoding".

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

#4


1  

I am such an idiot - I changed the association to "SQL Query Editor" and it now all works fine

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

The "with Encoding" bit should really have tipped me off!

“with Encoding”位应该真的让我失望!

#5


1  

In SQL Server Management Studio, select Tools/Options then expand "Text Editor" and add the extension "sql" and map it to "SQL Query Editor".

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

SQL Server Management Studio中的默认编码

The solution works if you are working with ASCII characters. If the sql contains characters that require explicit encoding (as determined by the SQL editor) then you will must select an encoding. The issue comes into play when creating files in both the SQL editor and other editor (NotePad++), then editing those files in the other editor. NotePad++ saves files without the header and can guess the encoding. The SQL editor on the other hand always requires the encoding header once certain characters are used.

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