'Incorrect SET Options' Error When Building Database Project

后端 未结 6 1498
长情又很酷
长情又很酷 2020-12-03 01:02

We are using Visual Studio and a database project to generate our database.

I just made a number of database changes (including adding a new table named Corres

相关标签:
6条回答
  • 2020-12-03 01:24

    In my case I was trying to create a table from one database to another on MS SQL Server 2012. Right-clicking on a table and selecting Script Table as > DROP And CREATE To > New Query Editor Window, following script was created:

    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
    

    However when executing above script it was returning the error:

    SELECT failed because the following SET options have incorrect settings: 'ANSI_PADDING'. 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.

    The Solution I've found: Enabling the settings on the Top of the script like this:

    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
    

    Hope this help.

    0 讨论(0)
  • 2020-12-03 01:28

    I had the same issue with the filtered index and my inserts and updates were failing. All I did was to change the stored procedure that had the insert and update statement to:

    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
    
    0 讨论(0)
  • 2020-12-03 01:30

    I found the solution for this problem:

    1. Go to the Server Properties.
    2. Select the Connections tab.
    3. Check if the ansi_padding option is unchecked.
    0 讨论(0)
  • 2020-12-03 01:37

    According to BOL:

    Indexed views and indexes on computed columns store results in the database for later reference. The stored results are valid only if all connections referring to the indexed view or indexed computed column can generate the same result set as the connection that created the index.

    In order to create a table with a persisted, computed column, the following connection settings must be enabled:

    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
    

    These values are set on the database level and can be viewed using:

    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
    

    However, the SET options can also be set by the client application connecting to SQL Server.

    A perfect example is SQL Server Management Studio which has the default values for SET ANSI_NULLS and SET QUOTED_IDENTIFIER both to ON. This is one of the reasons why I could not initially duplicate the error you posted.

    Anyway, to duplicate the error, try this (this will override the SSMS default settings):

    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
    ) 
    

    You can fix the test case above by using:

    SET ANSI_PADDING ON
    SET ANSI_WARNINGS ON
    

    I would recommend tweaking these two settings in your script before the creation of the table and related indexes.

    0 讨论(0)
  • 2020-12-03 01:40

    In my case, I found that a computed column had been added to the "included columns" of an index. Later, when an item in that table was updated, the merge statement failed with that message. The merge was in a trigger, so this was hard to track down! Removing the computed column from the index fixed it.

    0 讨论(0)
  • 2020-12-03 01:42

    For me, just setting the compatibility level to higher level works fine. To see C.Level :

    select compatibility_level from sys.databases where name = [your_database]
    
    0 讨论(0)
提交回复
热议问题