Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Trading in xp_cmdshell for SQLCLR (Part 1) - List Directory Contents


Trading in xp_cmdshell for SQLCLR (Part 1) - List Directory Contents

Author
Message
Phil Factor
Phil Factor
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1093 Visits: 2953
I'd be interested to see if anyone has a solution to the problem of preventing a barf when an invalid path is passed to DirectoryInfo. The obvious idea would be to send a RAISERROR through the Pipe to the SQLExecutionContext via ExecuteAndSend(). The error should be caught by the outer exception handling of the TSQL TRY....CATCH block so you can then do something sensible. However, in SQL Server 2005, the error is thrown back to the CLR and handled as SQLExpection. If the SQLException is caught within the CLR and nothing is done within the CLR CATCH block, then the execution will fail in the T-SQL context with an error, instead of being caught by the TSQL-TRY/CATCH Block. (Jens Suessmeyer in http://connect.microsoft.com/SQLServer/feedback/ViewFeedback.aspx?FeedbackID=251376)


Best wishes,

Phil Factor
Simple Talk
noeld
noeld
SSCertifiable
SSCertifiable (7.1K reputation)SSCertifiable (7.1K reputation)SSCertifiable (7.1K reputation)SSCertifiable (7.1K reputation)SSCertifiable (7.1K reputation)SSCertifiable (7.1K reputation)SSCertifiable (7.1K reputation)SSCertifiable (7.1K reputation)

Group: General Forum Members
Points: 7146 Visits: 2048
Although SQLCLR has great power I think it is very important to tell the reader that it also brings MANY undesirable things in.
- Bad Exception handling is just one of the examples.
- Bad routines could cause stability problems even knowing that is a "hosted" CLR
- There is a very high risk of introducing code that is not Server-Side friendly.
- Problems caused by CLR objects are a lot harder to deal with.


* Noel
Jedak
Jedak
SSCrazy
SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)

Group: General Forum Members
Points: 2568 Visits: 1116
Last time I worked on a CLR function for SQL Server, I was unable to find a way to handle an exception and pass an error back through the pipe to SQL Server. Now this was a scalar function versus a table value function, but I'm not sure you can handle the error a pass an exception back through the pipe to SQL Server on in a CLR function. You may be stuck with getting the .NET exception back in TSQL. You can do handle the exception and pass an error back through the pipe in a CLR Stored Procedure, however.
Jonathan Kehayias
Jonathan Kehayias
SSCrazy
SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)

Group: General Forum Members
Points: 2166 Visits: 1807
Phil Factor (2/5/2009)
I'd be interested to see if anyone has a solution to the problem of preventing a barf when an invalid path is passed to DirectoryInfo. The obvious idea would be to send a RAISERROR through the Pipe to the SQLExecutionContext via ExecuteAndSend(). The error should be caught by the outer exception handling of the TSQL TRY....CATCH block so you can then do something sensible. However, in SQL Server 2005, the error is thrown back to the CLR and handled as SQLExpection. If the SQLException is caught within the CLR and nothing is done within the CLR CATCH block, then the execution will fail in the T-SQL context with an error, instead of being caught by the TSQL-TRY/CATCH Block. (Jens Suessmeyer in http://connect.microsoft.com/SQLServer/feedback/ViewFeedback.aspx?FeedbackID=251376)



The clean solution is to return null if the existence check fails:


public static IEnumerable os_directory_info(SqlString path, SqlString filter)
{
DirectoryInfo di = new DirectoryInfo(path.Value);
if (di.Exists)
{
if (filter.IsNull || filter.Value == string.Empty)
return di.GetFileSystemInfos();
else
return di.GetFileSystemInfos(filter.Value);
}
else
{
return null;
}
}



Jonathan Kehayias | Principal Consultant | MCM: SQL Server 2008
My Blog | Twitter | MVP Profile
Training | Consulting | Become a SQLskills Insider
Troubleshooting SQL Server: A Guide for Accidental DBAs
Jonathan Kehayias
Jonathan Kehayias
SSCrazy
SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)

Group: General Forum Members
Points: 2166 Visits: 1807
noeld (2/5/2009)
Although SQLCLR has great power I think it is very important to tell the reader that it also brings MANY undesirable things in.
- Bad Exception handling is just one of the examples.

