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


Stairway to SQLCLR Level 1: What is SQLCLR?


Stairway to SQLCLR Level 1: What is SQLCLR?

Author
Message
Solomon Rutzky
Solomon Rutzky
Say Hey Kid
Say Hey Kid (664 reputation)Say Hey Kid (664 reputation)Say Hey Kid (664 reputation)Say Hey Kid (664 reputation)Say Hey Kid (664 reputation)Say Hey Kid (664 reputation)Say Hey Kid (664 reputation)Say Hey Kid (664 reputation)

Group: General Forum Members
Points: 664 Visits: 2937
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/
copling
copling
SSC Rookie
SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)

Group: General Forum Members
Points: 42 Visits: 78
Is it possible in SQL2012 to reference other dll's within the SQLCLR assembly?

Thanks
currentp
currentp
Grasshopper
Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)

Group: General Forum Members
Points: 13 Visits: 90
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 :-)
copling
copling
SSC Rookie
SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)

Group: General Forum Members
Points: 42 Visits: 78
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?
currentp
currentp
Grasshopper
Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)

Group: General Forum Members
Points: 13 Visits: 90
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.
Solomon Rutzky
Solomon Rutzky
Say Hey Kid
Say Hey Kid (664 reputation)Say Hey Kid (664 reputation)Say Hey Kid (664 reputation)Say Hey Kid (664 reputation)Say Hey Kid (664 reputation)Say Hey Kid (664 reputation)Say Hey Kid (664 reputation)Say Hey Kid (664 reputation)

Group: General Forum Members
Points: 664 Visits: 2937
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/
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45203 Visits: 39925
I always wondered why the FORMAT function was so slow. Now I know. :-)

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Iwas Bornready
Iwas Bornready
SSCrazy Eights
SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)

Group: General Forum Members
Points: 8702 Visits: 885
A great basic article for a novice like me. Thanks.
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