Title Image

Don Xml's Grok This

The home of Don Demsak
Welcome to Don Xml's Grok This Sign in | Help
in Search

This Blog

Syndication

Site Sponsors

DonXml's All Things Techie

First Rule Of CLR Stored Procedures

Recently, eWeek author Lisa Vaas emailed my wife regarding an article she was writing about CLR Stored Procedures in SQL Server 2005.  Well, my wife didn’t get back to her, and I’m sort of glad, since the article was released with the title: DBAs Bar Door Against Big Bad .Net Wolf, and is very misleading.  Basically, Lisa tries to state that all .Net programmers are ignorant of T-SQL and will wind up using CLR Stored Procs instead of bothering to learn T-SQL.  Yes, there are developers that have never learned SQL (i.e. former Access developers), and when they learned .Net continued to ignore T-SQL.  These are the folks Lisa seems to be talking about, but I wouldn’t group them with the rest of the .Net developers.  They are the same folks that don’t understand concurrency and try to open one connection and keep it open for the whole life of the application.  These are folks that don’t write enterprise ready applications, and border on the fringe of the “hobbyist” developer (I wouldn’t even call them Morts).  That being said, yes CLR Stored Procs can be a crutch that developers can use rather then learning how to do it correctly with a language designed around querying and manipulating relational data, T-SQL (what I would call a domain specific language, in this case the domain being querying and manipulating relational data).

Kimberly Tripp has stated (and I agree with her) that the 2 most abused features of SQL Server 2005 will be CLR Stored Procs, and XML data types, and to help developers decide if a CLR Stored Proc is needed,

I have this one rule to follow regarding CLR Stored Procs

If you would not have built it as an extended stored proc in SQL Server 2000, odds are that you don’t need to create a CLR Stored Proc in SQL Server 2005.

MSDN has a very good article that goes into more detail behind the process of determining if you need a CLR Stored Proc, if you need more details.  Oh, and if you don’t know what an extended stored proc is, you really don’t need a CLR stored proc.

That being said, I’ve got one killer implementation of a CLR stored proc in the works (and should be ready for the PDC).  It’s code name is Ramapo, and uses this open source project as it’s core.

update:

This post seems to have cause a bit of a conversation arounnd CLR Sprocs, which is a good thing.  Some are misinterpreting my rule that you should not use CLR Sprocs, which is not the case.  The original format of the rule (which I talked about at the April Philly Code Camp) was a little more lengthy, and I trimmed it up to help people to remember it, but I think I lost something important when I restructured it.  It was something like:

If you have never had to coded an Extended Stored Proc, then you probably don't need a CLR stored proc.  Extended stored procs were a real pain, and if you would have used them, except for things like security, then you may want to take a look at CLR sprocs.  Otherwise, play with extended sprocs first, feel the pain, and then you will have a better idea when the use of CLR stored procs is warranted.

Published Tuesday, August 30, 2005 11:52 AM by donxml
Filed under: ,

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

Sahil Malik said:

"If you would not have built it as an extended stored proc in SQL Server 2000, odds are that you don’t need to create a CLR Stored Proc in SQL Server 2005."

I completely and vehemently disagree with this rule. There are certain situations where you may want to use SQLCLR, because Extended Stored procedures were just not the right tool for it, or simply because they weren't managed code. In certain instances, SQLCLR is actually preferable over T-SQL
August 30, 2005 1:58 PM

Don Demsak said:

The only reasons I know of that warrant the use of SQLCLR are when dealing with non-relational data stores, which is why you would have used extended sprocs. I do not believe that a developer should use CLR StoredProcs because they do not wish to host their .Net in another container (IIS, Service). There are reasons to break this rule, but think long and hard before you do it.
August 30, 2005 2:05 PM

Sahil Malik said:

Don I posted something on my blog http://codebetter.com/blogs/sahil.malik/archive/2005/08/30/131435.aspx

But I will mention a few reasons why you cannot compare XPs to SQLCLR.

1. Context Connections - XPs don't have 'em.
2. System.Transactions - XPs don't have that integration
3. .NET Framework - XPs don't have 'em
4. Can you write an extended trigger?
5. Do XPs work with Code Access Security?
6. .. other reasons ..

In short, SQLCLR != XP, you cannot and should not compare the two.
August 30, 2005 2:13 PM

