Category: SQL

How to recover restoring database in SQL Server

At times, we end up with situations like databases suddenly goes into restoring state and becomes not accessible. Few cases those are valid like mirror database in a database mirroring or removed database from AlwaysOn etc. Today, we are not discussing about those valid cases, but as outcome of an incident or unfortunate db state change.

If you happened to see a database in restoring and you want to get it online, you can simply restore with recovery as below.

use master
restore database << dbname >> with recovery

In most cases, the db would get online immediately. Sometimes, it might give you an error message as below:

Msg 4333, Level 16, State 1, Line 3 The database cannot be recovered because the log was not restored. Msg 3013, Level 16, State 1, Line 3 RESTORE DATABASE is terminating abnormally.

If you result with the above error message, then we should understand that the particular database is not restored in a proper way. May be. the restore process would have terminated due to network issues or cancelled the restore part way through etc. Such database cannot be brought online. The best way would be to drop the database and restore from the backup.

restore database << dbname >> from disk ='<filepath>\<backup filename>'

It is important to have a valid database available for us to quickly get this db online. So ensuring the backup is important as always, hope we all will have a good backup plan in place by default, if not, that should be the first priority item.

I’d like to grow my readership. If you enjoyed this blog post, please share it with your friends!

Whats new with TempDB in SQL Server 2016

Its always quite interesting topic TempDB in SQL Server. Microsoft SQL Server core engine team continuously working on improving the performance versions to version and its one of the area TempDB that they focused a lots recently. If you need to get an overview of TempDB and its importance, I highly recommend to read earlier post.

Today, we are going to see some of the enhancements of TempDB in SQL Server 2016. If you wonder of its rationale of such late post, let me tell you I had to face a question int his area and thought its good to have a post to share with everyone if that helps to a larger people.

While we migrated our database server from SQL 2008 R2 to SQL 2016, we had made quite few changes at the configuration level. One of the changes is we completely removed the Trace Flags (TF -1117 and 1118) from start up parameters. So, today there was q question from one my co-worker why did we remove this TFs in SQL Server 2016? So, let us see the reason here, perhaps a little more than this can cover the enhancements in SQL Server 2016 for Tempdb database.

Few changes in SQL Server 2016

    1. SQL server 2016 decides on the number of TempDB files as default to use by considering the number of logical processors on the server. This helps to avoid the latch contention issues with a single TempDB file (by default in earlier version). Ofcourse, this configuration can be easily changed by experienced database administrators.

    2. Inclusion of Trace flags 1117 and 1118 in the database engine and no longer needed to activate.

    Excerpt from MSDN documentation on TF 1117 & 1118 as below.

    3. Temporary table object caching

I’d like to grow my readership. If you enjoyed this blog post, please share it with your friends!

How to find last restart time of SQL Server

Why do we need this information?

This is an important piece of information for a SQL Server engineer. I carefully tend to use the word “engineer” here because this information helps all kinds of people who works with SQL Server. As an Administrator would like to understand when did it happen to understand the underlining issue with any server level issues/fail over details etc. As a performance Engineer, this information helps when the cache gets cleared and for many other reasons. So its very important and useful information as long as you are working in SQL Server.

Now, there are two different ways your SQL Service could get restart – (1) By restarting the SQL Service and (2) By restarting the Windows Server. Let us look at different ways to find this information.

What are the different ways to get last restart of SQL Service?

Method 1: Using sys.dm_os_sys_info Its the most simple, easiest and my favorite way to get this information.
SELECT sqlserver_start_time FROM sys.dm_os_sys_info

Method 2: Using tempdb database creation date
SELECT create_date FROM sys.databases WHERE name = 'tempdb'

Method 3: Using sp_readerrorlog
sp_readerrorlog 0,1,'Copyright (c)'

What are the different ways to get last restart of Windows Server?

Method 1: Using Task Manager

Up time represents how long the server is being up and running since the last restart. So to calculate when did the reboot happened, need to substract the up time with current date and time.

Method 2: Using systeminfo command
systeminfo | find /i "Boot Time"

Hope this helps and if you have any other ways, please share in the comment section.

I’d like to grow my readership. If you enjoyed this blog post, please share it with your friends!

SQL Server – Invalid Urn filter on server level: filter must be empty, or server attribute must be equal with the true server name

Problem Statement:

One of my colleague has reported an issue as below while he is trying to restore a database from backup.

SQL Server – Invalid Urn filter on server level: filter must be empty, or server attribute must be equal with the true server name

To my best knowledge, I was seeing this error first time and I was trying to understand about the issue with my google search and there are quite few good posts out there too.

http://blog.patricknielsen.net/2011/01/sql-server-invalid-urn-filter-on-server.html
https://debabratahowlee.blogspot.com/2016/09/sql-server-invalid-urn-filter-on-server.html

But somehow I was thinking something different as I was sure there were no activities at server level. Then I simply closed all open connections and restarted SSMS and tried and it worked perfectly this time. So, I do not have any clue yet what would have happened under the cover, however, am sure there is something gone bad it could be a failover/underlined network or anything that it could not resolve the server name.

If you have any different thoughts, please share in comment section.

I’d like to grow my readership. If you enjoyed this blog post, please share it with your friends!

Update() Trigger Function in Table

Update() function returns “True” in any case the Insert/update attempt is successful on the supplied column, the significance of this function is to trigger the action/code only if the supplied column is get updated or inserted successfully.

In the below example, I’ve used the Update() function for identifying any changes in “CodeValue” column of table “Configtbl” and ignored the column changes if its value is same.

“Configtbl_Audit” table will get loaded based on the changes in “CodeValue” column in “Configtbl”

drop table if exists Configtbl
drop table if exists Configtbl_Audit

create table Configtbl(
	 Code nvarchar(10) not null,
	 CodeValue decimal(10,2) not null
)
insert into Configtbl(Code,CodeValue) values ('VA1111',25.99),('VA1118',19.99)

create table Configtbl_Audit(
	 Code nvarchar(10) not null,
	 Old_Value decimal(10,2) not null,
	 New_Value decimal(10,2) not null,
	 DateModified datetime not null
)
 
IF EXISTS (SELECT name FROM sys.objects WHERE name = 'TRG_UPD_NEW_OLD_VALUE' AND type = 'TR')  
   DROP TRIGGER TRG_UPD_NEW_OLD_VALUE 
GO 

CREATE TRIGGER TRG_UPD_NEW_OLD_VALUE  
ON Configtbl
FOR UPDATE   
AS   

declare @oldvalue decimal(10,2),@newvalue decimal(10,2), @Code nvarchar(10)
IF (UPDATE (CodeValue))  
BEGIN
	select @oldvalue = CodeValue , @Code = Code from deleted

	insert into Configtbl_Audit(Code, Old_Value, New_Value, DateModified)
	select @Code, @oldvalue, CodeValue, getdate()
	from inserted 
	where code=@Code
	and @oldvalue <> CodeValue

END;  
GO 

Output:-

View of Data in Configtbl and Configtbl_Audit tables before doing any updates:

After updating “Code” column in Configtbl table:

After updating “CodeValue” column in Configtbl table:

I’d like to grow my readership. If you enjoyed this blog post, please share it with your friends!