Turning ANSI_WARNINGS off permanently at the database level with alter command not working - sql

My goal is to turn off ANSI_WARNINGS at the database level permanently. I understand the risks.
The command that I'm using is
ALTER DATABASE <DB_NAME>
SET ANSI_WARNINGS OFF;
However, when I try to insert a big value( beyond what is defined for the column) into a column I still get
String or binary data would be truncated. The statement has been
terminated.
Please help me with making the alter command work permanently

Related

Query Store is configured but none of my queries under any load show up

SQL Server 2017 Enterprise Query Store is showing no data at all but shows READ_ONLY as the actual mode
The one similar question in this forum has an answer that doesn't apply - none of the exclusions are present.
I ran:
GO
ALTER DATABASE [MyDB] SET QUERY_STORE (OPERATION_MODE = READ_ONLY, INTERVAL_LENGTH_MINUTES = 5, QUERY_CAPTURE_MODE = AUTO)
GO
I also ran all these, having referenced the link below, DB context is MyDB:
https://learn.microsoft.com/en-us/sql/relational-databases/performance/best-practice-with-the-query-store?view=sql-server-2017
ALTER DATABASE MyDB SET QUERY_STORE = ON;
SELECT actual_state_desc, desired_state_desc, current_storage_size_mb,
max_storage_size_mb, readonly_reason, interval_length_minutes,
stale_query_threshold_days, size_based_cleanup_mode_desc,
query_capture_mode_desc
FROM sys.database_query_store_options;
ALTER DATABASE MyDB SET QUERY_STORE CLEAR;
-- Run together...
ALTER DATABASE MyDB SET QUERY_STORE = OFF;
GO
EXEC sp_query_store_consistency_check
GO
ALTER DATABASE MyDB SET QUERY_STORE = ON;
GO
No issues found. The SELECT returns matching Actual and Desired states.
I am a sysadmin role member, who actually sets up all 30+ production servers, and this is the only miscreant.
The server is under heavy load and I need internal-eyes on it, in addition to Solarwinds DPA. I've also run sp_blitzquerystore but it returns an empty rowset from the top query, and just the two priority 255 rows from the 2nd.
What on earth did I do wrong? Any clues, anyone, please?
I know this is an old post but for those who come here looking for answers: I do see you ran the query with OPERATION_MODE = READ_ONLY. This would put it into a read-only mode - a mode in which it only reads what is stored in the query store without collecting any additional information. There will be no information shown if the query store has never been in READ_WRITE mode.
If it has been in READ_WRITE mode before and you are still not seeing anything, it is possible that the heavy load on the server is pushing query plans out of the cache.

Why does 5082 SQL Error happen? "Cannot change the versioning state on database ..."

Can't find any info on the net about this error. I have the following script:
USE [master]
GO
IF NOT EXISTS(SELECT * FROM sys.databases WHERE name='WebPlatform')
BEGIN
CREATE DATABASE [WebPlatform]
ALTER DATABASE [WebPlatform] SET COMPATIBILITY_LEVEL = 100
IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled'))
BEGIN
EXEC [WebPlatform].[dbo].[sp_fulltext_database] #action = 'enable'
END
ALTER DATABASE [WebPlatform] SET ANSI_NULL_DEFAULT OFF,
ANSI_NULLS OFF,
ANSI_PADDING OFF,
ANSI_WARNINGS OFF,
ARITHABORT OFF,
AUTO_CLOSE OFF,
AUTO_CREATE_STATISTICS ON ,
AUTO_SHRINK OFF ,
AUTO_UPDATE_STATISTICS ON ,
CURSOR_CLOSE_ON_COMMIT OFF ,
CURSOR_DEFAULT GLOBAL ,
CONCAT_NULL_YIELDS_NULL OFF ,
NUMERIC_ROUNDABORT OFF ,
QUOTED_IDENTIFIER OFF ,
RECURSIVE_TRIGGERS OFF ,
ENABLE_BROKER ,
AUTO_UPDATE_STATISTICS_ASYNC OFF ,
DATE_CORRELATION_OPTIMIZATION OFF ,
TRUSTWORTHY OFF ,
ALLOW_SNAPSHOT_ISOLATION OFF ,
PARAMETERIZATION SIMPLE ,
READ_COMMITTED_SNAPSHOT OFF ,
HONOR_BROKER_PRIORITY OFF ,
READ_WRITE ,
RECOVERY FULL ,
MULTI_USER ,
PAGE_VERIFY CHECKSUM ,
DB_CHAINING OFF
END
When I run it in SQL Server Management Studio I get an exception
Msg 5082, Level 16, State 1, Line 13
Cannot change the versioning state on database "WebPlatform" together with another database state.
Msg 5069, Level 16, State 1, Line 13
ALTER DATABASE statement failed.
What is wrong here? And what is versioning state? The problem is obviously in the second ALERT statement. This script was originally generated by SQL Management Server (original script was working, I just have got rid of 'GO's separating the lines changing the settings) and I wonder if I need all those lines at all? Will it hurt if I delete them?
The GOs are an indication for SSMS to send the commands to SQL Server in separate batches. What this means is that SSMS sends each segment between tow GO lines as a separate batch.
There are some commands that must be the first in a batch, speacially those related with configuration changes, and DDL (Data Definition Language, i.e. sentences that create, delete, or alter some object in the database).
So you know now the reason for your error. You must leave the GOs where they're because they are important.
For more information look here: Batches, specially in the Rules for using batches section.
Multiple lines in ALTER DATABASE SET... is legal syntax. https://www.sqlservercentral.com/blogs/multiple-alter-database-set-options
The problem here is SQL server does not allow the setting of both ALLOW_SNAPSHOT_ISOLATION and READ_COMMITTED_SNAPSHOT in the same ALTER TABLE statement, as there are combinations of these settings that could be in conflict. In the MS documentation they show these as separate ALTER DATABASE commands, run serially: https://learn.microsoft.com/en-us/dotnet/framework/data/adonet/sql/snapshot-isolation-in-sql-server

