Category Archives: Imported from BeyondRelational

These posts are imported from my old blog page: http://beyondrelational.com/modules/2/blogs/77/nakuls-blog.aspx

#0329 – SQL Server – using DATEDIFF to compare two time values


Today’s post is a simple one, but inspired from a question I encountered in one of the forums. Comparing dates and date-time values are fairly common requirements and we already know a solution for realization of this requirement – the DATEDIFF function.

However, the query that was posted on the forums was about comparison of time values. Let us assume the requirement to be such that if the current time is less than 08:00AM, the default time being returned should be 08:00AM. Else, it can be whatever the current time is. The person who posted the query apparently wanted to know how to achieve this requirement.

The DATEDIFF function can help us here as well. DATEDIFF can work with date, time and date-time values.Here’s an example:

USE tempdb;
GO
DECLARE @referenceValue TIME = '08:00';
DECLARE @comparisonValue TIME = '07:30';

--Let us check the output for a value that is less
--than the reference value
SELECT @comparisonValue AS ComparisonValue,
       @referenceValue AS ReferenceValue,
       CASE WHEN DATEDIFF(SECOND,@referenceValue,@comparisonValue) < 0
            THEN '08:00'
            ELSE @comparisonValue
            END AS ReturnTime;

--Now, let us check the output for a value that is greater
--than the reference value
SELECT @comparisonValue = '08:30';

SELECT @comparisonValue AS ComparisonValue,
       @referenceValue AS ReferenceValue,
       CASE WHEN DATEDIFF(SECOND,@referenceValue,@comparisonValue) < 0
            THEN '08:00'
            ELSE @comparisonValue
            END AS ReturnTime;
GO

Here’s the output:

image

Further Reading

  • Msg 402 – The data types datetime and time are incompatible in the add/subtract operator [Link]
  • DATEDIFF date comparison function [Books On Line Link]

Until we meet next time,
Be courteous. Drive responsibly.

 

#0328 – SQL Server – Configuration Values for User Options


Earlier this week, I wrote a post on ANSI_NULL_DFLT_ON (Impact of ANSI_NULL_DFLT_ON on Temporary Tables). In this post, I used code similar to the following to ascertain whether the ANSI_NULL_DFLT_ON setting is set to ON as a user option (via the connection properties) or not.

IF (@@OPTIONS & 1024) > 0
    PRINT 'ANSI_NULL_DFLT_ON is SET';
ELSE
    PRINT 'ANSI_NULL_DFLT_ON is OFF';
GO

When I was originally introduced to the concept of the @@OPTIONS configuration function, the one question that came into my mind was:

How do I know what configuration value stands for which SET option?

I’m sure that many of you who read the posts from last week would also have had the same question.

The unique values for all user configuration options are documented in the following Books On Line/TechNet page: Configure the user options Server Configuration Option.

It is interesting to note that ultimately they correspond to the bit positions corresponding to a particular option (which is why we can do a bit-wise AND with the final value returned by @@OPTIONS to verify whether a particular option is enabled or not) – another example of how optimized the operation of SQL Server is by default.

Further reading:

  • The @@OPTIONS configuration function [Link]
  • Configure the user options Server Configuration option [Link]

Until we meet next time,

Be courteous. Drive responsibly.

#0327 – SQL Server – Fun with temporary tables – Impact of ANSI_NULL_DFLT_ON


Some lessons are learnt the hard way. Today, it is a well-accepted best practice to always define the NULL-ability on the columns of a temporary table definition. This is because the ANSI_NULL_DFLT_ON connection option influences the default value being inserted into a column when the NULL-ability of the column is not specified. Here’s the extract from Books-On-Line:



When SET ANSI_NULL_DFLT_ON is ON, new columns created by using the ALTER TABLE and CREATE TABLE statements allow null values if the nullability status of the column is not explicitly specified. SET ANSI_NULL_DFLT_ON does not affect columns created with an explicit NULL or NOT NULL.


Most enterprise applications use temporary objects, especially table variables and temporary tables. Some of these applications have been around since a decade or more. They have a mix of “legacy” code (written in the days of SQL 2000) and “modern” code (written in SQL 2008 and beyond).


I was recently called upon to assist with an error that one of our customers was encountering in their production environment. They were attempting to run a weekly routine and encountered an error similar to the following:


Msg 515, Level 16, State 2, Line 2
Cannot insert the value NULL into column ‘City’, table ‘tempdb.dbo.#NullsCheck_________________________________________________________________________________________________________000000000008’; column does not allow nulls. INSERT fails.

The statement has been terminated.


As is the case with most production issues, everything was working fine in our development and quality assurance environments.


