Sunday, January 10, 2016

Update/Rebuild Statistics run every sunday 3 am

DECLARE @SQL VARCHAR(1000)
DECLARE @DB sysname

DECLARE curDB CURSOR FORWARD_ONLY STATIC FOR
   SELECT [name]
   FROM master..sysdatabases
   WHERE [name]  NOT IN ('model', 'tempdb')
   ORDER BY [name]
   
OPEN curDB
FETCH NEXT FROM curDB INTO @DB
WHILE @@FETCH_STATUS = 0
   BEGIN
       SELECT @SQL = 'USE [' + @DB +']' + CHAR(13) + 'EXEC sp_updatestats' + CHAR(13)
       PRINT @SQL

       FETCH NEXT FROM curDB INTO @DB
   END
   
CLOSE curDB
DEALLOCATE curDB
--Put the code in JOB
-- No need for creating th sp in any databases, it is already exist




 Take out put and put in as a job

No comments:

Post a Comment

https://blog.sqlauthority.com/2009/06/27/sql-server-fix-error-17892-logon-failed-for-login-due-to-trigger-execution-changed-database-context...