Files
sql-scripts/EXPLOIT - enable query store on all dbs.sql
Thierry Schork b19a4c4bdd - adapted the procedures to not loop, but only do 1 batch of deletion.
The job will be called in a loop, every minutes and will handle the enforcement of not starting during working hours.
- Added the job that will call all cleanup procs in sequence
2025-09-03 16:17:57 +02:00

25 lines
618 B
Transact-SQL

DECLARE @dbName NVARCHAR(255);
DECLARE db_cursor CURSOR FOR
SELECT name
FROM sys.databases
WHERE state_desc = 'ONLINE'
AND is_read_only = 0
AND source_database_id IS NULL
AND [database_id]>4 --ignore system dbs
AND [is_query_store_on] = 0
;
OPEN db_cursor;
FETCH NEXT FROM db_cursor INTO @dbName;
WHILE @@FETCH_STATUS = 0
BEGIN
EXEC('ALTER DATABASE [' + @dbName + '] SET QUERY_STORE = ON (OPERATION_MODE = READ_WRITE);');
PRINT 'ALTER DATABASE [' + @dbName + '] SET QUERY_STORE = ON (OPERATION_MODE = READ_WRITE);'
FETCH NEXT FROM db_cursor INTO @dbName;
END
CLOSE db_cursor;
DEALLOCATE db_cursor;