Cannot create the clustered index “%.*ls” on view “%.*ls” because the view references an unknown value (SUM aggregate of nullable expression). Consider referencing only non-nullable values in SUM. ISNULL() may be useful for this.

Error Message:
Msg 8662, Level 16, State 0, Line 2
Cannot create the clustered index “%.*ls” on view “%.*ls” because the view references an unknown value (SUM aggregate of nullable expression). Consider referencing only non-nullable values in SUM. ISNULL() may be useful for this.

Severity level:
16.

Description:
This error message appears when you try to create an indexed view that allows NULL markers in a SUM aggregate expression.

Consequences:
The T-SQL statement can be parsed, but causes the error at runtime.

Resolution:
Errors of the Severity Level 16 are generated by the user and can be fixed by the SQL Server user. The statement cannot be executed this way. You must modify the SUM expression. If you cannot prevent NULL markers, try to replace them via ISNULL() with a resultneutral value.

Versions:
All versions of SQL Server.

Example(s):
USE Northwind;
GO
SET NUMERIC_ROUNDABORT OFF;
SET ANSI_PADDING,
    ANSI_WARNINGS,
    CONCAT_NULL_YIELDS_NULL,
    ARITHABORT,
    QUOTED_IDENTIFIER,
    ANSI_NULLS ON;
GO

IF OBJECT_ID (‘dbo.MyOrderView’, ‘View’) > 0
   DROP VIEW dbo.MyOrderView;
GO
CREATE VIEW dbo.MyOrderView
WITH SCHEMABINDING
AS
SELECT SUM(od.UnitPrice*od.Quantity*(NULL-od.Discount)) AS Revenue,
    o.OrderDate, od.ProductID, COUNT_BIG(*) AS MyCount
  FROM dbo.[Order Details] AS od
  JOIN dbo.Orders AS o
    ON od.OrderID = o.OrderID
 GROUP BY o.OrderDate, od.ProductID;
GO

CREATE UNIQUE CLUSTERED INDEX cix_MyOrderView
    ON dbo.MyOrderView (OrderDate, ProductID);
GO

Remarks:
In the above example we try to create an indexed view. Because we use the NULL marker directly in the SUM expression, the error is raised.




Related Articles :

  • No Related Articles Found

Trackbacks/Pingbacks

  1. SQL Server Performance - May 22, 2011

    [...] procname can only be a literal or variable of type char, varchar, nchar, or nvarchar.Cannot create the clustered index “%.*ls” on view “%.*ls” because the view r…Cannot add identity column, using the SELECT INTO statement, to table ‘%.*ls’, which [...]

Software Reviews | Book Reviews | FAQs | Tips | Articles | Performance Tuning | Audit | BI | Clustering | Developer | Reporting | DBA | ASP.NET Ado | Views tips | | Developer FAQs | Replication Tips | OS Tips | Misc Tips | Index Tuning Tips | Hints Tips | High Availability Tips | Hardware Tips | ETL Tips | Components Tips | Configuration Tips | App Dev Tips | OLAP Tips | Admin Tips | Software Reviews | Error | Clustering FAQs | Performance Tuning FAQs | DBA FAQs |