DECLARE @Database VARCHAR(255);
DECLARE @Table VARCHAR(255);
DECLARE @cmd NVARCHAR(500);
DECLARE @fillfactor INT;
SET @fillfactor = 80;
DECLARE DatabaseCursor CURSOR FOR
SELECT name
FROM sys.databases WITH ( NOLOCK )
WHERE name NOT IN ( 'master', 'msdb', 'tempdb', 'model', 'distribution' )
AND state_desc = 'ONLINE'
ORDER BY 1;
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
IF ( @@MICROSOFTVERSION / POWER(2, 24) >= 9 )
BEGIN
-- SQL 2005 or higher command
SET @cmd = 'ALTER INDEX ALL ON ' + @Table
+ ' REBUILD WITH (FILLFACTOR = '
+ CONVERT(VARCHAR(3), @fillfactor) + ')';
EXEC ( @cmd );
END;
ELSE
BEGIN
-- SQL 2000 command
DBCC DBREINDEX(@Table, ' ', @fillfactor);
END;
FETCH NEXT FROM TableCursor
INTO @Table;
END;
CLOSE TableCursor;
DEALLOCATE TableCursor;
FETCH NEXT FROM DatabaseCursor
INTO @Database;
END;
CLOSE DatabaseCursor;
DEALLOCATE DatabaseCursor;
Showing posts with label SQL Server. Show all posts
Showing posts with label SQL Server. Show all posts
Wednesday, July 26, 2017
Rebuild All Index of All Table of All Database - SQL Server
To Rebuild all Indexes of all Tables of all databases can be done using this code.
Monday, August 11, 2014
Fix User Permission in SQL Server
After the restore, it is needed to fix the previous user permissions in SQL Server Database.
EXEC sp_change_users_login 'Auto_Fix', 'webuser'Here webuser is the user name.
Enable Database Mail in SQL Server
Enable the db mail feature at server level:
Enable service broker in the MSDB database:
sp_configure 'Database Mail XPs', 1 reconfigure
Enable service broker in the MSDB database:
USE [master] GO ALTER DATABASE [MSDB] SET ENABLE_BROKER WITH NO_WAIT GO
Enable 'xp_cmdshell' SQL Server
-- To allow advanced options to be changed. EXEC sp_configure 'show advanced options', 1 GO -- To update the currently configured value for advanced options. RECONFIGURE GO -- To enable the feature. EXEC sp_configure 'xp_cmdshell', 1 GO -- To update the currently configured value for this feature. RECONFIGURE GO
Subscribe to:
Posts (Atom)