ANSI_NULL_DEFAULT always set to on

Michael Karpenko 21 Reputation points
2021-12-01T21:40:42.247+00:00

Hi it is year 2021 almost 2022

the issue described here is still a problem

ansinulldefault-always-set-to-on-in-database-professional-rtm

Is any official way to control ANSI_NULL_DEFAULT in VS Database Project or SQLpackage.exe or in Azure DevOps YAML task called "SqlDacpacDeploymentOnMachineGroup@0"

Not Monitored
Not Monitored
Tag not monitored by Microsoft.
36,002 questions
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,552 questions
0 comments No comments
{count} votes

Accepted answer
  1. Tom Phillips 17,716 Reputation points
    2021-12-02T13:23:45.25+00:00

    In answer to your question, no there is no way to control that option from a database project directly. You can create a post-deploy script to change it back if needed. There are several things like this database projects do automatically which you have no control over. A database project expects you to always use the database project and deployment. So the code stored and verified inside the project is expecting that setting to be on.

    That setting only controls columns added to tables without NULL/NOT NULL explicitly set. The simplest answer is to always explicitly set those options in your code. Then the setting does not matter.

    0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Erland Sommarskog 101K Reputation points MVP
    2021-12-01T22:28:01.077+00:00

    That setting is a legacy setting that you should never touch. Like it or, by ANSI, a column is nullable unless a NOT NULL constraint is added. So VS Database Projects is doing the right thing when it does not let you meddle with the setting.

    0 comments No comments

  2. Michael Karpenko 21 Reputation points
    2021-12-01T23:20:03.69+00:00

    yes, i wish it does not touch it at all

    but i see in my delta script (VS 2019)

    BEGIN
        ALTER DATABASE [$(DatabaseName)]
            SET ANSI_NULL_DEFAULT ON,
            WITH ROLLBACK IMMEDIATE;
    END
    

    and all my databases have it unchanged since created (off)

    So if it is legacy and ignored - I' fine

    but if if change behavior of some procs doing something with NULL values - than that is the problem

    I do

                  "C:\Program Files\Microsoft SQL Server\150\DAC\bin\**sqlpackage**.exe"    ^
                  /action:script ^
                  /diagnostics:true ^
                  /sourcefile:"$(Pipeline.Workspace)\_drop\Database.dacpac"  ^
                  /targetConnectionString:"Data Source=${<!-- -->{ server }}; Initial Catalog=${<!-- -->{ parameters.database }}; User Id=xxx; Password=$(deploypasswordyaml);" ^
                  /outputpath:$(Pipeline.Workspace)\changesScript\changesScript_${<!-- -->{ parameters.environment }}.sql ^   
                  /p:ScriptDatabaseOptions=False  ^
                  /p:IgnoreAnsiNulls=True
    

    To see the script

    and also tried

    • task: SqlDacpacDeploymentOnMachineGroup@0
      displayName: 'Deploy [${<!-- -->{ parameters.database }}] on [${<!-- -->{ server }}]'
      continueOnError: true
      inputs:
      taskType: 'dacpac'
      #targetMethod: 'server'
      DacpacFile: '"$(Pipeline.Workspace)_drop\Database.dacpac"'
      ServerName: '${<!-- -->{ server }}'
      DatabaseName: '${<!-- -->{ parameters.database }}'
      AdditionalArguments: '/p:GenerateSmartDefaults=True'
      AuthScheme: 'sqlServerAuthentication'
      SqlUsername: ${<!-- -->{ parameters.SQL_UserName }}
      SqlPassword: ${<!-- -->{ parameters.SQL_Password }}
    0 comments No comments

  3. Michael Karpenko 21 Reputation points
    2021-12-01T23:39:34.563+00:00

    I've also tried to control all database properties (publish profile and project settings) .

    did not work

    0 comments No comments