{"id":817,"date":"2006-12-30T00:04:00","date_gmt":"2006-12-30T00:04:00","guid":{"rendered":"\/blogs\/bobb\/post\/Using-SQLCLR-functions-in-indexed-views.aspx"},"modified":"2006-12-30T00:04:00","modified_gmt":"2006-12-30T00:04:00","slug":"using-sqlclr-functions-in-indexed-views","status":"publish","type":"post","link":"https:\/\/www.sqlskills.com\/blogs\/bobb\/using-sqlclr-functions-in-indexed-views\/","title":{"rendered":"Using SQLCLR functions in indexed views"},"content":{"rendered":"<p>\nIt&#39;s been over a month since I&#39;ve blogged, confirmed by the previous blog entry when &quot;it&#39;s sunny in Barcelona&quot;. Thought I&#39;d blog about&nbsp;a question that someone wrote to ask me&nbsp;a few weeks ago.\n<\/p>\n<p>\nWhat are the limitations\/requirements for using a SQLCLR function in an indexed view (aka materialized view)?\n<\/p>\n<p>\nSome of the requirements came from the books online, but can be verified with a simple sample, but there were a few surprises.\n<\/p>\n<p>\n1. The VIEW must be created WITH SCHEMABINDING. This is a &quot;normal&quot; requirement of an indexed view, but usually when creating VIEWs WITH SCHEMABINDING that reference UDFs, the UDF must be defined WITH SCHEMABINDING as well. SQLCLR UDFs can&#39;t be explicitly be defined WITH SCHEMABINDING, but they can be used in views defined as WITH SCHEMABINDING.<br \/>\n2. The VIEW can&#39;t use a SQLCLR derived column as part of the index key unless the derived column is declared as PERSISTED in the base table.<br \/>\n3. The SQLCLR function must be declared Deterministic and Precise, and do no data access using SqlCommand, etc. DataAccess = None, SystemDataAccess = None is the default in SQLCLR functions anyway. And no external access is allowed.<br \/>\n4. An indexed view cannot contain a SQLCLR user-defined aggregate (UDA) function.\n<\/p>\n<p>\nTo demonstrate, I wrote two functions AddOne in SQLCLR, TSQLAddOne in TSQL. You can use AddOne in an indexed view. You can use AddOne as a key column in an indexed view if its defined as persisted in the base table. Note that the TSQLAddOne can be used as an index only if its defined WITH SCHEMABINDING.\n<\/p>\n<p>\nThis is fairly similar to the restrictions for TSQL functions and indexed views, except that you can access data in TSQL function and use it as the key, only if the TSQL function is defined with schemabinding. SQLCLR functions can&#39;t be defined with schemabinding, because there is no way to figure out which tables, etc, it&#39;s accessing. So because you can declare a TSQL function with schemabinding, you can probably make it part of the key if you access data. But, as when using a SQLCLR&nbsp;function in a VIEW&nbsp;WITH SCHEMABINDING, this shouldn&#39;t be an&nbsp;issue&nbsp;when&nbsp;your SQLCLR function does no data access.&nbsp;Hmmm&#8230;\n<\/p>\n<p>\nCheck out &quot;table3 and view3&quot;. It uses SQLCLR function in a persisted computed column, and then as the KEY in an indexed view derived from the table.\n<\/p>\n<p><a href=\"\/blogs\/bobb\/content\/binary\/index_view_clr.zip\">index_view_clr.zip (13.75 KB)<\/a> <\/p>\n","protected":false},"excerpt":{"rendered":"<p>It&#39;s been over a month since I&#39;ve blogged, confirmed by the previous blog entry when &quot;it&#39;s sunny in Barcelona&quot;. Thought I&#39;d blog about&nbsp;a question that someone wrote to ask me&nbsp;a few weeks ago. What are the limitations\/requirements for using a SQLCLR function in an indexed view (aka materialized view)? Some of the requirements came from [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[28,38],"tags":[],"class_list":["post-817","post","type-post","status-publish","format-standard","hentry","category-sql-server-2005","category-sqlclr"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.9.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Using SQLCLR functions in indexed views - Bob Beauchemin<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.sqlskills.com\/blogs\/bobb\/using-sqlclr-functions-in-indexed-views\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Using SQLCLR functions in indexed views - Bob Beauchemin\" \/>\n<meta property=\"og:description\" content=\"It&#039;s been over a month since I&#039;ve blogged, confirmed by the previous blog entry when &quot;it&#039;s sunny in Barcelona&quot;. Thought I&#039;d blog about&nbsp;a question that someone wrote to ask me&nbsp;a few weeks ago. What are the limitations\/requirements for using a SQLCLR function in an indexed view (aka materialized view)? Some of the requirements came from [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.sqlskills.com\/blogs\/bobb\/using-sqlclr-functions-in-indexed-views\/\" \/>\n<meta property=\"og:site_name\" content=\"Bob Beauchemin\" \/>\n<meta property=\"article:published_time\" content=\"2006-12-30T00:04:00+00:00\" \/>\n<meta name=\"author\" content=\"Bob Beauchemin\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Bob Beauchemin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/using-sqlclr-functions-in-indexed-views\/\",\"url\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/using-sqlclr-functions-in-indexed-views\/\",\"name\":\"Using SQLCLR functions in indexed views - Bob Beauchemin\",\"isPartOf\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/#website\"},\"datePublished\":\"2006-12-30T00:04:00+00:00\",\"dateModified\":\"2006-12-30T00:04:00+00:00\",\"author\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/62bfa986c5b5d28fcffd8b4fc409c73e\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/using-sqlclr-functions-in-indexed-views\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.sqlskills.com\/blogs\/bobb\/using-sqlclr-functions-in-indexed-views\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/using-sqlclr-functions-in-indexed-views\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"SQL Server 2005\",\"item\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/category\/sql-server-2005\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Using SQLCLR functions in indexed views\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/#website\",\"url\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/\",\"name\":\"Bob Beauchemin\",\"description\":\"SQL Server Blog\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/62bfa986c5b5d28fcffd8b4fc409c73e\",\"name\":\"Bob Beauchemin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/6f80e6cc667410857fa6a21931dc528b8092f4d112bf7a8ff7c267674d44ee37?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/6f80e6cc667410857fa6a21931dc528b8092f4d112bf7a8ff7c267674d44ee37?s=96&d=mm&r=g\",\"caption\":\"Bob Beauchemin\"},\"sameAs\":[\"http:\/www.sqlskills.com\/blogs\/bobb\/\"],\"url\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/author\/bobb\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Using SQLCLR functions in indexed views - Bob Beauchemin","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.sqlskills.com\/blogs\/bobb\/using-sqlclr-functions-in-indexed-views\/","og_locale":"en_US","og_type":"article","og_title":"Using SQLCLR functions in indexed views - Bob Beauchemin","og_description":"It&#39;s been over a month since I&#39;ve blogged, confirmed by the previous blog entry when &quot;it&#39;s sunny in Barcelona&quot;. Thought I&#39;d blog about&nbsp;a question that someone wrote to ask me&nbsp;a few weeks ago. What are the limitations\/requirements for using a SQLCLR function in an indexed view (aka materialized view)? Some of the requirements came from [&hellip;]","og_url":"https:\/\/www.sqlskills.com\/blogs\/bobb\/using-sqlclr-functions-in-indexed-views\/","og_site_name":"Bob Beauchemin","article_published_time":"2006-12-30T00:04:00+00:00","author":"Bob Beauchemin","twitter_misc":{"Written by":"Bob Beauchemin","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/using-sqlclr-functions-in-indexed-views\/","url":"https:\/\/www.sqlskills.com\/blogs\/bobb\/using-sqlclr-functions-in-indexed-views\/","name":"Using SQLCLR functions in indexed views - Bob Beauchemin","isPartOf":{"@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/#website"},"datePublished":"2006-12-30T00:04:00+00:00","dateModified":"2006-12-30T00:04:00+00:00","author":{"@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/62bfa986c5b5d28fcffd8b4fc409c73e"},"breadcrumb":{"@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/using-sqlclr-functions-in-indexed-views\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.sqlskills.com\/blogs\/bobb\/using-sqlclr-functions-in-indexed-views\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/using-sqlclr-functions-in-indexed-views\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.sqlskills.com\/blogs\/bobb\/"},{"@type":"ListItem","position":2,"name":"SQL Server 2005","item":"https:\/\/www.sqlskills.com\/blogs\/bobb\/category\/sql-server-2005\/"},{"@type":"ListItem","position":3,"name":"Using SQLCLR functions in indexed views"}]},{"@type":"WebSite","@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/#website","url":"https:\/\/www.sqlskills.com\/blogs\/bobb\/","name":"Bob Beauchemin","description":"SQL Server Blog","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.sqlskills.com\/blogs\/bobb\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/62bfa986c5b5d28fcffd8b4fc409c73e","name":"Bob Beauchemin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/6f80e6cc667410857fa6a21931dc528b8092f4d112bf7a8ff7c267674d44ee37?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/6f80e6cc667410857fa6a21931dc528b8092f4d112bf7a8ff7c267674d44ee37?s=96&d=mm&r=g","caption":"Bob Beauchemin"},"sameAs":["http:\/www.sqlskills.com\/blogs\/bobb\/"],"url":"https:\/\/www.sqlskills.com\/blogs\/bobb\/author\/bobb\/"}]}},"_links":{"self":[{"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/posts\/817","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/comments?post=817"}],"version-history":[{"count":0,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/posts\/817\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/media?parent=817"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/categories?post=817"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/tags?post=817"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}