Bugbash
At some point in the past, I blogged about a silent bug with computed columns and clustered column store indexes.
In this post, I’m going to take a quick look at a very loud bug.
Normally, you can’t add a filtered index to a computed column. I’ve always hated that limitation. How nice would that be for so many currently difficult tasks?
I wonder if Oracle…
You Can’t Do That On Television
If you run this code to completion — and boy are you missing out if you don’t — you get an error.
CREATE TABLE dbo.ohno ( id int NOT NULL, crap AS id * 2 ); GO CREATE NONCLUSTERED INDEX c ON dbo.ohno (crap) WHERE crap > 0; GO
Here’s the error:
Msg 10609, Level 16, State 1, Line 13 Filtered index 'c' cannot be created on table 'dbo.ohno' because the column 'crap' in the filter expression is a computed column. Rewrite the filter expression so that it does not include this column.
Okay, great! Works on my machine.
Kapow, Kapow
However, you can totally create this table using the inline index creation syntax.
CREATE TABLE dbo.ohyeah ( id int NOT NULL, crap AS id * 2, INDEX c (crap) WHERE crap > 0 ); GO
However, if you try to query the table, you’re met with a buncha errors.
SELECT id FROM dbo.ohyeah AS o; SELECT c = COUNT_BIG(*) FROM dbo.ohyeah AS o;
Even without explicitly referencing the computed column, you get this error message.
Msg 207, Level 16, State 1, Procedure ohyeah, Line 1 [Batch Start Line 29] Invalid column name 'crap'. Msg 207, Level 16, State 1, Procedure ohyeah, Line 1 [Batch Start Line 29] Invalid column name 'crap'. Msg 207, Level 16, State 1, Procedure ohyeah, Line 1 [Batch Start Line 29] Invalid column name 'crap'. Msg 207, Level 16, State 1, Procedure ohyeah, Line 1 [Batch Start Line 29] Invalid column name 'crap'. Msg 207, Level 16, State 1, Procedure ohyeah, Line 1 [Batch Start Line 29] Invalid column name 'crap'. Msg 4184, Level 16, State 2, Line 30 Cannot retrieve table data for the query operation because the table "dbo.ohyeah" schema is being altered too frequently. Because the table "dbo.ohyeah" contains a filtered index or filtered statistics, changes to the table schema require a refresh of all table data. Retry the query operation, and if the problem persists, use SQL Server Profiler to identify what schema-altering operations are occurring.
You see what? See you what that error message says? “[I[f the problem persists, use SQL Server Profiler to identify what schema-altering operations are occurring”
Use SQL Server Profiler.
Take that, Extended Events.
Thanks for reading!
Going Further
If this is the kind of SQL Server stuff you love learning about, you’ll love my training. I’m offering a 75% discount to my blog readers if you click from here. I’m also available for consulting if you just don’t have time for that and need to solve performance problems quickly.
Related Posts
- Common SQL Server Consulting Advice: Adding Computed Columns
- A Silent Bug With Clustered Column Store Indexes And Computed Columns In SQL Server 2019
- IF Branching In SQL Server Stored Procedures: Dynamic SQL!
- IF Branching In SQL Server Stored Procedures: Fixing Parameters, Recompiling, And A Good Enough Plan
HAHA! That’s fantastic!
Eye of the beholder and whatnot.
LMAO
must you break all the things
It was like that when I got here.