sql - Computed Field fails because ANSI_WARNINGS = OFF - Stack Overflow

admin2025-04-29  2

I am trying to have an auto-computed column to tell me the difference in hours between the 2 dates.

It will not let me do it because the database has ANSI_WARNINGS = OFF.

I know the best practice is to set ANSI_WARNINGS ON and handle the issues instead of trying to hide them, however, I am not the owner of the database, so I need to work with what I can.

I also know that if I remove the "persisted" keyword, it works, but I need to remove the overhead it generates when queries are made.

Is there any way I can adjust my computed column to allow its creation?

Here is my SQL statement:

ALTER TABLE TABLE_NAME 
    ADD DATE_DIFFERENCE 
        AS ROUND( (DATEDIFF(minute, DATETIME1, DATETIME2) / 60.00), 2) PERSISTED

I am getting this error:

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

I am trying to have an auto-computed column to tell me the difference in hours between the 2 dates.

It will not let me do it because the database has ANSI_WARNINGS = OFF.

I know the best practice is to set ANSI_WARNINGS ON and handle the issues instead of trying to hide them, however, I am not the owner of the database, so I need to work with what I can.

I also know that if I remove the "persisted" keyword, it works, but I need to remove the overhead it generates when queries are made.

Is there any way I can adjust my computed column to allow its creation?

Here is my SQL statement:

ALTER TABLE TABLE_NAME 
    ADD DATE_DIFFERENCE 
        AS ROUND( (DATEDIFF(minute, DATETIME1, DATETIME2) / 60.00), 2) PERSISTED

I am getting this error:

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

Share Improve this question edited Jan 7 at 4:54 marc_s 757k184 gold badges1.4k silver badges1.5k bronze badges asked Jan 7 at 0:56 Joseph EspositoJoseph Esposito 1 4
  • 1 If you're not the owner of the database and can't change the ANSI_WARNINGS settings, then you can't do this - so you'll need to think of other ways to accomplish this. You could investigate e.g. triggers on the table to just simply set the value of a "normal" (not computed) column, or you could possibly have a scheduled job that would update that column on a regular basis (e.g. every hour) - this depends on your needs which you haven't specified in great detail - so I can't really recommend anything .... – marc_s Commented Jan 7 at 4:56
  • 2 You can set ansi_warnings on before executing the above. Not sure persisted will help though, unless you create index, and usually its not needed either – siggemannen Commented Jan 7 at 7:23
  • The setting is persisted as part of object definitions themselves, so they act in a way compatible with the original code (ie you can have a view defined with it OFF referring to a view with it ON). Computed columns etc don't allow this though. But you should always have it ON anyway. You always have permission to set it ON in your own session, and it will persist in certain objects that you are allowed to create.. – Charlieface Commented Jan 7 at 9:29
  • @Charlieface sorry, i meant persisted as in the computed column persisted part of the syntax. – siggemannen Commented Jan 7 at 9:32
Add a comment  | 

1 Answer 1

Reset to default 0

Short answer "NO" - the documentation is clear ANSI_WARNINGS must be ON when you are creating or manipulating indexes on computed columns or indexed views.

If you're not the owner of the database and can't change the ANSI_WARNINGS settings, then you can't do this - so you'll need to think of other ways to accomplish this. You could investigate e.g. triggers on the table to just simply set the value of a "normal" (not computed) column, or you could possibly have a scheduled job that would update that column on a regular basis (e.g. every hour) - this depends on your needs which you haven't specified in great detail - so I can't really recommend anything

转载请注明原文地址:http://anycun.com/QandA/1745936422a91356.html