Like others previously, today in my SQL logs I can see that a scheduled job has failed to execute due to Error 1934.
ALTER INDEX failed because the following SET options have incorrect settings: 'QUOTED_IDENTIFIER'
After reading here, here and here, I still feel unsure about whether my hack (below) is introducing risk based upon advice given elsewhere. My lack of experience is the main driver for this question.
The original code is:
DECLARE @Database varchar(255);
DECLARE @Table varchar(255);
DECLARE @cmd nvarchar(500);
DECLARE @fillfactor int = 90;
DECLARE DatabaseCursor CURSOR FOR
SELECT
name
FROM
MASTER.dbo.sysdatabases
WHERE
name IN ('MyDbName')
ORDER BY
name;
OPEN DatabaseCursor;
FETCH NEXT FROM DatabaseCursor INTO @Database;
WHILE (@@FETCH_STATUS = 0)
BEGIN
SET @cmd = 'DECLARE TableCursor CURSOR FOR SELECT ''['' + table_catalog + ''].['' + table_schema + ''].['' +
table_name + '']'' as tableName FROM ' + @Database + '.INFORMATION_SCHEMA.TABLES
WHERE table_type = ''BASE TABLE''';
-- create table cursor
EXEC (@cmd);
OPEN TableCursor;
FETCH NEXT FROM TableCursor INTO @Table;
WHILE (@@FETCH_STATUS = 0)
BEGIN
SET @cmd = 'ALTER INDEX ALL ON ' + @Table + ' REBUILD WITH (FILLFACTOR = ' + CONVERT(varchar(3), @fillfactor) + ')';
EXEC (@cmd);
FETCH NEXT FROM TableCursor INTO @Table;
END
CLOSE TableCursor;
DEALLOCATE TableCursor;
FETCH NEXT FROM DatabaseCursor INTO @Database;
END
CLOSE DatabaseCursor;
DEALLOCATE DatabaseCursor;
USE MyDbName;
GO
EXEC sys.sp_updatestats;
I believe that this error began occuring after an index was added to an XML column in one particular table, but I'm not 100% sure. Elsewhere we also have stored procedures that use XPATH. I know others have experienced the same error, but without the experience of manually setting QUOTED_IDENTIFIER, I'm looking for advice plus a way to improve my fix.
Based on what I've read, this revised code does execute without error:
SET @cmd = 'SET QUOTED_IDENTIFIER ON; ' +
'ALTER INDEX ALL ON ' + @Table + ' REBUILD WITH (FILLFACTOR = ' + CONVERT(varchar(3), @fillfactor) + ')';
My concerns are:
Thank you in advance for sharing your expertise.
TRY / CATCH
block, so you can log the variables' contents somewhere.QUOTED_IDENTIFIER
will always be on. However, it's not a 100% guarantee, since data access drivers can override connection settings (different drivers - ODBC, OLE DB, Native Client, etc. - have slightly different defaults).If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With