MSSQL Archives - Topic Street https://www.topicstreet.com/category/mssql/ Topic Street is a platform where readers find useful topics , tools and technology latest news, technical support, technical skills , technical information. Sun, 13 Feb 2022 13:08:33 +0000 en-US hourly 1 https://wordpress.org/?v=6.9.1 https://i0.wp.com/www.topicstreet.com/wp-content/uploads/2022/08/cropped-cropped-TopicStreet-1-e1713715422623.png?fit=32%2C32&ssl=1 MSSQL Archives - Topic Street https://www.topicstreet.com/category/mssql/ 32 32 197729668 Microsoft SQL Server Database Rebuild Index Script https://www.topicstreet.com/microsoft-sql-server-database-rebuild-index-script/?utm_source=rss&utm_medium=rss&utm_campaign=microsoft-sql-server-database-rebuild-index-script Fri, 11 Feb 2022 20:29:36 +0000 https://topicstreet.com/?p=27   SQL Server script to rebuild all indexes for all tables in database Purpose of index rebuilding Rebuilding an index

The post Microsoft SQL Server Database Rebuild Index Script appeared first on Topic Street.

]]>
 

SQL Server script to rebuild all indexes for all tables in database

Purpose of index rebuilding

Rebuilding an index means deleting the old index replacing it with a new index. Performing an index rebuild eliminates fragmentation, compacts the pages based on the existing fill factor setting to reclaim storage space, and also reorders the index rows into contiguous pages.

When we do index rebuilding

Microsoft recommends fixing index fragmentation issues by rebuilding the index if the fragmentation percentage of the index exceeds 30%


The below script will work with SQL Server 2005 and later versions.
The script below allows you to rebuild indexes for all tables within a database. 




Use [DBNAME] -- Change Database name
Go

SELECT
'Alter INDEX [' +dbindexes.[name] +'] ON ' + dbtables.[name] + ' REBUILD  ' FragmentedIndexes,
dbschemas.[name] as 'Schema',
dbtables.[name] as 'Table',
dbindexes.[name] as 'Index',
indexstats.avg_fragmentation_in_percent,
indexstats.page_count,indexstats.*
FROM sys.dm_db_index_physical_stats (DB_ID(), NULL, NULL, NULL, NULL) AS indexstats 
INNER JOIN sys.tables dbtables on dbtables.[object_id] = indexstats.[object_id] 
INNER JOIN sys.schemas dbschemas on dbtables.[schema_id] = dbschemas.[schema_id] 
INNER JOIN sys.indexes AS dbindexes ON dbindexes.[object_id] = indexstats.[object_id] AND indexstats.index_id = dbindexes.index_id 
WHERE indexstats.database_id = DB_ID() AND dbtables.[name] like '%%' and
indexstats.avg_fragmentation_in_percent>=50   ---Change if required to check less fragmentation perc
and dbindexes.[name] is not null
ORDER BY  indexstats.avg_fragmentation_in_percent desc
Go

The post Microsoft SQL Server Database Rebuild Index Script appeared first on Topic Street.

]]>
27
How to take all sql databases differential backup in azure blob storage script https://www.topicstreet.com/how-to-take-all-sql-databases-differential-backup-in-azure-blob-storage-script/?utm_source=rss&utm_medium=rss&utm_campaign=how-to-take-all-sql-databases-differential-backup-in-azure-blob-storage-script Fri, 11 Feb 2022 20:28:20 +0000 https://topicstreet.com/?p=25  How to take all sql databases backup in azure blob storage script Below Script will help you take differential backup

The post How to take all sql databases differential backup in azure blob storage script appeared first on Topic Street.

]]>
 How to take all sql databases backup in azure blob storage script

Below Script will help you take differential backup of all online databases to azure blob storage..

 
First create a SQL Server credential (If not created earlier), follow these steps.

  1. Connect to SQL Server Management Studio.

  2. Open a new query window and connect to the SQL Server instance of the database engine.

  3. In the new query window, paste the CREATE CREDENTIAL statement with the shared access signature.

/* Example:
USE master  
CREATE CREDENTIAL [https://[StorageAccountname].blob.core.windows.net/[containerName]] ---replace with actual storage account name and container including "[]"
WITH IDENTITY='SHARED ACCESS SIGNATURE'   
, SECRET = 'sharedaccesssignature' 
GO */

USE master  
CREATE CREDENTIAL [https://[StorageAccountname].blob.core.windows.net/[containerName]] ---replace with actual storage account name and container including "[]"
  -- this name must match the container path, start with https and must not contain a forward slash at the end
WITH IDENTITY='SHARED ACCESS SIGNATURE' 
  -- this is a mandatory string and should not be changed   
 , SECRET = 'sharedaccesssignature' 
   -- this is the shared access signature key that you obtained from azure account.   
GO
4. To see all available credentials, you can run the following statement in a query window connected to your instance:

SELECT * from sys.credentials
Then finally execute below script to take all differential backup:

DECLARE @name VARCHAR(max) -- database name 
DECLARE @path VARCHAR(max) -- azure blob storage path for backup files 
DECLARE @fileName VARCHAR(max)   -- filename for backup 
DECLARE @fileDate VARCHAR(max) -- used for file name 
Declare @SQL VARCHAR(max)


SET @path = N'https://[StorageAccountname].blob.core.windows.net/[containerName]/'  ---replace with actual storage account name and container including "[]"

set @SQL = ''

DECLARE db_cursor CURSOR FAST_FORWARD     FOR
SELECT name ,  @path + name + '_' + REPLACE(REPLACE(REPLACE(REPLACE(CONVERT(VARCHAR, GETDATE(), 100), '  ', '_'), ' ', '_'), '-', '_'), ':', '_') + '.bak' pathfile
FROM sys.databases 
WHERE  state_desc='ONLINE'
order by name

OPEN db_cursor  
FETCH NEXT FROM db_cursor INTO @name  ,@fileName

WHILE @@FETCH_STATUS = 0  
BEGIN  

      
--print 'BACKUP DATABASE '+@name+' TO  URL = '''+@fileName+''' WITH  DIFFERENTIAL , 
--NOFORMAT, NOINIT,  NAME = ''Diff_'+@name+''', NOSKIP, NOREWIND, NOUNLOAD, COMPRESSION,  STATS = 10'
 
 
-- print '---' +@name +'---Start ------------------------'

exec('BACKUP DATABASE '+@name+' TO  URL = '''+@fileName+''' WITH  DIFFERENTIAL , 
NOFORMAT, NOINIT,  NAME = ''Diff_'+@name+''', NOSKIP, NOREWIND, NOUNLOAD, COMPRESSION,  STATS = 10' )
 
--  print '----------END---------------------'
 

      FETCH NEXT FROM db_cursor INTO @name ,@fileName
END 

CLOSE db_cursor  
DEALLOCATE db_cursor 

--print @SQL

The post How to take all sql databases differential backup in azure blob storage script appeared first on Topic Street.

]]>
25