Description of the issue
In IndexOptimize there's this code:
SET @CurrentDatabase_sp_executesql = QUOTENAME(@CurrentDatabaseName) + '.sys.sp_executesql'
IF @ExecuteAsUser IS NOT NULL
BEGIN
SET @CurrentCommand = ''
SET @CurrentCommand += 'IF EXISTS(SELECT * FROM sys.database_principals database_principals WHERE database_principals.[name] = @ParamExecuteAsUser) BEGIN SET @ParamExecuteAsUserExists = 1 END ELSE BEGIN SET @ParamExecuteAsUserExists = 0 END'
EXECUTE @CurrentDatabase_sp_executesql @stmt = @CurrentCommand, @params = N'@ParamExecuteAsUser sysname, @ParamExecuteAsUserExists bit OUTPUT', @ParamExecuteAsUser = @ExecuteAsUser, @ParamExecuteAsUserExists = @CurrentExecuteAsUserExists OUTPUT
END
If you have AG databases which are set to not allow read only connections, the above will fail.
The target database, '...', is participating in an availability group and is currently not accessible for queries. Either data movement is suspended or the availability replica is not enabled for read access. To allow read-only access to this and other databases in the availability group, enable read access to one or more secondary availability replicas in the group.
This is because the code is a little too far above the Updateability READ_WRITE checks. If you add an extra line:
SET @CurrentDatabase_sp_executesql = QUOTENAME(@CurrentDatabaseName) + '.sys.sp_executesql'
IF @ExecuteAsUser IS NOT NULL
AND DATABASEPROPERTYEX(@CurrentDatabaseName, 'Updateability') = 'READ_WRITE'
BEGIN
SET @CurrentCommand = ''
SET @CurrentCommand += 'IF EXISTS(SELECT * FROM sys.database_principals database_principals WHERE database_principals.[name] = @ParamExecuteAsUser) BEGIN SET @ParamExecuteAsUserExists = 1 END ELSE BEGIN SET @ParamExecuteAsUserExists = 0 END'
EXECUTE @CurrentDatabase_sp_executesql @stmt = @CurrentCommand, @params = N'@ParamExecuteAsUser sysname, @ParamExecuteAsUserExists bit OUTPUT', @ParamExecuteAsUser = @ExecuteAsUser, @ParamExecuteAsUserExists = @CurrentExecuteAsUserExists OUTPUT
END
Then the problem goes away. But you probably want to reshuffle things instead.
SQL Server version and edition
Microsoft SQL Server 2019 (RTM-CU18) (KB5017593) - 15.0.4261.1 (X64)
Sep 12 2022 15:07:06
Copyright (C) 2019 Microsoft Corporation
Enterprise Edition: Core-based Licensing (64-bit) on Windows Server 2016 Standard 10.0 (Build 14393: )
Version of the script
2022-01-02 13:58:13
and
2022-12-03 17:23:44
The target database, '...', is participating in an availability group and is currently not accessible for queries. Either data movement is suspended or the availability replica is not enabled for read access. To allow read-only access to this and other databases in the availability group, enable read access to one or more secondary availability replicas in the group.
Description of the issue In IndexOptimize there's this code:
If you have AG databases which are set to not allow read only connections, the above will fail.
This is because the code is a little too far above the Updateability READ_WRITE checks. If you add an extra line:
Then the problem goes away. But you probably want to reshuffle things instead.
SQL Server version and edition Microsoft SQL Server 2019 (RTM-CU18) (KB5017593) - 15.0.4261.1 (X64) Sep 12 2022 15:07:06 Copyright (C) 2019 Microsoft Corporation Enterprise Edition: Core-based Licensing (64-bit) on Windows Server 2016 Standard 10.0 (Build 14393: )
Version of the script 2022-01-02 13:58:13
and 2022-12-03 17:23:44
What command are you executing?
What output are you getting?