构建数据库项目时出现“不正确的SET选项”错误

时间:2012-02-10 22:11:16

标签: sql-server sql-server-2008 database-project

我们正在使用Visual Studio和数据库项目来生成我们的数据库。

我刚刚对数据库项目进行了一些数据库更改(包括添加一个名为Correspondence的新表),并尝试部署(重建)数据库。

当我这样做时,我收到以下错误:

  

创建[dbo]。[通讯] ......   消息1934,级别16,状态1,服务器(服务器名称),第1行   CREATE TABLE失败,因为以下SET选项具有不正确的设置   :'ANSI_WARNINGS,ANSI_PADDING'。验证SET选项是否正确使用   具有计算列和/或筛选索引的索引视图和/或索引   和/或查询通知和/或XML数据类型方法和/或空间索引   操作

任何人都可以向我解释这个错误,帮我解决一下吗?这是数据库项目用于创建此表的脚本。

PRINT N'Creating [dbo].[Correspondence]...';
GO

SET ANSI_NULLS, QUOTED_IDENTIFIER ON;
GO

CREATE TABLE [dbo].[Correspondence] (
    [Id]                INT              IDENTITY (1, 1) NOT NULL,
    [WorkbookId]        INT              NOT NULL,
    [ProviderId]        UNIQUEIDENTIFIER NOT NULL,
    [MessageThreadId]   INT              NOT NULL,
    [MessageThreadType] AS               ((1)) PERSISTED NOT NULL
);
GO

SET ANSI_NULLS, QUOTED_IDENTIFIER OFF;
GO

PRINT N'Creating PK_Correspondence...';
GO

ALTER TABLE [dbo].[Correspondence]
ADD CONSTRAINT [PK_Correspondence] PRIMARY KEY CLUSTERED ([Id] ASC)
    WITH (ALLOW_PAGE_LOCKS = ON, ALLOW_ROW_LOCKS = ON, PAD_INDEX = OFF,
    IGNORE_DUP_KEY = OFF, STATISTICS_NORECOMPUTE = OFF);
GO

6 个答案:

答案 0 :(得分:78)

根据BOL

  

计算列上的索引视图和索引将结果存储在   数据库供以后参考。存储的结果仅在全部有效时才有效   连接引用索引视图或索引计算列   可以生成与创建的连接相同的结果集   索引。

要创建具有持久计算列的表,必须启用以下连接设置:

SET ANSI_NULLS ON
SET ANSI_PADDING ON
SET ANSI_WARNINGS ON
SET ARITHABORT ON
SET CONCAT_NULL_YIELDS_NULL ON
SET NUMERIC_ROUNDABORT ON
SET QUOTED_IDENTIFIER ON

这些值在数据库级别设置,可以使用以下方式查看:

SELECT 
    is_ansi_nulls_on,
    is_ansi_padding_on,
    is_ansi_warnings_on,
    is_arithabort_on,
    is_concat_null_yields_null_on,
    is_numeric_roundabort_on,
    is_quoted_identifier_on
FROM sys.databases

但是,the SET options can also be set by the client application连接到SQL Server。

一个完美的例子是SQL Server Management Studio,它将SET ANSI_NULLS和SET QUOTED_IDENTIFIER的默认值都设置为ON。这是我最初无法复制您发布的错误的原因之一。

无论如何,要复制错误,请尝试此操作(这将覆盖SSMS默认设置):

SET ANSI_NULLS ON
SET ANSI_PADDING OFF
SET ANSI_WARNINGS OFF
SET ARITHABORT OFF
SET CONCAT_NULL_YIELDS_NULL ON 
SET NUMERIC_ROUNDABORT OFF
SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE T1 (
    ID INT NOT NULL,
    TypeVal AS ((1)) PERSISTED NOT NULL
) 

您可以使用以下方法修复上述测试用例:

SET ANSI_PADDING ON
SET ANSI_WARNINGS ON

我建议在创建表和相关索引之前在脚本中调整这两个设置。

答案 1 :(得分:2)

