<p>My current view is no, prefer Transact SQL stored procedures because they are a lighter weight and (possibly) higher performing option, while CLR procedures allow developers to get up to all sorts of mischief.</p>

<p>However recently I have needed to debug some very poorly written TSQL stored procs.  As usual I found many of the problems due to the original developer developer having no real TSQL experience, they were  ASP.NET / C# focused.</p>

<p>So, using CLR procedures would firstly provide a much more familiar toolset to this type of developer, and secondly, the debugging and testing facilities are more powerful (ie Visual Studio instead of SQL Management Studio).  </p>

<p>I'd be very interested in hearing your experience as it's seems it is not a simple choice. </p>

[{'answer_id': 58194, 'author': 'Dan Blair', 'author_id': 1327, 'author_profile': 'https://Stackoverflow.com/users/1327', 'pm_score': 2, 'selected': False, 'text': "<p>In general you use the CLR if you have something that doesn't need to interface with the database much.  So let's say you are parsing, or decoding a value. This is easier to do in the CLR and then return the value.</p>\n\n<p>Trying to do a compelx query in the CLR is just not the way to go.</p>\n\n<p>BTW this didn't change in 2008 either.</p>\n"}, {'answer_id': 58200, 'author': 'Eric Z Beard', 'author_id': 1219, 'author_profile': 'https://Stackoverflow.com/users/1219', 'pm_score': 4, 'selected': False, 'text': '<p>CLR stored procedures are not meant to replace set-based queries.  If you need to query the database, you are still going to need to put SQL into your CLR code, just as if it was embedded in regular code.  This would be a waste of effort.</p>\n\n<p>CLR stored procedures are for two main things: 1) interaction with the OS, such as reading from a file or dropping a message in MSMQ, and 2) performing complex calculations, especially when you already have the code written in a .NET language to do the calculation.</p>\n'}, {'answer_id': 58204, 'author': 'Eugene Yokota', 'author_id': 3827, 'author_profile': 'https://Stackoverflow.com/users/3827', 'pm_score': 5, 'selected': True, 'text': '<p>There are places for both well-written, well-thought-out T-SQL and CLR. If some function is not called frequently and if it required extended procedures in SQL Server 2000, CLR may be an option. Also running things like calculation right next to the data may be appealing. But solving bad programmers by throwing in new technology sounds like a bad idea.</p>\n'}, {'answer_id': 58260, 'author': 'Ryan Farley', 'author_id': 1627, 'author_profile': 'https://Stackoverflow.com/users/1627', 'pm_score': 2, 'selected': False, 'text': "<p>It always comes down to the right tool for the job, so it really depends on what you are trying to accomplish.  </p>\n\n<p>However, as a general rule, you're right that CLR procs have a greater overhead and will never perform on set operations like T-SQL. My guideline is do it all in T-SQL unless what you need becomes overly complicated in T-SQL. Then, try harder to get the T-SQL approach to work.  :-)  </p>\n\n<p>CLR procs are great and do have their place, but their use should be the exception, not the rule.</p>\n"}, {'answer_id': 58275, 'author': 'icelava', 'author_id': 2663, 'author_profile': 'https://Stackoverflow.com/users/2663', 'pm_score': 3, 'selected': False, 'text': '<p>Hosting the CLR within SQL Server is meant to give database developers <em>more flexible options</em> in how they sought to accomplish tasks. Like others have mentioned, SQL is great for operations and modifications on <em>sets of data</em>. Anybody who has done extensive large application development with complex business/domain rules would likely tell you - trying to enforce some of these rules using pure SQL (some times into a single macro query) can get truly nightmarish.</p>\n\n<p>There are just certain tasks that are better handled in a procedural or OO fashion. By having the choice of using .NET code to break down the sequence of logic, query operations can get easier to read and debug. Having used CLR stored procs I can tell you stepping through with the debugger really makes it easier to follow through with what is happening at the database level.</p>\n\n<p>Just one example, we frequently use CLR stored procs here as a "gateway" for dynamic search queries. Say a search request that can have up to 30 different search parameters. Users obviously don\'t use all 30 of them, so the data structure passed in will have 30 parameters but mostly DBNULL. The client side has <em>no option</em> to generate a dynamic statement, for obvious security reasons. The resulting dynamic statement is generated internally without fear of external "extras".</p>\n'}, {'answer_id': 58285, 'author': 'Mark Cidade', 'author_id': 1659, 'author_profile': 'https://Stackoverflow.com/users/1659', 'pm_score': 2, 'selected': False, 'text': '<p>The SQL Server Books Online\'s <a href="http://msdn.microsoft.com/en-us/library/ms131089.aspx" rel="nofollow noreferrer">page on the subject</a> lists these benefits:</p>\n\n<ul>\n<li><p><strong>A better programming model.</strong> The .NET Framework languages are in many respects richer than Transact-SQL, offering constructs and capabilities previously not available to SQL Server developers. Developers may also leverage the power of the .NET Framework Library, which provides an extensive set of classes that can be used to quickly and efficiently solve programming problems.</p></li>\n<li><p><strong>Improved safety and security.</strong> Managed code runs in a common language run-time environment, hosted by the Database Engine. SQL Server leverages this to provide a safer and more secure alternative to the extended stored procedures available in earlier versions of SQL Server.</p></li>\n<li><p><strong>Ability to define data types and aggregate functions.</strong> User defined types and user defined aggregates are two new managed database objects which expand the storage and querying capabilities of SQL Server.</p></li>\n<li><p><strong>Streamlined development through a standardized environment.</strong> Database development is integrated into future releases of the Microsoft Visual Studio .NET development environment. Developers use the same tools for developing and debugging database objects and scripts as they use to write middle-tier or client-tier .NET Framework components and services.</p></li>\n<li><p><strong>Potential for improved performance and scalability.</strong> In many situations, the .NET Framework language compilation and execution models deliver improved performance over Transact-SQL.</p></li>\n</ul>\n'}, {'answer_id': 58375, 'author': 'Gishu', 'author_id': 1695, 'author_profile': 'https://Stackoverflow.com/users/1695', 'pm_score': 2, 'selected': False, 'text': '<p>I believe those two aren\'t equivalent... fit to square off against each other.<br>\n<strong>CLR Integration is supposed to phase out "extended stored procedures" of yore.</strong> We have some of these in our workplace... essentially blocks of processing/logic over SQL data that was too hard/impossible to do via conventional DB Stored procedures/T SQL. So they wrote it up as extended stored procedures in C++ DLLs that can be invoked similarly. \n<a href="http://msdn.microsoft.com/en-us/library/ms164734.aspx" rel="nofollow noreferrer">Now they have been phased out and CLR integration is the replacement</a></p>\n\n<ul>\n<li>DB Stored procedures: if it can be done in T SQL Stored procs, do it.</li>\n<li>CLR Stored procedures: if the logic is too complex or tedious to do via T SQL... if its something that will take fewer lines of CLR code to tackle it (string manipulation, complex/custom sorting or filtering, etc.) use this approach.</li>\n</ul>\n'}, {'answer_id': 59452, 'author': 'AlexCuse', 'author_id': 794, 'author_profile': 'https://Stackoverflow.com/users/794', 'pm_score': 2, 'selected': False, 'text': "<p>Aside from the file system access (where CLR procs has a very pronounced advantage) I would use T-SQL procs.  If you have especially complex calculations you could possibly put that piece into a CLR <strong>function</strong> and call this from within your proc (udf's are where I've found the CLR integration really shines).  Then you get the benefits of the CLR integration for that particular part of your task but keep as much of your stored proc logic in the DB as you can.</p>\n"}, {'answer_id': 439978, 'author': 'HLGEM', 'author_id': 9034, 'author_profile': 'https://Stackoverflow.com/users/9034', 'pm_score': 2, 'selected': False, 'text': "<p>Given what you said, I would rather you get the deveopers properly trained in t-SQl and databases in general than allow them to create posssibly much more damage to performance by allowing them to do t-sql tasks in CLRs. Developers who don't understand databases use that as an excuse to avoid doing things the way that is best for database performance because they want to take what they see as the easier route. </p>\n"}, {'answer_id': 12591788, 'author': 'user1698643', 'author_id': 1698643, 'author_profile': 'https://Stackoverflow.com/users/1698643', 'pm_score': 2, 'selected': False, 'text': "<p>We ran into a situation with a CLR function that was called thousands of times in a regular SQL proc.  This was a proc for importing data from another system. The function validated the data and handled nulls nicely.  </p>\n\n<p>If we did the operation in TSQL, the proc finished in about 15 seconds.  If we used the CLR function, the proc finished in 20 - 40 minutes. The CLR function looked more elegant, but as far as we could tell, there was a startup hit for each use of the CLR function.  So if you have a large operation done using one CLR function, that's fine since the startup time is small compared to the time for the operation.  Or if you a calling the CLR function a modest number of times, the total startup time for all invocations of the function would be small.  But be careful of loops.  </p>\n\n<p>Also, for maintainability, it's nicer not to have more languages than you really need.</p>\n"}, {'answer_id': 24745698, 'author': 'capito', 'author_id': 3833113, 'author_profile': 'https://Stackoverflow.com/users/3833113', 'pm_score': 0, 'selected': False, 'text': '<p>I would add a couple of reasons to use CLR that may not have been mentioned.</p>\n\n<ul>\n<li>Replace and extend basic query, non-query, and scalar sql functions.<br>\nA) Error reporting and alerts can be integrated based upon defined requirements.\nB) Easily define debugging levels.\nC) Enable an easier way to interact with foreign SQL servers  </li>\n<li>Move legacy code to a managed environment.</li>\n</ul>\n'}, {'answer_id': 24812630, 'author': 'Solomon Rutzky', 'author_id': 577765, 'author_profile': 'https://Stackoverflow.com/users/577765', 'pm_score': 0, 'selected': False, 'text': '<p>I posted the following answer to a similar question: <a href="https://stackoverflow.com/questions/439859/advantage-of-sql-server-clr">Advantage of SQL SERVER CLR</a>.\nI will add here though, that C# / VB.net / etc being a language someone is more comfortable with than T-SQL should <em>not</em> be a reason to use SQLCLR over T-SQL.  If someone doesn\'t know how to accomplish something in T-SQL, first ask for help in finding a T-SQL solution. If one does not exist, <em>then</em> go the CLR route.</p>\n\n<hr>\n\n<p>SQLCLR / CLR Integration within SQL Server is just another tool to help solve certain (not all) problems.  There are a few things that it does better than what can be done in pure T-SQL, and there are some things that can only be done via SQLCLR.  I wrote an article for SQL Server Central, <a href="http://www.sqlservercentral.com/articles/Stairway+Series/104406/" rel="nofollow noreferrer">Stairway to SQLCLR Level 1: What is SQLCLR?</a> (free registration is required to read articles there), that addresses this question. The basics are (see the linked article for details):</p>\n\n<ul>\n<li>Streaming Table-Valued Functions (sTVF)</li>\n<li>Dynamic SQL (within Functions)</li>\n<li>Better Access to External Resources / Replace xp_cmdshell\n\n<ul>\n<li>Passing data in is easier</li>\n<li>Getting multiple columns of a result set back is easier</li>\n<li>No external dependencies (e.g. 7zip.exe)</li>\n<li>Better security via Impersonation</li>\n</ul></li>\n<li>Ability to Multi-thread</li>\n<li>Error Handling (within Functions)</li>\n<li>Custom Aggregates</li>\n<li>Custom Types</li>\n<li>Modify State (within a Function and without <code>OPENQUERY</code> / <code>OPENROWSET</code>)</li>\n<li>Execute a Stored Procedure (read-only; within a Function and without <code>OPENQUERY</code> / <code>OPENROWSET</code>)</li>\n<li>Performance (<strong>note:</strong> this is <em>not</em> meaning in all cases, but definitely in some cases depending on the type and complexity of the operation)</li>\n<li>Can capture output (i.e. what is sent to the Messages tab in SSMS) (e.g. <code>PRINT</code> and <code>RAISERROR</code> with a <em>severity</em> = 0 to 10) -- I forgot to mention this one in the article ;-).</li>\n</ul>\n\n<p>One other thing to consider is, sometimes it is beneficial to be able to share code between the app and the DB so that the DB has insight into certain business logic without having to build custom, internal-only screens just to access that app code. For example, I have worked on a system that imported data files from customers and use a custom hash of most of the fields and saved that value to the row in the DB. This allowed for easily skipping rows when importing their data again as the app would hash the values from the input file and compare to the hash value stored on the row. If they were the same then we knew instantly that none of the fields had changed so we went onto the next row, and it was a simple INT comparison. But that algorithm for doing the hash was only in the app code so whether for debugging a customer case or looking for ways to offload some processing to back-end services by flagging rows that had at least one field with changes (changes coming from our app as opposed to looking for changes within a newer import file), there was nothing I could do. That would have been a great opportunity to have a rather simple bit of business logic in the DB, even if not for normal processing; having what amounts to an encoded value in the DB with no ability to understand its meaning makes it much hard to solve problems.</p>\n\n<p>If interested in seeing some of these capabilities in action without having to write any code, the Free version of <a href="http://www.sqlsharp.com/" rel="nofollow noreferrer">SQL#</a> (of which I am the author) has RegEx functions, custom Aggregates (UDAs), custom Types (UDTs), etc.</p>\n'}]
['https://Stackoverflow.com/questions/58190', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/5023/']