Quantcast
Channel: SQLServerCentral » SQL Server 2014 » Development - SQL Server 2014 » Latest topics
Viewing all articles
Browse latest Browse all 3145

Persisted computed column not acting very persisted...

$
0
0
The short version of the story is this...I have a Column called "BillID" that is a VARCHAR(20) data type. A typical value looks something like this... A6304158-2The problem is that the numeric portion between the last alpha character and the dash, is an invoice number and we need to be able join that back to our actual invoice tables.So, I had the bright idea to simply write a scalar udf and use that to create a persisted computed column...So far so good... I have verified that the column is indeed persisted.The problem is that SQL Server is balking at any attempt to include this column in an index.ANDLooking at the execution plan for a newly written query, the UDF is being called rather than simply looking at the persisted value.Clearly my understanding of persisted columns is lacking here. I'm hoping that someone may be able to fill in the blanks.Would I be better off dropping the computed column and simply use an insert/update trigger to populate this column?The function code is below.[code="sql"]CREATE FUNCTION dbo.GetInvoiceIDFromBillID/* ===========================================================10/08/2015 JL, Created to strip the InvoiceID out or the BillID to be used to create a persisted, computed column on the BlahBlahBlah_PRI table=========================================================== */( @BillID VARCHAR(20))RETURNS INT WITH SCHEMABINDINGAS BEGIN DECLARE @InvoiceID INT; IF TRY_CAST(@BillID AS INT) > 0 BEGIN SET @InvoiceID = @BillID; END ELSE BEGIN SELECT @InvoiceID = dbo.CLR_RemoveNonNumeric(sc.Item) FROM dbo.SplitCSVToTable8K(@BillID, '-') sc WHERE sc.ItemNumber = 1; END RETURN @InvoiceIDEND;[/code]Note: dbo.CLR_RemoveNonNumeric is a CLRfunction that simply strips out any non-numeric characters and dbo.SplitCSVToTable8K is a renamed version of Jeff's DelimitedSplit8K function.The error I get when trying to create an index is as follows:[quote]Msg 10137, Level 16, State 1, Line 4Cannot create index on view "RevenueCycle.dbo.SplitCSVToTable8K" because it references common table expression "E1". Views referencing common table expressions cannot be indexed. Consider not indexing the view, or removing the common table expression from the view definition.The statement has been terminated.[/quote]Baffling since "RevenueCycle.dbo.SplitCSVToTable8K" is a iTVF not a view and the fact that I'm attempting to index is persisted.Let me know if you required test tables and sample data... Thanks in advance,Jason

Viewing all articles
Browse latest Browse all 3145

Trending Articles