Change a SQL Server 2008 R2 database collation

I have created a database and I forgot to set its collation. So all my chars are like ???? in it. The default one is SQL_Latin1_General_CP1_CI_AS and I want to change it to Persian_100_CI_AS.
I used this SQL statement:
USE master;
GO
ALTER DATABASE land_gis
COLLATE Persian_100_CI_AS ;
GO
But I get this error :
Msg 5030, Level 16, State 2, Line 1
The database could not be exclusively locked to perform the operation.
Msg 5072, Level 16, State 1, Line 1
ALTER DATABASE failed. The default collation of database 'land_gis' cannot be
set to Persian_100_CI_AS.
I can not drop and rebuild database. Is there any way to change it?
Thanks a lot
By the way, I'm using SQL Server 2008 R2.
Seems like your database is in use - if you're confident that you'll do no harm - you can use this code to get exclusive access:
USE master;
GO
ALTER DATABASE land_gis
SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
GO
ALTER DATABASE land_gis
COLLATE Persian_100_CI_AS ;
GO
Then your ALTER DATABASE COLLATE call should work .
WARNING: this call to SET SINGLE_USER WITH ROLLBACK IMMEDIATE will disconnect anyone who might be connected to that database without warning, without chance of saving data! USE WITH CAUTION! especially in production environments!!
You can use sp_lock then find the spid associated with concerned DB and kill them (but first you can check them whats running behind them by
DBCC inputbuffer(spid))
USE master;
GO
ALTER DATABASE land_gis
SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
GO
ALTER DATABASE land_gis
COLLATE Persian_100_CI_AS ;
GO

Error In Database Renaming

I need to rename one of my database and tried a query like this
ALTER DATABASE Test MODIFY NAME = NewTest
But this throws an error
Msg 5030, Level 16, State 2, Line 1
The database could not be exclusively locked to perform the operation.
Can any one give me any suggestion?
Try something like;
USE master
GO
ALTER DATABASE Test
SET SINGLE_USER
WITH ROLLBACK IMMEDIATE
GO
ALTER DATABASE Test MODIFY NAME = NewTest
GO
ALTER DATABASE NewTest
SET MULTI_USER
GO
Be aware of the fact that this may not rename the physical file on the hard drive though.
There are a couple of things you need to investigate. The reason you're getting that error could be due to one or more of the following things:
The account you're using does not have permission to run the command
The DB is locked by another process/user
The database contains a file group that is read-only
You can try and force single user mode to check 2.
ALTER DATABASE SINGLE_USER ROLLBACK IMMEDIATE.
That will kill any concurrent connections to the DB allowing you to rule out number two.
You have two options:
Look for and kill all connections like OMG Priories suggest
Open the database in Single Server Mode as described here:
http://technet.microsoft.com/en-us/library/ms345598(v=sql.105).aspx

UPDATE failed because the following SET options have incorrect settings: 'QUOTED_IDENTIFIER'

