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 ««12

Stairway to SQLCLR Level 1: What is SQLCLR? Expand / Collapse
Author
Message
Posted Thursday, January 9, 2014 2:41 PM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: 2 days ago @ 2:01 PM
Points: 368, Visits: 1,956
currentp (1/9/2014)
Great Article. I have been writing SQL Server external objects since 2007 and I have developed some best practices that work for me. You talked on most of them and I wanted to elaborate at bit on one of them.

Do as much of the operation in T-SQL that you can and write small external objects (SQLCLR tasks) to perform the tasks that cannot be done in T-SQL.

...

I hope my trial and errors help someone.


Hi there. Thank you for the compliment and I am glad you enjoyed the article. Thank you also for sharing your experience with the community as I think it is very helpful to see the practical side of these issues and that some of these decisions can be rather impacting on our systems.

I do feel bad though, that you ever had to do COM / OLE Automation programming. I never have done anything with COM and have rarely ever used the sp_OA* procs, so it is also nice to see some of the issues there.

Thanks again and take care,
Solomon...





SQL# - http://www.SQLsharp.com/
Post #1529576
Posted Friday, January 10, 2014 6:28 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Friday, September 12, 2014 6:00 AM
Points: 29, Visits: 60
Is it possible in SQL2012 to reference other dll's within the SQLCLR assembly?

Thanks
Post #1529755
Posted Friday, January 10, 2014 3:21 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, July 30, 2014 11:44 AM
Points: 7, Visits: 70
I’m assuming that the DLL is not part of the .Net framework. If my assumption is correct then the DLL needs to be added to SQL Server as an assembly prior to your assembly being installed. Once the third party DLL is an assembly within SQL Server, then SQL Server “knows” about it and it could be referenced by another external procedure. Here is an example script.

EXECUTE sp_configure 'show advanced options' , '1';
RECONFIGURE WITH OVERRIDE;
GO

EXECUTE sp_configure 'clr enabled', '1';
RECONFIGURE WITH OVERRIDE;
GO

-- Beacause the assembly is accessing a third party DLL, the database needs to be set to TRUSTWORTHY
ALTER DATABASE [JUNK] SET TRUSTWORTHY ON
GO

USE [JUNK]
GO

-- Create assembly using thrid party DLL
CREATE ASSEMBLY ThirdPartySDK
FROM 'C:\Program Files\SomeProgram\SDK.dll'
WITH PERMISSION_SET = UNSAFE;
GO

-- Create external function that references the SDK.DLL installed above
CREATE FUNCTION fnGetValue
(
@Timestamp As DateTime,
@UserName As nVarchar(256) = '',
@Password As nVarchar(256) = ''
)
RETURNS [sql_variant] WITH EXECUTE AS CALLER
AS
EXTERNAL NAME [MyExtendedProcedures].[Functions].[fnGetValue]
GO

GRANT EXECUTE ON [dbo].[fnGetValue] TO Public
GO


-- Solomon Rutzky - I hope I'm not stealing your thunder
Post #1529990
Posted Friday, January 10, 2014 3:42 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Friday, September 12, 2014 6:00 AM
Points: 29, Visits: 60
Thanks for the reply. But what if the dll is a .net dll? You know in .net one assembly can have reference to another .net assembly; can the same functionality be done for a SQLCRL assembly?
Post #1529995
Posted Friday, January 10, 2014 3:49 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, July 30, 2014 11:44 AM
Points: 7, Visits: 70
While developing the project in Visual Studio, reference the DLL as you would normally. The issue is when you try to install the assembly. If the .Net DLL is classified as Safe, then SQL Server already knows about it. If SQL Server classifies it as not safe, then you would treat it the same way as a third party DLL.
Post #1529996
Posted Friday, January 10, 2014 4:09 PM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: 2 days ago @ 2:01 PM
Points: 368, Visits: 1,956
copling (1/10/2014)
Thanks for the reply. But what if the dll is a .net dll? You know in .net one assembly can have reference to another .net assembly; can the same functionality be done for a SQLCRL assembly?


Hi there. I will cover this concept in a future article (or 2), but the simple answer is "Yes" and it is the same in all versions of SQL Server since SQLCLR was introduced in SQL 2005.

The more complicated answer is: as usual, you need to add a reference to the DLL in your project. Now, if the DLL is a .Net framework DLL, then not all of them are natively available in SQL Server's restricted CLR. There is a list of which ones are valid and it is a rather short list. You can still use non-validated framework DLLs, but you need to import them into SQL Server in the manner shown by "currentp" and that can only be done if they are marked as UNSAFE. Does this answer the question?




currentp (1/10/2014)
...
-- Solomon Rutzky - I hope I'm not stealing your thunder


Nope, not at all. Anyone is welcome to answer questions .

Take care,
Solomon..





SQL# - http://www.SQLsharp.com/
Post #1529999
« Prev Topic | Next Topic »

Add to briefcase ««12

Permissions Expand / Collapse