4

我有一个 SQL Server Dockerfile,我的 import-data.sh*.sql从 sql-data 文件夹导入文件。如果我从 Datagrip 之类的工具运行*.sql文件,一切正常,但是在自动运行时导入失败并显示此错误消息。

错误信息:

消息 1934,级别 16,状态 1,第 4 行
CREATE INDEX 失败,因为以下 SET 选项的设置不正确:'QUOTED_IDENTIFIER'。验证 SET 选项对于索引视图和/或计算列上的索引和/或过滤索引和/或查询通知和/或 XML 数据类型方法和/或空间索引操作是否正确。

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

入口点.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

导入数据.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-数据/setup.sql

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

USE Products;
GO
4

2 回答 2

10

QUOTED_IDENTIFIER OFF遗憾的是,出于向后兼容性的原因,SQLCMD 实用程序默认为。指定-I参数以便QUOTED_IDENTIFIER ON使用。

/opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P $SA_PASSWORD -i $file -I
于 2018-07-05T23:44:02.557 回答
1

SQL Server Management Studio 和 Datagrip 等工具默认启用带引号的标识符。您必须在 SQLCMD 中手动启用它,方法是将您的 SQL 脚本修改为SET QUOTED_IDENTIFIER ON

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

/sql-数据/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
于 2018-07-05T22:56:53.113 回答