I'm experiencing a strange issue on a clients database when using the ISNULL()
function of SQL Server 2014. Searching online has lead me to other people experiencing the same error but when INSERT
ing. I'm getting the following error when running a simple SELECT
:
Msg 681, Level 16, State 3, Line 1
Attempting to set a non-NULL-able column's value to NULL.
The following query produces that error:
SELECT
ISNULL( [t2].[date_update], GETDATE() )
FROM
[dbo].[t1]
INNER JOIN
[dbo].[t2] ON [t1].[a] = [t2].[id];
I've stripped down the tables to simplify my explanation, but the affected columns are defined as follows:
CREATE TABLE [dbo].[t1] (
[id] INT NOT NULL PRIMARY KEY,
[a] NVARCHAR(100) NOT NULL
);
CREATE TABLE [dbo].[t2] (
[id] BIGINT NOT NULL PRIMARY KEY,
[date_update] DATETIME NULL
);
Any help or insight is greatly appreciated!
Edit:
In troubleshooting I was able to reproduce the same error message on the clients system with the following INSERT
:
CREATE TABLE #t2 ([id] BIGINT NOT NULL PRIMARY KEY NONCLUSTERED, [date_update] DATETIME NULL);
INSERT INTO #t2 ([id], [date_update)
SELECT [id], [date_update]
FROM [dbo].[t2];
Edit2:
More troubleshooting has indicated that the index the SELECT
is using is at fault. When trying to rebuild the index I get the same error!
Rebuild failed for Index xxx
Attempting to set a non-NULL-able column's value to NULL.