DECLARE @SQLServiceRestartDateTime AS VARCHAR(50), @ServerRestartDateTime AS VARCHAR(50) SELECT @SQLServiceRestartDateTime = create_date FROM sys.databases WHERE database_id=2 SELECT @ServerRestartDateTime = CAST(DATEADD(SECOND, (ms_ticks/1000) * -1, GETDATE()) AS datetime) FROM sys.dm_os_sys_info SELECT @SQLServiceRestartDateTime AS [SQL Service Start DateTime], @ServerRestartDateTime AS [Server Start DateTime]
Wednesday, May 25, 2022
T-SQL to find Server and SQL restart date time
This script comes handy when you would like to find out the Server and SQL services restart times.
Friday, May 20, 2022
T-SQL to find Backup or Restore Progress
Here is a script that comes handy while performing a huge database Backup or Restore. This script provides the details on the progress of the Backup or Restore operation including the estimated finish time.
SELECT session_id AS SPID, command AS [Command], a.text AS Query, start_time AS [Start Time], percent_complete AS [Percent Complete], dateadd(second,estimated_completion_time/1000, getdate()) AS [Estimated Completion Time] FROM sys.dm_exec_requests r CROSS APPLY sys.dm_exec_sql_text(r.sql_handle) a WHERE r.command like 'BACKUP%' OR r.command like 'RESTORE%')
Wednesday, June 13, 2018
Trouble in Opening MDF File because it Says SQL Error 5171? - A guest Post by Andre Williams
MS SQL Server is the most widely used and deployed database server in organizations. But, there are times when the SQL Server database gets corrupted due to various reasons. Error messages like SQL Error 1571 are also frequent with SQL Server. Let’s learn more about the SQL error 1571, its reasons and solutions.
Symptoms of SQL Error 5171:
With SQL Error 5171, you may face failures while logging in to SQL Server, restoring SQL database files, creating a tempdb database, and attaching MDF files successfully to the SQL Server database. Instead you will receive an error saying – “.mdf is not a primary database file. (Microsoft SQL server 5171)”
Possible Reasons for SQL Error 5171:
MDF file saves data in the form of pages, and each page occupies space of 8KB. The initial first page is the header page containing important database details such as signature, file size, compatibility, and much more. Rest all the other pages stores the actual data.
When the header page or related page of the file does not get recognized by the SQL Server database, it results in the SQL Error 1571 as the entire MDF is not considered to be a valid file.
Solution to Fix SQL Error 5171:
There are multiple reasons due to which SQL Error 5171 occurs. Some scenarios are mentioned below with their possible fix solution.
Scenario 1:
Usage of a mirror database in MS SQL Server by a user encounters the Error 5171, when database is set online by executing ALTER DATABASE command
Scenario 2:
When the SQL Server is upgraded to a latest or newer version, there are possible chances of Error 5171. As, you will have to first detach the database and then upgrade it to the new version. Hence, when you will try to attach it back to the MS SQL Server, it will fail to do so and error 5171 might encounter.
Below mentioned are the possible solutions for this error:
Method 1: For database mirroring
Step 1: First set, the principal database
Step 2: Use ALTER DATABASE MODIFY FILE command to modify the information.
Step 3: Now, stop the SQL server instance.
Step 4: Copy MDF and LDF files in a separate directory
Step 5: Now, restart SQL Server and attach the database files
Method 2: For attaching the database
Step 1: To troubleshoot the error, use the sp_attach_db command
Step 2: The command will attach the detached database files after upgrading is completed.
Please note: This method will work only in the case where you have used the sp_detach_db command to detach the database
Method 3: Automated Method to FIX SQL Server Error 5171
The above methods can easily remove the SQL Error 5171. However, if you still face the error after trying the above workaround methods, then you can opt for the automated solution. You can use Kernel for SQL Database Recovery tool, one of the most recommended methods by the database experts. The tool smoothly repairs and recovers all the database objects of corrupt or inaccessible MDF and NDF files. It flawlessly recovers large-sized MDF and NDF files.
Symptoms of SQL Error 5171:
With SQL Error 5171, you may face failures while logging in to SQL Server, restoring SQL database files, creating a tempdb database, and attaching MDF files successfully to the SQL Server database. Instead you will receive an error saying – “.mdf is not a primary database file. (Microsoft SQL server 5171)”

