Tag: SQL Server 2005

  • SQL Server – Backup Verification Script – Based on conditions

    Problem: There are scenarios where you have to verify the multiple SQL Server instances backup within limited time frame whether it meet your organization backup criteria standards or not.

    Solution:
    You can execute below T-SQL script to check the backup status as per your organization standard by assigning backup conditions value to backup criteria variable. You can execute below script either by connecting each SQL Server Instance or execute it by registering all the SQL Server Instances in Central Management Server (CMS).

    Below T-SQL Script work with all the versions of MSSQL Server and verify FULL, Differential and Transaction Log Backup. It will check the max backup date and evaluate result based on condition as backup is Passed or Not.

    Backup Verification Script will return below values. Column names are self-explanatory.
    • SQLInstanceName
    • DatabaseName
    • db_create_ddate
    • DB_Recovery_Model
    • dbstatus
    • windowsservername
    • productversion
    • productlevel
    • edition
    • current_datetime
    • last_full_backup_date
    • last_diff_backup_date
    • last_tran_backup_date
    • days_since_last_full_backup
    • days_since_last_diff_backup
    • hours_since_last_tranlog_backup
    • Full_Backup_Stauts
    • Diff_Backup_Stauts
    • Log_Backup_Stauts
    • full_backup_location
    • diff_backup_location
    • tlog_backup_location

    You have to specify the backup condition based on your organization standard by assigning values to below variables.

    <br />
    declare @full_backup_criteria_in_days int<br />
    declare @diff_backup_criteria_in_days int<br />
    declare @log_backup_criteria_in_hours int</p>
    <p>-- specify the backup criteria<br />
    set @full_backup_criteria_in_days = 7 -- 7 day older Full Backup is Pass<br />
    set @diff_backup_criteria_in_days = 1 -- 1 day older Diff Backup is Pass<br />
    set @log_backup_criteria_in_hours = 2 -- 2 hours older Log Backup is Pass<br />
    

    Complete Backup Verification Script

    <br />
    declare @full_backup_criteria_in_days int<br />
    declare @diff_backup_criteria_in_days int<br />
    declare @log_backup_criteria_in_hours int</p>
    <p>-- specify the backup criteria<br />
    set @full_backup_criteria_in_days = 7 -- 7 day older Full Backup is Pass<br />
    set @diff_backup_criteria_in_days = 1 -- 1 day older Diff Backup is Pass<br />
    set @log_backup_criteria_in_hours = 2 -- 2 hours older Log Backup is Pass</p>
    <p>select<br />
               serverproperty('servername') as SQLInstanceName,<br />
               quotename(bkup_full.[database_name]) as DatabaseName,<br />
                ( select    sdb.crdate<br />
                  from      [master]..[sysdatabases] sdb<br />
                  where     sdb.name = bkup_full.[database_name]<br />
                ) as [db_create_ddate],<br />
               databasepropertyex(bkup_full.[database_name],'recovery')  as DB_Recovery_Model,<br />
               databasepropertyex(bkup_full.[database_name],'status')  as dbstatus,<br />
               case serverproperty('isclustered')<br />
                            when 1 then cast(serverproperty('computernamephysicalnetbios') as varchar)<br />
                            when 0 then cast(serverproperty('machinename') as varchar)<br />
               end as windowsservername,<br />
               serverproperty('productversion') as productversion,<br />
               serverproperty('productlevel') as productlevel,<br />
               serverproperty('edition') as edition,</p>
    <p>           current_timestamp as current_datetime,</p>
    <p>		   bkup_full.[backup_finish_date] as [last_full_backup_date],<br />
    		   bkup_diff.[backup_finish_date] as [last_diff_backup_date] ,<br />
    		   bkup_log.[backup_finish_date] as [last_tran_backup_date] ,</p>
    <p>           datediff(dd, bkup_full.[backup_finish_date], current_timestamp) as [days_since_last_full_backup] ,<br />
               datediff(dd, bkup_diff.[backup_finish_date], current_timestamp) as [days_since_last_diff_backup] ,<br />
               datediff(hh, bkup_log.[backup_finish_date], current_timestamp) as [hours_since_last_tranlog_backup] ,</p>
    <p>           case<br />
                            when datediff(dd, bkup_full.[backup_finish_date], current_timestamp) &lt;= @full_backup_criteria_in_days<br />
                            then 'Pass'<br />
                            else 'Fail'<br />
                            end as Full_Backup_Stauts,</p>
    <p>            case<br />
                            when datediff(dd, bkup_diff.[backup_finish_date], current_timestamp) &lt;= @diff_backup_criteria_in_days  then 'Pass'<br />
                            else case when quotename(bkup_full.[database_name]) IN ('[master]') then 'N/A' else 'Fail' end<br />
                            end as Diff_Backup_Stauts,</p>
    <p>            case<br />
                            when datediff(hh, bkup_log.[backup_finish_date], current_timestamp) &lt;= @log_backup_criteria_in_hours  then 'Pass'<br />
                            else case when databasepropertyex(bkup_full.[database_name],'recovery') = 'SIMPLE' then 'N/A' else 'Fail' end<br />
                end as Log_Backup_Stauts,            </p>
    <p>            ( select top 1 [physical_device_name]<br />
                  from      [msdb]..[backupmediafamily] bkup_media_family<br />
                  where     bkup_media_family.[media_set_id] = bkup_full.[media_set_id]<br />
                ) as [full_backup_location] ,<br />
                ( select top 1  [physical_device_name]<br />
                  from      [msdb]..[backupmediafamily] bkup_media_family<br />
                  where     bkup_media_family.[media_set_id] = bkup_diff.[media_set_id]<br />
                ) as [diff_backup_location] ,<br />
                ( select top 1  [physical_device_name]<br />
                  from      [msdb]..[backupmediafamily] bkup_media_family<br />
                  where     bkup_media_family.[media_set_id] = bkup_log.[media_set_id]<br />
                ) as [tlog_backup_location]</p>
    <p>    from    [msdb]..[backupset] as bkup_full<br />
                left join [msdb]..[backupset] as bkup_log on bkup_log.[database_name] = bkup_full.[database_name]<br />
                                                           and bkup_log.[server_name] = bkup_full.[server_name]<br />
                                                           and bkup_log.[type] = N'L'<br />
                                                           and bkup_log.[backup_finish_date] = ( (select  max([backup_finish_date])<br />
                                                                                                from    [msdb]..[backupset] b2<br />
                                                                                                where   b2.[database_name] = bkup_full.[database_name]<br />
                                                                                                        and b2.[server_name] = bkup_full.[server_name]<br />
                                                                                                        and b2.[type] = N'L') )<br />
                left join [msdb]..[backupset] as bkup_diff on bkup_diff.[database_name] = bkup_full.[database_name]<br />
                                                           and bkup_diff.[server_name] = bkup_full.[server_name]<br />
                                                           and bkup_diff.[type] = N'I'<br />
                                                           and bkup_diff.[backup_finish_date] = ( (select max([backup_finish_date])<br />
                                                                                                from    [msdb]..[backupset] b2<br />
                                                                                                where   b2.[database_name] = bkup_full.[database_name]<br />
                                                                                                        and b2.[server_name] = bkup_full.[server_name]<br />
                                                                                                         and b2.[type] = N'I') )<br />
        where   bkup_full.[type] = N'D'<br />
                and bkup_full.[backup_finish_date] = ( (select  max([backup_finish_date])<br />
                                                     from   [msdb]..[backupset] b2<br />
                                                     where  b2.[database_name] = bkup_full.[database_name]<br />
                                                            and b2.[server_name] = bkup_full.[server_name]<br />
                                                            and b2.[type] = N'D') )<br />
                and exists ( select [name]<br />
                             from   [master]..[sysdatabases]<br />
                             where  [name] = bkup_full.[database_name] )<br />
                and bkup_full.[database_name] &lt;&gt; N'tempdb'<br />
    

    This script is the “Sustain” dimension

    A backup you haven’t verified is a hope, not a plan — and this script is exactly the kind of continuous, boring verification that keeps a data platform honest. In my AI risk framework, this is Sustain: monitor and govern, so the other controls hold up in production. The framework: R-I-S-K-S: The 5-Dimension Framework for AI Risk Management

  • Service Principle Name

    SPN (Service Principle Name)

    What is SPN? A service principal name (SPN) is the name by which a client uniquely identifies an instance of a service. The Kerberos authentication service can use an SPN to authenticate a service. When a client wants to connect to a service, it locates an instance of the service, composes an SPN for that instance, connects to the service, and presents the SPN for the service to authenticate.

    How the SPN works or what is the internal process of SQL Server to register SPN?When SQL Server service is started, it first checks if SPN exists and if no SPN found calls API to create new SPN. At the shutdown service sends request to delete the SPN. 

    What if SQL Server is running under local administrator account? Can it register SPN?No, it cannot. The account must have domain administrator rights to register the SPN 

    Can we register SPN for other services as well? Yes, you can register the SPN for the other services as well. (i.e Analysis Service) 

    How can I see the SPN for my different SQL Boxes? First you need to download the SPN tool from below URL http://www.microsoft.com/downloads/details.aspx?FamilyId=6EC50B78-8BE1-4E81-B3BE-4E7AC4F0912D&displaylang=en

     How to use this utility, what are the commands available for it?Please find the SPN Syntax as below.

    Syntax setspn [switches data] computername 

     Note: Computer Name can be the name or domain\name

     Switches:
       -R = reset HOST ServicePrincipalName
        Usage:   setspn -R computername
       -A = add arbitrary SPN
        Usage:   setspn -A SPN computername
       -D = delete arbitrary SPN
        Usage:   setspn -D SPN computername
       -L = list registered SPNs
        Usage:   setspn [-L] computername

    Examples:
    setspn -R daserver1
       It will register SPN “HOST/daserver1 and “HOST/{DNS of daserver1}”
    setspn -A http/daserver daserver1
       It will register SPN “http/daserver” for computer “daserver1”
    setspn -D http/daserver daserver1
       It will delete SPN “http/daserver” for computer “daserver1”

    How should I use this utility SQL Server Services?You can use below commands to register different services 

    To Register SQL Server Database Service

    SetSPN –A MSSQLSvc/serverHostName.Fully_Qualified_domainName:[TCP Port Number] [Account Name]

    Note: You can use the same command Named Instance as well

    To Register Analysis Service

    Setspn.exe -A MSOLAPSvc.3/serverHostName.Fully_Qualified_domainName OLAP_Service_Startup_Account

    How to check whether SQL Server Services are registered or not?

    You can use below command.

    SetSPN –L [AccountName]

    What happens if SQL Server Service fails to create SPN during startup?

    If the SQL Server Service is failed to create and register SPN, it will log the error in the error log during startup. Moreover you will get the below type of issues from client side

    You will get the connectivity issues from client side as below

    Error: 18456, Severity: 14, State: 11.
    Login failed for user ‘NT AUTHORITY\ANONYMOUS LOGON’.

    How to check after SPN registration SQL Server Kerberos authentication?

    Check the auth_scheme column by executing below query

    select auth_scheme from sys.dm_exec_connections where session_id=@@spid;

    What are the best practices for SPN in cluster environment? In cluster server configure both nodes to use same network DC

  • An error has occurred while establishing a connection to the server

    Problem:

    An error has occurred while establishing a connection to the server. When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections. (provider: SQL Network Interfaces, error: 28 – Server doesn’t support requested protocol) (Microsoft SQL Server, Error: -1)

    Resolution:

    Check the below setting at Server Level
    1. Check the firewall for any blocking (i.e TCP/IP Port:1433)
    2. Check the SQL Server Instance Name, there may be chances of the spelling mistakes
    3. May be remote connection is disable. Use the SAC (SQL Server Surface area configuration tool for remote connection)
    4. Check the status of SQL Browser Service, it should be running to allow remote connectivity

  • Execute As in SQL Server 2005

    When an EXECUTE AS statement is run, the execution context of the session is switched to the specified login or user name. This will help database administrator to check the different user permissions. `Execute As` is also very helpful when a user wants to execute a stored procedure in the context of another user.

    Syntax
    Execute as user = ‘SQL_JShah’
    select * from CompanyProducts.Products

  • Datbase Snapshot

    What is database snapshot?
    A database snapshot provides a read-only, static view of a source database as it existed at snapshot creation, minus any uncommitted transactions. Uncommitted transactions are rolled back in a newly created database snapshot because the Database Engine runs recovery after the snapshot has been created. 

     

    Is database snapshot transitionally consistent?
    Yes.
    Each database snapshot is transitionally consistent with the source database at the moment of the snapshot’s creation. When we create a database snapshot, the source database will typically have number open transactions. Before the snapshot becomes available, the open transactions are rolled back to make the database snapshot transitionally consistent. Just like it follows the recovery interval step and it will not affect the source database. 

     

    What are the uses of database snapshot?
    Reporting Purpose
    we can recover damaged database using database snapshot
    also useful if we are planning to do major change in the source database
    Mirroring database is always in recovering mode, to read that database we can use database snapshot.
    Which edition of SQL Server 2005 supports database snapshot? Enterprise Edition 

     

    Is Developer edition of SQL Server 2005 supports database snapshot?
    No. 

     

    What is copy-on-write operation in database snapshot?
    Database snapshots operate at the data-page level. Before a page of the source database is modified for the first time, the original page is copied from the source database to the snapshot. This process is called a copy-on-write operation. The snapshot stores the original page, preserving the data records as they existed when the snapshot was created.

     

    What is Sparse file in Database Snapshot?

    To store the copied original pages, the snapshot uses one or more sparse files. Initially, a sparse file is an essentially empty file that contains no user data and has not yet been allocated disk space for user data. As more and more pages are updated in the source database, the size of the file grows. When a snapshot is taken, the sparse file takes up little disk space. As the database is updated over time, however, a sparse file can grow into a very large file.

     

    What is NTFS File System?

    The NTFS acronym stands for New Technology File System. The name derives from the implementation of very innovative data storage techniques that were refined in NTFS. While none of the techniques are unique to NTFS, it is the first time that so many innovations were released at once on a production file system. The FAT file system had long been criticized for not including some of the more obvious improvements such as journaling, disk quotas, and file compression. However, these improvements made NTFS incompatible with previous versions of Windows, and also with hard disk tools designed for FAT file systems. For example, data recovery tools such as GetDataBack and partitioning tools such as PartitionMagic would run on Windows NT, yet could not function on the newer file system. This led to much frustration with users who had purchased licenses for these products before upgrading to Windows NT.

     

    Explain Sparse file Size in database snapshot

    At the creation time sparse file will take very little space, but as the data changes occurred into the parent database, data page before the changes copied into the sparse file. Thus sparse file grows. Sparse files are the feature of NTFS file system. As the sparse file grows NTFS will allocate the space to sparse file gradually.

     

    Why does the size of a sparse file slightly exceed than the space actually filled by pages in it?

    Sparse files grow in 64-kilobyte (KB) increments; thus, the size of a sparse file on disk is always a multiple of 64 KB. The latest 64-KB increment holds from one to eight 8-KB pages, depending on how many pages have been copied from the source database. This means that, on the average, the size of a sparse file slightly exceeds the space actually filled by pages.

     

    How to create database snapshot?

    1. Based on the current size of the source database, ensure that you have sufficient disk space to hold the database snapshot. The maximum size of a database snapshot is the size of the source database at snapshot creation.
    2. Issue a CREATE DATABASE statement on the files using the AS SNAPSHOT OF clause. Creating a snapshot requires specifying the logical name of every database file of the source database.

     

    Syntax

    CREATE DATABASE AdventureWorks_SS ON

    (

    NAME = AdventureWorks_Data,

    FILENAME = ‘D:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\AdventureWorks_Data.ss’

    )

    AS SNAPSHOT OF AdventureWorks