While we were going through the scripts to see what could have gone wrong, we learnt that in another troubleshooting attempt by the technical support team, the same routine executed successfully when executed from another SQL Server client. The focus immediately shifted to the connection options.


Once we started reviewing the connection options, the problem was detected almost immediately. In the SSMS on the SQL server client environment where errors were encountered, the ANSI_NULL_DLFT_ON was set to OFF/unchecked in Tools –> Options –> Query Execution –> SQL Server –> ANSI:


image


ANSI_NULL_DFLT_ON is ON


On most SQL Server clients, the ANSI_NULL_DFLT_ON is set to ON by default. In such environments, when an attempt is made to insert NULL values into a column where the NULL-ability is undefined, no error will be reported. The following example demonstrates this.

USE AdventureWorks2012;
GO
IF (@@OPTIONS & 1024) > 0
    PRINT 'ANSI_NULL_DFLT_ON is SET';
ELSE
    PRINT 'ANSI_NULL_DFLT_ON is OFF';
GO

--Safety Check
IF OBJECT_ID('tempdb..#NullsCheck','U') IS NOT NULL
    DROP TABLE #NullsCheck;
GO

--Table definition 
--Notice that the NULL-ability on the columns is undefined
CREATE TABLE #NullsCheck ([Id] INT IDENTITY(1,1),
                          [City] VARCHAR(50),
                          [State] VARCHAR(50)
                         );
GO

--Attempt to insert test data
INSERT INTO #NullsCheck ([City],[State])
VALUES (NULL, NULL),
       ('Portsmouth','New Hampshire'),
       ('Boston', 'Massachusetts');
GO

--Select from the table
SELECT * FROM #NullsCheck;
GO

/*****************************
RESULTS
---------------------------
Id  City        State
---------------------------
1   NULL        NULL
2   Portsmouth  New Hampshire
3   Boston      Massachusetts
*****************************/

Before we close the query editor window in SSMS, let us take a quick look at the NULL-ability on the columns of the temporary table.

--Check the column properties
SELECT tisc.TABLE_NAME,
       tisc.COLUMN_NAME,
       tisc.IS_NULLABLE
FROM tempdb.INFORMATION_SCHEMA.COLUMNS AS tisc
WHERE tisc.TABLE_NAME LIKE '#NullsCheck%';
GO

image 


ANSI_NULL_DFLT_ON is OFF


When ANSI_NULL_DFLT_ON is OFF (as it was in this case), the same script will return the error that we were seeing.


This is because, when the temporary table is created, the table definition has been created with the columns as NOT NULL. Here’s the same check as above, but after turning the ANSI_NULL_DFLT_ON to OFF:


image


Lessons Learnt



  1. Always define the NULL-ability when defining tables – temporary or otherwise
  2. Whenever a new coding standard is adopted for an existing system, it is always a good idea to have an inventory of objects that violated the coding standard when the standards were adopted
  3. As service releases of the product are released, it is a good idea to revisit the inventory and make the entire code conform to the standard over time

Further Reading



  • Fun with Temporary Tables – Named Constraints, Msg 2714, 1750 [Link]
  • Fun with Temporary Tables – Foreign Keys [Link]
  • ANSI_NULL_DFLT_ON [Books On Line Link]

Until we meet next time,



Be courteous. Drive responsibly.

#0326 – SQL Server – Setting database to READ_ONLY does not change the file-group properties


Last week, I re-learnt something which, according to me, is a little counter-intuitive. I was studying read-only databases out of academic interest and noticed something interesting:

When a database is marked as Read-only, the underlying file/file-groups are not marked Read-only.

Here’s a simple test wherein we:

  1. Create a test database with multiple file-groups (the test works equally well with a single file-group)
  2. Set the database to READ_ONLY
  3. Check the file-group properties
USE master;
GO
--Create the test database
CREATE DATABASE ReadOnlyDB
ON
PRIMARY  (Name=ReadOnlyDB_Primary,
          FileName='C:DatabasesSQL2012ReadOnlyDB_Primary.mdf'
         ),
FILEGROUP SecondaryFG
        (Name=ReadOnlyDB_Secondary,
         FileName='C:DatabasesSQL2012ReadOnlyDB_Secondary.ndf'
        )
LOG ON (Name=ReadOnlyDB_Log,
        FileName='C:DatabasesSQL2012ReadOnlyDB_Log.ldf'
       );
GO

USE master;
GO
--Set the database to READ_ONLY
ALTER DATABASE ReadOnlyDB SET READ_ONLY;
GO

USE ReadOnlyDB;
GO
--Check the File & File-group properties
SELECT sfg.is_read_only,sfg.*
FROM sys.filegroups AS sfg;
GO
SELECT sdf.is_read_only,sdf.*
FROM sys.database_files AS sdf;
GO