Actually .NET has very robust exception handling abilities, and if you implement them along with TSQL exception handling there shouldn't be problems.


BEGIN TRY

select *
from dbo.os_directory_info('f:\Start', null)

END TRY
BEGIN CATCH

SELECT error_message()

END CATCH





- Bad routines could cause stability problems even knowing that is a "hosted" CLR


Describe a stability problem that you have encountered in a SAFE/EXTERNAL_ACCESS assembly implementation? If you go UNSAFE all bets are off, but SAFE/EXTERNAL_ACCESS can not cause you stability issues.


- There is a very high risk of introducing code that is not Server-Side friendly.


You also have to run this kind of code with UNSAFE access, so it really is all about how you control what you put into your SQL Server.


- Problems caused by CLR objects are a lot harder to deal with.

Describe a problem caused by a SAFE/EXTERNAL_ACCESS assembly in SQL Server and how it was harder to deal with.

Jonathan Kehayias | Principal Consultant | MCM: SQL Server 2008
My Blog | Twitter | MVP Profile
Training | Consulting | Become a SQLskills Insider
Troubleshooting SQL Server: A Guide for Accidental DBAs
Jonathan Kehayias
Jonathan Kehayias
SSCrazy
SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)

Group: General Forum Members
Points: 2166 Visits: 1807
Phil Factor (2/5/2009)
use of CLR makes it harder for a SQL Server administrator to see what's going on with their SQL Server.

Yes, Agreed! However, I've recently started using NET Reflector with an add-in that allows you to see the source of all the CLR routines currently in the database.
http://www.denisbauer.com/NETTools/SQL2005Browser.aspx
It is a joy to use and it means that the D**ned developers can't hide their code from you, particularly as you can decompile it into VB or C#. and save the source to a file. Power to the DBA!


Depending on how you deploy your Assembly you don't really even need Reflector. For brevities sake I didn't include all the files in the script I provided here, but if you do, then you can get the code from the sys.assembly_files DMV:


declare @content varchar(max)

select @content= cast(content as varchar(max))
from sys.assembly_files
where name = 'os_directory_info.cs'

print @content



Jonathan Kehayias | Principal Consultant | MCM: SQL Server 2008
My Blog | Twitter | MVP Profile
Training | Consulting | Become a SQLskills Insider
Troubleshooting SQL Server: A Guide for Accidental DBAs
Jonathan Kehayias
Jonathan Kehayias
SSCrazy
SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)

Group: General Forum Members
Points: 2166 Visits: 1807
Attached is a new installer that will includes the source file so you can view the code natively in SQL Server, and handles the exception being raised if the path does not exist.

Jonathan Kehayias | Principal Consultant | MCM: SQL Server 2008
My Blog | Twitter | MVP Profile
Training | Consulting | Become a SQLskills Insider
Troubleshooting SQL Server: A Guide for Accidental DBAs
Attachments
Jedak
Jedak
SSCrazy
SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)

Group: General Forum Members
Points: 2568 Visits: 1116
Thanks for the article. The last time I was developing a CLR function/procedure I could not find a good explanation of how to handle the signing and install of the assembly for external access. So, we had to use the trustworthy setting. Now I have a better idea of the key install.

Thanks
eschudy
eschudy
Forum Newbie
Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)

Group: General Forum Members
Points: 3 Visits: 7
The reason's that most DBA's continue to use xp_cmdshell is simple - it's easier. There are numerous examples on the web of how to get a directory listing using xm_cmdshell. I cut and paste, and voila! I have my routine. Using SQLCLR requires someone to know a different language, and all it's data access routines (Fill, etc.). So most DBA's will stick with what they know and since everyone's overworked, will not use something new.

BTW, I write and use SQLCLR for special mathematical functions that we write, so I have nothing against it. But I've been writing C/C++/Vb code since the 80's, and because I'm familiar with the algol-68 derivative languages, it's easy for me to switch back and forth. I don't think you can expect that of most people.
Phil Factor
Phil Factor
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1093 Visits: 2953
Oooh. This is getting very useful. I'd missed out on the advantages of passing string parameters as SQLStrings and the trick of passing back a NULL. It makes perfect sense. Now it has the behaviour that one would want.


Best wishes,

Phil Factor
Simple Talk
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search