Possible Reasons for SQL Error 5171:
MDF file saves data in the form of pages, and each page occupies space of 8KB. The initial first page is the header page containing important database details such as signature, file size, compatibility, and much more. Rest all the other pages stores the actual data.
When the header page or related page of the file does not get recognized by the SQL Server database, it results in the SQL Error 1571 as the entire MDF is not considered to be a valid file.
Solution to Fix SQL Error 5171:
There are multiple reasons due to which SQL Error 5171 occurs. Some scenarios are mentioned below with their possible fix solution.
Scenario 1:
Usage of a mirror database in MS SQL Server by a user encounters the Error 5171, when database is set online by executing ALTER DATABASE command
Scenario 2:
When the SQL Server is upgraded to a latest or newer version, there are possible chances of Error 5171. As, you will have to first detach the database and then upgrade it to the new version. Hence, when you will try to attach it back to the MS SQL Server, it will fail to do so and error 5171 might encounter.
Below mentioned are the possible solutions for this error:
Method 1: For database mirroring
Step 1: First set, the principal database
Step 2: Use ALTER DATABASE MODIFY FILE command to modify the information.
Step 3: Now, stop the SQL server instance.
Step 4: Copy MDF and LDF files in a separate directory
Step 5: Now, restart SQL Server and attach the database files
Method 2: For attaching the database
Step 1: To troubleshoot the error, use the sp_attach_db command
Step 2: The command will attach the detached database files after upgrading is completed.
Please note: This method will work only in the case where you have used the sp_detach_db command to detach the database
Method 3: Automated Method to FIX SQL Server Error 5171
The above methods can easily remove the SQL Error 5171. However, if you still face the error after trying the above workaround methods, then you can opt for the automated solution. You can use Kernel for SQL Database Recovery tool, one of the most recommended methods by the database experts. The tool smoothly repairs and recovers all the database objects of corrupt or inaccessible MDF and NDF files. It flawlessly recovers large-sized MDF and NDF files.
Final Words:
You can always opt to use the manual methods for SQL recovery if you are an experienced database professional. But if you’re a naïve user of SQL database and you are not skilled enough to understand the errors of the database, then we suggest you restore your database files with the help of the automated solution.
Hope the solutions help you to resolve the issues related to MS SQL Server. If you have any queries, please mention in the comments. We will get back to you with a possible resolution.
About Andre Williams
Andre Williams, with more than three year's experience in SQL related technologies, contributes articles, blogs, and how-to tips regularly.
Social Media Profiles
Sunday, October 30, 2016
Using DBCC CLONEDATABASE to generate a schema and statistics only copy of a user database in SQL Server 2014 SP2
DBCC CLONEDATABASE is a new DBCC command introduced in SQL Server 2014 SP 2 which is used for creating the clone of a specified user database which helps in troubleshooting the performance issues related to the query optimizer.
When a clone of the database is created using DBCC CLONEDATABASE, it will create a schema and statistics only copy of the specified database and does not contain any copy of the data.
Creating the clone is as simple as passing the source database name and clone database name to the DBCC command.
Output of the DBCC Command
Once the cloning is completed, the cloned database will be in Read-Only mode.
When a clone of the database is created using DBCC CLONEDATABASE, it will create a schema and statistics only copy of the specified database and does not contain any copy of the data.
Creating the clone is as simple as passing the source database name and clone database name to the DBCC command.
DBCC CLONEDATABASE ('SansSQL', 'SansSQL_Clone')
Output of the DBCC Command
Once the cloning is completed, the cloned database will be in Read-Only mode.
SELECT name, database_id, is_read_only
FROM sys.databases
WHERE name in ('SansSQL', 'SansSQL_Clone')
It will start with few validations before the clone is created, The following validations are performed by DBCC CLONEDATABASE. The command fails if any of the validations fail.
- The source database must be a user database. Cloning of system databases (master, model, msdb, tempdb, distribution database etc.) isn't allowed.
- The source database must be online or readable.
- A database that uses the same name as the clone database must not already exist.
- The command isn't in a user transaction.
- Creating primary data file and log file
- Adding secondary dataspaces
- Adding secondary files
The destination database files will inherit the size and growth settings from the model database and the file names of the destination database will follow the source_file_name _underscore_random_number convention.
SELECT database_id, file_id, type_desc, name, physical_name
FROM sys.master_files
WHERE DB_NAME(database_id) in ('SansSQL', 'SansSQL_Clone')
Then the DBCC CLONEDATABASE will do a Internal Database Snapshot with the following steps
- Validate the source database
- Get S lock for the source database
- Create snapshot of the source database
- Create a clone database (this is an empty database which inherits from model)
- Get X lock for the clone database
- Copy the metadata to the clone database
- Release all DB locks
Using the below command, we can check if a database is a clone or a normal database.
SELECT DATABASEPROPERTYEX('SansSQL','isClone') AS SansSQL_DB_CloneStatus
,DATABASEPROPERTYEX('SansSQL_Clone','isClone') AS SansSQL_CloneDB_CloneStatus
Labels:
Clone,
DBCC CLONEDATABASE,
MSSQL,
SP2,
SQL SERVER 2014
Friday, December 26, 2014
The timeout period elapsed while attempting to consume the pre-login handshake acknowledgement.
You might get the following error when trying to connect to an newly installed SQL Server 2014 instance.
The error says "The timeout period elapsed while attempting to consume the pre-login handshake acknowledgement."
This is because the server on which SQL server is installed, is denying the client connections to SQL Server.
To fix this error, allow connections to the SQL Server Port in the firewall.
The error says "The timeout period elapsed while attempting to consume the pre-login handshake acknowledgement."
This is because the server on which SQL server is installed, is denying the client connections to SQL Server.
To fix this error, allow connections to the SQL Server Port in the firewall.
Labels:
Connection failure,
firewall,
MSSQL,
SQL SERVER 2014
Sunday, May 11, 2014
The 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine. (System.Data)
When trying to load an excel spread sheet into SQL Server you may be presented with the below error.
This occurs because the Office System Driver and Data Connectivity Components are not installed on the server and SQL Server is unable to interact with the MS Office document. For the SQL Server to interact, open and load the data from excel spreadsheet, it requires the Office System Driver and Data Connectivity Components to be installed.
To fix this issue, download and install the Office System Driver and Data Connectivity Components from the below link.
http://www.microsoft.com/en-us/download/details.aspx?id=23734
This will install a set of components that can be used to facilitate transfer of data between Microsoft Office System files and non-Microsoft Office applications.
This occurs because the Office System Driver and Data Connectivity Components are not installed on the server and SQL Server is unable to interact with the MS Office document. For the SQL Server to interact, open and load the data from excel spreadsheet, it requires the Office System Driver and Data Connectivity Components to be installed.
To fix this issue, download and install the Office System Driver and Data Connectivity Components from the below link.
http://www.microsoft.com/en-us/download/details.aspx?id=23734
This will install a set of components that can be used to facilitate transfer of data between Microsoft Office System files and non-Microsoft Office applications.
Wednesday, May 7, 2014
Create an Encrypted Backup in SQL Server 2014
Encryption for Backups is a new feature introduced in SQL Server 2014 and the benefits of this option are
Restoring the encrypted backup:
SQL Server restore does not require any encryption parameters to be specified during restores. It does require that the certificate or the asymmetric key used to encrypt the backup file be available on the instance that you are restoring to. The user account performing the restore must have VIEW DEFINITION permissions on the certificate or key. If you are restoring the encrypted backup to a different instance, you must make sure that the certificate is available on that instance.
Reference: http://msdn.microsoft.com/en-us/library/dn449489(v=sql.120).aspx
- Encrypting the database backups helps secure the data.
- Encryption can also be used for databases that are encrypted using TDE.
- Encryption is supported for backups done by SQL Server Managed Backup to Windows Azure, which provides additional security for off-site backups.
- This feature supports multiple encryption algorithms including AES 128, AES 192, AES 256, and Triple DES
- You can integrate encryption keys with Extended Key Management (EKM) providers.
The following are pre-requisites for encrypting a backup:
- Create a Database Master Key for the master database.
USE master; GO CREATE MASTER KEY ENCRYPTION BY PASSWORD = 'somepass@word123'; GO
- Create a certificate or asymmetric Key to use for backup encryption.
Use Master GO CREATE CERTIFICATE CertforBackupEncryption WITH SUBJECT = 'Certificate for Backup Encryption '; GO
Backup the database with encryption:
BACKUP DATABASE [SansSQL] TO DISK = N'C:\Backup\SansSQL.bak' WITH INIT, COMPRESSION, ENCRYPTION ( ALGORITHM = AES_256, SERVER CERTIFICATE = CertforBackupEncryption ), STATS = 10 GO
Restoring the encrypted backup:
SQL Server restore does not require any encryption parameters to be specified during restores. It does require that the certificate or the asymmetric key used to encrypt the backup file be available on the instance that you are restoring to. The user account performing the restore must have VIEW DEFINITION permissions on the certificate or key. If you are restoring the encrypted backup to a different instance, you must make sure that the certificate is available on that instance.
Reference: http://msdn.microsoft.com/en-us/library/dn449489(v=sql.120).aspx
Labels:
Backup and Restore,
Encrypted Backup,
MSSQL,
SQL SERVER 2014
Tuesday, May 6, 2014
Page Level Restoration now has GUI
In one of my previous posts "Page Level Restoration", I had explained how to perform a page level restoration for a database. The whole procedure was using the T-SQL commands.
From SQL Server 2012 onward, we have an GUI to perform Page level restoration which makes life easy.
From SQL Server 2012 onward, we have an GUI to perform Page level restoration which makes life easy.