I am having a problem with an update stored procedure. The error is:
UPDATE failed because the following SET options have incorrect settings: 'QUOTED_IDENTIFIER'. Verify that SET options are correct for use with indexed views and/or indexes on computed columns and/or query notifications and/or xml data type methods.
SQL State: 42000
Native Error: 1934
Unfortunately, there are no indexed views, computed columns, or query notifications for this table. This Stored Procedure was running fine for past couple of days and since today has been returning this error.
Is there any suggestion that would help in identifying the problem?
Note: If I set the quoted_identifier to ON and rerun the CREATE PROCEDURE, the issue will be fixed (for now). But I want to understand what triggered this issue in the first place.
To avoid that error, I needed to add
SET ANSI_NULLS, QUOTED_IDENTIFIER ON;
for all my stored procs editing a table with a computed column.
You don't need to add the SET inside the proc, just use it during creation, like this:
SET ANSI_NULLS, QUOTED_IDENTIFIER ON;
GO
CREATE PROCEDURE dbo.proc_myproc
...
I got this error when I tried to run an sql file via the command line with sqlcmd:
sqlcmd -i myfile.sql
By default QUOTED_IDENTIFIER is set to OFF when using this command line tool and you will get the same error (no matter that in the SSMS it may be set to ON and the same script will pass).
So indeed the solution is to add this QUOTED_IDENTIFIER ON to your sql file like Jim suggested, or explicitly specify the flag -I:
sqlcmd -i myfile.sql -I
We cannot create a indexed view by setting the quoted identifier off. I just tried it and SQL 2005 throws an error straight away if it is turned off:
Cannot create index. Object 'SmartListVW' was created with the following SET options off: 'QUOTED_IDENTIFIER'.
As gbn said, rebuilding the indexes must be the only other way it got turned off.
I have seen lots of articles saying it must be on before creating index on views. Otherwise you would get an error while inserting, updating the table, but here I can get the error straight away, so sql engine won't allow to create index on views by setting it to off, per this msdn link.
I have asked a similar question here in stack sometime ago...
EDIT
I turned off the global queryexecution (in editor) ANSI settings and ran the index script in new editor, this time also it throws the same error. So it's clear we can't create indexes on views by turning off quoted_identifier.
I'm late to this party but had this error and wanted to share it.
Our problem was recurrent but random so we knew it wasn't an object that had been created incorrectly.
We finally tracked it down to an ODBC connection on one of the servers in our Citrix farm. On that server, the ODBC in question had had its QUOTED_IDENTIFIERS turned off (unchecked). On all the other servers, it was checked as expected. We turned the option on and the problem was instantly solved.
I got this error when I run SQL Agent Job, which has 3 steps T-sql scripts.
Msg 1934, Sev 16, State 1, Line 15 : UPDATE failed because the
following SET options have incorrect settings: 'QUOTED_IDENTIFIER'.
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. [SQLSTATE 42000]
I added
SET ANSI_NULLS, QUOTED_IDENTIFIER ON; to the top of the Agent Job and that solved the issue.
Some thoughts:
Did indexes get rebuilt? If you do index maintenance using DMO, then quoted_identifier will not always be preserved. It can be a pain to track down and was a particular problem is SQL Server 2000 until SP4 or so.
However, I've seen on SQL Server 2005 some time ago too.
SELECT
OBJECT_NAME (sm.object_id) AS [Name],
sm.uses_ansi_nulls,
sm.uses_quoted_identifier,
N'SET ANSI_NULLS, QUOTED_IDENTIFIER ON;
--change the below CREATE to an ALTER.
GO
' + sm.definition AS PossibleFixingStatement
FROM
sys.sql_modules AS sm
WHERE
1 = 1
AND
(
sm.uses_ansi_nulls <> 1
OR sm.uses_quoted_identifier <> 1
)
AND NOT EXISTS
(
SELECT
*
FROM
sys.objects AS o
WHERE
o.is_ms_shipped = 1
AND o.object_id = sm.[object_id]
)
ORDER BY
sm.uses_ansi_nulls,
sm.uses_quoted_identifier;
Query to identify the affected objects. Part of the sp_blitz procedure mentioned here at https://github.com/BrentOzarULTD/SQL-Server-First-Responder-Kit/issues/1698
I got this error today running a stored procedure in SSMS. Disconnecting from the server and reconnecting with a new session solved the problem for me. The SP I was running had never had this problem before.
I got the same error running this query in the Job Scheduler SQL Server Agent
UPDATE [Order]
SET OrderStatusID = 100
WHERE OrderStatusID = 200
AND OrderID IN (
[...]
)
I solved removing the [ ] characters from [Order]:
UPDATE Order
SET OrderStatusID = 100
WHERE OrderStatusID = 200
AND OrderID IN (
[...]
)
No more errors
I got the same error, had to add a couple of settings to get it resolved:
SET ANSI_NULLS ON;
SET ANSI_PADDING ON;
SET ANSI_WARNINGS ON;
SET ARITHABORT ON;
SET CONCAT_NULL_YIELDS_NULL ON;
SET NUMERIC_ROUNDABORT OFF;
SET QUOTED_IDENTIFIER OFF;
SET NOCOUNT ON;