Using QUOTED_IDENTIFIER to overcome the ALTER INDEX failed error

Like others before , today in my SQL logs I see that the scheduled task could not be completed due to error 1934.

ALTER INDEX error because the following SET parameters have incorrect settings: 'QUOTED_IDENTIFIER'

After reading here , here and here , I still feel insecure that my hack (below) introduces a risk based on tips given elsewhere. My lack of experience is the main driver for this issue.

Source:

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;

, , XML , 100%. , XPATH. , , QUOTED_IDENTIFIER .

, , :

 SET @cmd = 'SET QUOTED_IDENTIFIER ON; ' + 
            'ALTER INDEX ALL ON ' + @Table + ' REBUILD WITH (FILLFACTOR = ' + CONVERT(varchar(3), @fillfactor) + ')';

:

  • - , ( , , !)
  • , , (, / )?
  • , SQL ?

.

+4
2
  • , .
  • , TRY / CATCH, - .
  • , QUOTED_IDENTIFIER . 100%, ( - ODBC, OLE DB, Native Client .. - ).
+3

quoted_identifier ON,

SET QUOTED_IDENTIFIER ON
+2

Source: https://habr.com/ru/post/1659400/


All Articles