SQL Docker 中的服务器 CREATE INDEX 失败,因为以下 SET 选项设置不正确:'QUOTED_IDENTIFIER'

SQL Server in Docker CREATE INDEX failed because the following SET options have incorrect settings: ‘QUOTED_IDENTIFIER’

我有一个 SQL 服务器 Dockerfile,我的 import-data.sh 从 sql-data 文件夹导入 *.sql 文件。如果我 运行 来自 Datagrip 等工具的 *.sql 文件,一切正常,但当它自动 运行 时,导入失败并显示此错误消息。

错误信息:

Msg 1934, Level 16, State 1, Line 4
CREATE INDEX failed because the following SET options have incorrect settings: 'QUOTED_IDENTIFIER'. Verify that SET options are correct for use with indexed views and/or indexes on computed columns and/or filtered indexes and/or query notifications and/or XML data type methods and/or spatial index operations.

Dockerfile

FROM microsoft/mssql-server-linux:2017-latest

RUN mkdir /sql-data/
EXPOSE 1433

COPY entrypoint.sh /usr/local/bin/
RUN chmod +x /usr/local/bin/entrypoint.sh

COPY import-data.sh /usr/src/app/
RUN chmod +x /usr/src/app/import-data.sh

# Copy SQL Scripts to sql-data for processing
COPY ./sql-data/*.sql /sql-data/

CMD /bin/bash /usr/local/bin/entrypoint.sh

entrypoint.sh

#!/bin/bash

#start SQL Server, start the script to create the DB and import the data, start the app
/usr/src/app/import-data.sh & /opt/mssql/bin/sqlservr

导入-data.sh

#!/bin/bash
# wait for the SQL Server to come up https://github.com/twright-msft/mssql-node-docker-demo-app/issues/11
while [ ! -f /var/opt/mssql/log/errorlog ]
do
  sleep 2
done

## tail the error log for the startup dll and then quit
tail -f /var/opt/mssql/log/errorlog | while read LOGLINE
do
   [[ "${LOGLINE}" == *"Using 'xpstar.dll' version"* ]] && pkill -P $$ tail
done

echo "Running SQL Scripts"
# Scan for SQL files and load them in
for file in /sql-data/*.sql; do
    echo $file
    /opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P $SA_PASSWORD -i $file 
done

/sql-data/setup.sql

IF NOT EXISTS (SELECT name FROM master.dbo.sysdatabases WHERE name = 'Products')
BEGIN
    CREATE DATABASE Products;
END
GO

USE Products;
GO

SQL Server Management Studio 和 Datagrip 等工具默认启用带引号的标识符。您必须通过将 SQL 脚本修改为 SET QUOTED_IDENTIFIER ON

在 SQLCMD 中手动启用它

您可以像这样修改 setup.sql 脚本:

/sql-data/setup.sql

SET QUOTED_IDENTIFIER ON
IF NOT EXISTS (SELECT name FROM master.dbo.sysdatabases WHERE name = 'Products')
BEGIN
    CREATE DATABASE Products;
END
GO

USE Products;
GO

不幸的是,出于向后兼容的原因,SQLCMD 实用程序默认为 QUOTED_IDENTIFIER OFFSpecify the -I argument so that QUOTED_IDENTIFIER ON is used.

/opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P $SA_PASSWORD -i $file -I