Labels:
MSSQL,
Page Level Restore,
SQL Server 2012,
SQL SERVER 2014
Backup a database to Windows Azure storage and restore a database from Windows Azure storage
The evaluation of SQL 2014 continues and here is what's new in Backup and Restore of SQL Server 2014.
In SQL Server 2014, we will be able to backup the database to Windows Azure storage and restore the database backup directly from the Windows Azure storage.
To backup a database to Windows Azure storage, Choose the "Back up To:" to "URL" instead of "Disk"

To restore a database from Windows Azure storage, Choose the "Backup media type:" to "URL" instead of "File"

And here is where you connect to the Windows Azure storage during restore operation
Labels:
Backup and Restore,
MSSQL,
SQL SERVER 2014,
What's New
Monday, May 5, 2014
Re-assign F5 key to refresh action in SQL Server 2014 Management Studio
During my evaluation of SQL Server 2014, I happened to face a problem, when I hit the F5 key in the Object Explorer, the SSMS was starting the Debug action instead of Refresh.
Usually most of them will be happy for F5=Refresh and F5=Query Execution.
Here is the procedure to Re-assign F5 key to refresh action in SQL Server Management Studio.
Usually most of them will be happy for F5=Refresh and F5=Query Execution.
Here is the procedure to Re-assign F5 key to refresh action in SQL Server Management Studio.
- Open Management Studio
- Go to Tools >> Options

- Expand Environment >> Keyboard >> Keyboard
- In the "Show commands containing:", type "View.Refresh" and here you can observe that "Shortcuts for selected command:" will be grayed out
- Now choose "Global" in the "Use new shortcut in:" and press F5 button in "Press shortcut keys:"

- Click Assign
- Click Ok
From now on, when you press F5 in the Object explorer, the window will be refreshed instead of initiating debug option.
Labels:
Hekaton,
MSSQL,
SQL SERVER 2014
Saturday, November 27, 2010
Quick flash back on MS SQL Server Code Names
| Code Name | Final name |
| SQL95 | SQL Server 6.0 |
| Hydra | SQL Server 6.5 |
| Sphinx | SQL Server 7.0 |
| Shiloh | SQL Server 2000 (32-bit) |
| Liberty | SQL Server 2000 (64-bit) |
| Yukon | SQL Server 2005 |
| Katmai / Akadia | SQL Server 2008 |
| Kilimanjaro | SQL Server 2008R2 |
| Denali | SQL Server 2012 |
| Hekaton | SQL Server 2014 |
Subscribe to:
Posts (Atom)