Don Demsak said:

None of your reasons explain why to use CLR Stored Procs, just why CLR Stored Procs are better than XP (which is a correct statement, CLR Stored Procs are a much more improved version of XP). If you didn't use XP, you probably don't need to use CLR Stored Procs.
August 30, 2005 2:19 PM

Sahil Malik said:

None of your reasons explain why to use CLR Stored Procs ---

Again I disagree :). Let me complete the above sentences for you.

1. Context Connections - XPs don't have 'em.

Context Connections provide significant architectural change. XPs would need to create a loop back connection which is just as expensive as a connection from outside the database. Earlier you had NO OPTION but to ferry data to the data layer/business layer to do computationally intensive tasks - with XP you still had to, with SQLCLR you Donot.

2. System.Transactions - XPs don't have that integration

System.Transactions integration again lets you do things that XPs didn't allow you to do. Can you have an XP procedure that uses Lightweight Transaction Manager and promotable enlistment and tie it with a SqlTransaction object at the client end? No you cannot. SQLCLR will let you do that, XP won't.

.. do I really need to type explanations for all of the below?

3. .NET Framework - XPs don't have 'em
4. Can you write an extended trigger?
5. Do XPs work with Code Access Security?
August 30, 2005 2:24 PM

Don Demsak said:

I understand all your points on why XP is not CLR Stored Procs, but why are you putting Domain logic in your data tier? That is the point of this rule, determine IF you should put domain logic in the data tier. There are reasons to do it, but if you didn't look at XP "back in the day", then odds are you don't need it in SQL 2005. I'm trying to prevent people from using CLR Sprocs instead of bothering to learn T-SQL.
August 30, 2005 2:35 PM

Sahil Malik said:

Hey I "COMPLETELY" Agree with your viewpoint of "Learn TSQL", and yes there is a danger of developers overusing SQLCLR (like they did with Windows 3.1 fonts). So there is no argument there.

Now there are certain instances where you may need to put logic inside your database. Lets say certain procedural calculation that is done better in CLR, required tomes of data to be ferried from the database. You could simply use a context connection and get the final result out. Another instance is where you want a stored procedure to do a non-database operation (write to this archaic IBM mainframe when you are done processing this job), and then you may want to tie that with COMTI, those and others are perfect candidates for SQLCLR, and not for XP. I'm just shooting from the hip here, I am sure there are other instances where SQLCLR is a great alternative to both TSQL and XP :)

But I do agree when you say "Learn TSQL, and try doing it in TSQL first"
August 30, 2005 2:56 PM

Randy Holloway said:

manual trackback
August 30, 2005 11:30 PM

Kent Tegels said:

manual trackback
August 31, 2005 3:29 PM

Bruce Johnson said:

Bruce's take
August 31, 2005 8:06 PM

Paul Lorena said:

"...I understand all your points on why XP is not CLR Stored Procs, but why are you putting Domain logic in your data tier? " I think there is not incompatibility to put business logic in the Data Base (store procs, or functions with some business logic (SQLCLR o TSLQ), maybe there is a misunderstood between the concept of "tier" and "layer"
January 27, 2007 5:31 PM

Kirk Allen Evans's Blog said:

I just got off the phone with a customer, a huge telco, that is in the process of rearchitecting some
September 19, 2007 5:20 PM

Leave a Comment

(required) 
(optional)
(required) 
Submit

About donxml

I’m an independent consultant, specializing in .Net solutions architecture, based out of New Jersey who also doubles as an evangelist for XML, Domain Driven Design, enterprise architecture and .Net. I do not work for Microsoft, the W3C or any other big company that you may know of (at least not yet). I’ve been an indie for over ten years, and although I’ve been tempted a couple times to take a job with companies like Microsoft, I’ve haven’t found something better than my current situation. I work mostly with the large pharmaceuticals that are based here in New Jersey, and usually find myself on long term contracts. Definitely not the prototypical indie consultant, but it lets me dedicate time to my non-income generating activities like the developer community stuff, plus financing open source projects like XPathmania and MVP-XML. If you would like to talk to me about doing some contract work, just contact me via the contact page. My rates vary widely, depending on lots of different variables, but mostly distance from Jersey, and type of work. Plus, I’ve been known to donate some of my code for various projects.
Powered by Community Server, by Telligent Systems