Restore PRIMARY Filegroup only on a database using full backup

-> I had to restore a database of size 26 TB in my Development server. This database had 2 filegroups namely PRIMARY and BLOB. Primary Filegroup has tables of size 3 TB and BLOB filegroup has tables of size 23 TB.

-> My requirement for this restore was to just restore PRIMARY filegroup and not BLOB filegroup.

-> Below TSQL can be used to restore PRIMARY Filegroup only from the full database backup.


USE [master]
RESTORE DATABASE [JB_DB] FILEGROUP='PRIMARY'
FROM  DISK = N'C:\DB\JB_DB_01of06.bak',
DISK = N'C:\DB\JB_DB_02of06.bak',
DISK = N'C:\DB\JB_DB_03of06.bak',
DISK = N'C:\DB\JB_DB_04of06.bak',
DISK = N'C:\DB\JB_DB_05of06.bak',
DISK = N'C:\DB\JB_DB_06of06.bak'
WITH PARTIAL, RECOVERY, FILE = 1, NOUNLOAD, STATS = 1, REPLACE
GO

Thank You,
Vivek Janakiraman

Disclaimer:
The views expressed on this blog are mine alone and do not reflect the views of my company or anyone else. All postings on this blog are provided “AS IS” with no warranties, and confers no rights.

Advertisements

Execute job step if a job is not running

-> There are two SQL Server Agent jobs scheduled. Job1 has 6 steps and Job2 as 1 step.

-> Job1 details below,

Job Step Step Details
Step1 Changes Maxdop to 0.
Step2 Starts Job2.
Step3 Run reindexing for 3 Indexes.
Step4 Checks if Job2 is completed and waits for the Job2 to complete before proceeding to next step.
Step5 Changes Maxdop to 1.
Step6 Executes TSQL to create views.

-> Job2 details below,

Job Step Step Details
Step1 Run reindexing for 10 Indexes

-> Step4 of Job1 executes below TSQL that checks if Job2 has completed or not and wait for Job2 to complete.


DECLARE @JOB_NAME SYSNAME = N'JOBNAME';
DECLARE @JOB_NAME1 SYSNAME = N'JOBNAME'; 

Job_Status:
IF NOT EXISTS(
        select 1
        from msdb.dbo.sysjobs_view job
        inner join msdb.dbo.sysjobactivity activity on job.job_id = activity.job_id
        where
            activity.run_Requested_date is not null
        and activity.stop_execution_date is null
        and job.name IN (@JOB_NAME,@JOB_NAME1)<span id="mce_SELREST_start" style="overflow:hidden;line-height:0;"></span>
        )
BEGIN
    PRINT 'Job not running'
END
ELSE
BEGIN
    PRINT 'Job is running';
	waitfor delay '00:01:00'
	goto Job_Status;
END 

Thank You,
Vivek Janakiraman

Disclaimer:
The views expressed on this blog are mine alone and do not reflect the views of my company or anyone else. All postings on this blog are provided “AS IS” with no warranties, and confers no rights.

Archival Report – Getting before and after row count post data archival

-> Data archival on a SQL Server database is a common activity that needs to be performed by a SQL Server database administrator.

-> A report that contains before and after row count post Data Archival with the row count difference will be of much help.

-> Download  Archival_Report_Job.sql and execute the query using SQL Server Management Studio on database server where Archiving will be performed.  Remember to change the database name to appropriate database where Archiving will be performed in the downloaded script. This will create a job called Archival_Report and the job can be found within the jobs folder under SQL Server Agent. This will be an one time activity and should be performed on database server during initial setup.

-> The first step on job Archival_Report inserts data related to table size into table Archival_Table_Details.

-> The second step on job Archival_Report deletes data older than 7 years from table Archival_Table_Details.

-> SQL Server agent job “Archival_Report” should be executed before the start of data archival either manually or by executing below code from application scheduler agent,

sqlcmd -SSQLServerInstance -E -Q"Exec msdb..sp_start_job N'Archival_Report'"

-> Wait for data archival to complete. Once the Data Archival is complete. SQL Server agent job “Archival_Report” should be executed once again either manually or by executing below code from application scheduler agent,

sqlcmd -SSQLServerInstance -E -Q"Exec msdb..sp_start_job N'Archival_Report'"

-> Execute below query on the context of Archived database on database server where Archiving was performed. Remember to change the database name to appropriate database where Archiving will be performed in the below script. Archival report will be displayed in the output tab as part of query window in SQL Server Management Studio.


use [JBDB]
GO
;with Archival_Report_CTE as (
select TableName, [Time],[# Records],[Table_used_Space GB],row_number() Over(Partition by TableName order by Time DESC ) RowNumber
from Archival_Table_Details)

Select
Max(Case when RowNumber=1 then Cast([Time] as date) else null End) Time,
TableName
,Max(Case when RowNumber=2 then [# Records] else null End) [Before # Records]
,Max(Case when RowNumber=2 then [Table_used_Space GB] else null End) [Before_Table_used_Space GB]
, Max(Case when RowNumber=1 then [# Records] else null End) [After # Records]
, Max(Case when RowNumber=1 then [Table_used_Space GB] else null End) [After_Table_used_Space GB]
,Max(Case when RowNumber=2 then [# Records] else null End)-Max(Case when RowNumber=1 then [# Records] else null End) [# Records Difference]
From Archival_Report_CTE
where RowNumber<=2 and TableName NOT LIKE '%Archival_Table_Details%'
Group by TableName

Thank You,
Vivek Janakiraman

Disclaimer:
The views expressed on this blog are mine alone and do not reflect the views of my company or anyone else. All postings on this blog are provided “AS IS” with no warranties, and confers no rights.