image

As can be seen from the output, none of the file-groups were marked as read-only even though the database is read-only. To confirm that the database is indeed read-only, let us attempt to create a table on the database.

USE ReadOnlyDB;
GO
CREATE TABLE TestTbl (Id INT IDENTITY(1,1),
                      Name VARCHAR(50)
                     )
ON SecondaryFG;
GO

Msg 3906, Level 16, State 1, Line 1

Failed to update database “ReadOnlyDB” because the database is read-only.

A possible explanation

The fact that the file-groups are not marked read-only even though the database is read-only is counter-intuitive.

The only possible explanation that I have is that primary file-groups cannot be marked read-only which is why SQL Server does not automatically attempt to mark the file-groups and log files as read-only.

Further Reading

  • SQL Server Myth: Log files are removed when a database is made READ_ONLY [Link]

Until we meet next time,

Be courteous. Drive responsibly.

#0325 – SQL Server – fn_trace_gettable – Get database name change history from default trace


A few weeks ago, I received a very interesting question from one of the readers. The question was:



“Given that no explicit auditing/logging exists on a particular database, can we get a history of all the various names that a database was assigned?”


This is a classic case where the answer is: “It depends!


NOTE: The explanation below requires an understanding of SQL Server’s default tracing mechanism.


Depending upon the needs of the business, it is quite possible that there exist multiple copies of the same database on the same or multiple servers (in a development or QA environment, for example). Hence, it may become very difficult to maintain the database name history within the database (because there would be many questions like – should the history be truncated upon restore; etc). Besides, the database name itself does not have any impact on the operation of the database then why should we maintain a history of the various names that the database has assumed over time?


Therefore, there is no durable log of the database rename activity stored in the database or any of the system databases.


By default, Microsoft SQL Server maintains a default profiler trace that can help administrators get a log of activity primarily related to the configuration options that happened within SQL Server. However, the default trace is limited to a set of files and when the SQL Server fills up all of these files, it rolls over and deletes the first of the files in the set to maintain the same number of trace files by continuing the trace in a new file.


Because a trace is constantly running against the server, the trace would have captured the renaming of the database. Hence, if we query the default trace file, we can get information about database renames, provided that the trace file has not yet been rolled over by SQL Server. Allow me to demonstrate with an example:


The following query creates a database and renames it thrice. It then queries the default trace to get a log of all rename activity.

–Safety check
IF EXISTS(SELECT * FROM sys.databases AS sd WHERE sd.name = ‘BRNAV0325v3’)
DROP DATABASE BRNAV0325v3;
GO

–Now create the test database
CREATE DATABASE BRNAV0325;
GO

–Rename the database (1st time)
ALTER DATABASE BRNAV0325
MODIFY NAME=BRNAV0325v1;
GO

–Rename the database (2nd time)
ALTER DATABASE BRNAV0325v1
MODIFY NAME=BRNAV0325v2;
GO

–Rename the database (3rd time)
ALTER DATABASE BRNAV0325v2
MODIFY NAME=BRNAV0325v3;
GO

–If the default trace is enabled, get the path to the log file
–SELECT * from sys.traces where is_default = 1

–Query the default trace to get a record of database renames
SELECT ft.DatabaseID ,
ft.DatabaseName ,
MIN(ft.StartTime) AS ActivityDate ,
ft.LoginName,
DB_NAME(ft.DatabaseID) AS CurrentDatabaseName
FROM ::fn_trace_gettable( ‘C:Program FilesMicrosoft SQL ServerMSSQL11.SQL2012MSSQLLoglog.trc’ , DEFAULT) AS ft
WHERE ft.EventClass IN( 46 , –CREATE
47 , –DROP
164) –ALTER
AND ft.EventSubclass = 0 –We are not interested in granularity
AND ft.DatabaseID <> 2 –tempdb activity can be ignored
AND ft.IndexID IS NULL –We do not need any table/column activity
–(including statistics)
AND ft.DatabaseName LIKE ‘BRNAV0325%’
GROUP BY ft.DatabaseID,
ft.DatabaseName,
ft.LoginName;
GO

–Cleanup
IF EXISTS(SELECT * FROM sys.databases AS sd WHERE sd.name = ‘BRNAV0325v3’)
DROP DATABASE BRNAV0325v3;
GO


The output looks similar to the following:


image



As you can see, subject to the availability of the necessary data in the default trace, we can use the function – fn_trace_gettable() – to get a record of the database rename operations.


Further Reading



  • sys.fn_trace_gettable [Books On Line Link]
  • Default Trace Server configuration option [Books On Line Link]

Until we meet next time,


Be courteous. Drive responsibly.