我找到了解决这个问题的方法:

  1. 转到“服务器属性”。
  2. 选择 Connections 标签。
  3. 检查是否取消选中 ansi_padding 选项。

答案 2 :(得分:2)

在我的情况下,我试图在MS SQL Server 2012上从一个数据库创建一个表到另一个数据库。右键单击一个表并选择脚本表作为> DROP AND CREATE To>创建了以下脚本的新查询编辑器窗口

USE [SAMPLECOMPANY]
GO

ALTER TABLE [dbo].[Employees] DROP CONSTRAINT [FK_Employees_Departments]
GO

/****** Object:  Table [dbo].[Employees]    Script Date: 8/24/2016 9:31:15 PM ******/
DROP TABLE [dbo].[Employees]
GO

/****** Object:  Table [dbo].[Employees]    Script Date: 8/24/2016 9:31:15 PM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[Employees](
    [EmployeeId] [int] IDENTITY(1,1) NOT NULL,
    [DepartmentId] [int] NOT NULL,
    [FullName] [varchar](50) NOT NULL,
    [HireDate] [datetime] NULL
 CONSTRAINT [PK_Employees] PRIMARY KEY CLUSTERED 
(
    [EmployeeId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

ALTER TABLE [dbo].[Employees]  WITH CHECK ADD  CONSTRAINT [FK_Employees_Departments] FOREIGN KEY([DepartmentId])
REFERENCES [dbo].[Departments] ([DepartmentID])
GO

ALTER TABLE [dbo].[Employees] CHECK CONSTRAINT [FK_Employees_Departments]
GO

然而,当执行上面的脚本时,它返回错误:

  

SELECT失败,因为以下SET选项不正确   设置:'ANSI_PADDING'。验证SET选项是否正确使用   具有索引视图和/或计算列和/或过滤的索引   索引和/或查询通知和/或XML数据类型方法和/或   空间索引操作。

解决方案我发现:在脚本顶部启用设置,如下所示:

USE [SAMPLECOMPANY]
GO
/****** Object:  Table [dbo].[Employees]    Script Date: 8/24/2016 9:31:15 PM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

ALTER TABLE [dbo].[Employees] DROP CONSTRAINT [FK_Employees_Departments]
GO

/****** Object:  Table [dbo].[Employees]    Script Date: 8/24/2016 9:31:15 PM ******/
DROP TABLE [dbo].[Employees]
GO



CREATE TABLE [dbo].[Employees](
    [EmployeeId] [int] IDENTITY(1,1) NOT NULL,
    [DepartmentId] [int] NOT NULL,
    [FullName] [varchar](50) NOT NULL,
    [HireDate] [datetime] NULL
 CONSTRAINT [PK_Employees] PRIMARY KEY CLUSTERED 
(
    [EmployeeId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

ALTER TABLE [dbo].[Employees]  WITH CHECK ADD  CONSTRAINT [FK_Employees_Departments] FOREIGN KEY([DepartmentId])
REFERENCES [dbo].[Departments] ([DepartmentID])
GO

ALTER TABLE [dbo].[Employees] CHECK CONSTRAINT [FK_Employees_Departments]
GO

SET ANSI_PADDING OFF
GO

希望得到这个帮助。

答案 3 :(得分:0)

对我来说,只需将兼容级别设置为更高级别即可。要看C.Level:

select compatibility_level from sys.databases where name = [your_database]

答案 4 :(得分:0)

就我而言,我发现已将计算列添加到索引的“包含列”中。后来,当更新该表中的项目时,merge语句失败,并显示该消息。合并是一个触发器,因此很难追踪!从索引中删除计算列会对其进行修复。

答案 5 :(得分:0)

过滤索引存在相同的问题,并且插入和更新失败。我所做的就是将具有插入和更新语句的存储过程更改为:

create procedure abc
()
AS
BEGIN
SET NOCOUNT ON
SET NUMERIC_ROUNDABORT OFF
SET CONCAT_NULL_YIELDS_NULL ON 
SET ANSI_WARNINGS ON 
SET ANSI_PADDING ON 
end