Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««12345»»»

Trading in xp_cmdshell for SQLCLR (Part 1) - List Directory Contents Expand / Collapse
Author
Message
Posted Thursday, February 5, 2009 7:13 AM


Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Friday, July 25, 2014 3:19 AM
Points: 577, Visits: 2,503
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
Post #650827
Posted Thursday, February 5, 2009 7:33 AM
SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Tuesday, May 6, 2014 5:51 AM
Points: 6,266, Visits: 2,028
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
Post #650857
Posted Thursday, February 5, 2009 7:36 AM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Friday, July 25, 2014 7:33 AM
Points: 1,843, Visits: 999
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.
Post #650859
Posted Thursday, February 5, 2009 7:37 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Monday, June 23, 2014 11:55 AM
Points: 1,708, Visits: 1,792
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
Post #650860
Posted Thursday, February 5, 2009 7:49 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Monday, June 23, 2014 11:55 AM
Points: 1,708, Visits: 1,792
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
Post #650876
Posted Thursday, February 5, 2009 7:52 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Monday, June 23, 2014 11:55 AM
Points: 1,708, Visits: 1,792
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
Post #650881
Posted Thursday, February 5, 2009 7:58 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Monday, June 23, 2014 11:55 AM
Points: 1,708, Visits: 1,792
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


  Post Attachments 
Install SQLCLRNet_DirectoryBrowser in SQL Server Redone.txt (53 views, 49.32 KB)
Post #650886
Posted Thursday, February 5, 2009 8:07 AM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Friday, July 25, 2014 7:33 AM
Points: 1,843, Visits: 999
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
Post #650902
Posted Thursday, February 5, 2009 8:33 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, July 7, 2009 1:52 PM
Points: 1, 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.
Post #650935
Posted Thursday, February 5, 2009 8:45 AM


Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Friday, July 25, 2014 3:19 AM
Points: 577, Visits: 2,503
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
Post #650943
« Prev Topic | Next Topic »

Add to briefcase ««12345»»»

Permissions Expand / Collapse