Stairway to SQLCLR Level 1: What is SQLCLR?

  • Comments posted to this topic are about the item Stairway to SQLCLR Level 1: What is SQLCLR?

    SQL#https://SQLsharp.com/ ( SQLCLR library ofover 340 Functions and Procedures)
    Sql Quantum Lifthttps://SqlQuantumLift.com/ ( company )
    Sql Quantum Leaphttps://SqlQuantumLeap.com/ ( blog )
    Info sitesCollations     •     Module Signing     •     SQLCLR

  • Very timely for me. Another programmer deployed a CLR function about a year ago and I don't know anything about it. I've been saying I was going to learn C#, but have been remiss. I need to catch up, so I'm looking forward to the follow-up articles. Thanx!

    Wallace Houston
    Sunnyland Farms, Inc.

    "We must endeavor to persevere."

  • Looking forward to more steps. I need to do an EDI import routine, and think a CLR will fit he bill. Thanks!

    [font="Verdana"]Please don't go. The drones need you. They look up to you.[/font]
    Connect to me on LinkedIn

  • I am glad to see a reasonable and balanced approach to the article. Well written!

    John.

  • Caruncles (1/9/2014)


    Very timely for me. Another programmer deployed a CLR function about a year ago and I don't know anything about it. I've been saying I was going to learn C#, but have been remiss. I need to catch up, so I'm looking forward to the follow-up articles. Thanx!

    Hi there. You are quite welcome.

    I assume you have the source code for that CLR function? Also, just to be clear: the goal of this series is to show how to work with the CLR Integration of .Net within SQL Server, not to educate on the topic of C#. I would recommend doing what you can to learn C# in the meantime so that you will have a better understanding of what the function is doing outside the scope of SQL Server interaction and nuances.

    Take care,

    Solomon...

    SQL#https://SQLsharp.com/ ( SQLCLR library ofover 340 Functions and Procedures)
    Sql Quantum Lifthttps://SqlQuantumLift.com/ ( company )
    Sql Quantum Leaphttps://SqlQuantumLeap.com/ ( blog )
    Info sitesCollations     •     Module Signing     •     SQLCLR

  • Thomas Abraham (1/9/2014)


    Looking forward to more steps. I need to do an EDI import routine, and think a CLR will fit he bill. Thanks!

    You're welcome!

    And just FYI, it will be a few more levels before we get to the specifics of creating Functions and Stored Procedures. There is a little more foundational content to go over first. But, by the time we get to those two levels I think you will have a good grasp of how to accomplish that project (assuming, of course, you know how to get the EDI data :-)).

    Take care,

    Solomon...

    SQL#https://SQLsharp.com/ ( SQLCLR library ofover 340 Functions and Procedures)
    Sql Quantum Lifthttps://SqlQuantumLift.com/ ( company )
    Sql Quantum Leaphttps://SqlQuantumLeap.com/ ( blog )
    Info sitesCollations     •     Module Signing     •     SQLCLR

  • Yes, I have the source code and am aware that learning C# is not the focus of this topic. I've needed the C# to begin re-writes of custom VB6 programs which I'm finding won't run on 64-bit Windows 7 boxes. So, yes, I need to learn both, but your article is a good motivator for me. Thanx!

    Wallace Houston
    Sunnyland Farms, Inc.

    "We must endeavor to persevere."

  • Thomas Abraham (1/9/2014)


    Looking forward to more steps. I need to do an EDI import routine, and think a CLR will fit he bill. Thanks!

    Thank you (on all 3 counts)!

    Take care,

    Solomon...

    SQL#https://SQLsharp.com/ ( SQLCLR library ofover 340 Functions and Procedures)
    Sql Quantum Lifthttps://SqlQuantumLift.com/ ( company )
    Sql Quantum Leaphttps://SqlQuantumLeap.com/ ( blog )
    Info sitesCollations     •     Module Signing     •     SQLCLR

  • Caruncles (1/9/2014)


    Yes, I have the source code and am aware that learning C# is not the focus of this topic. I've needed the C# to begin re-writes of custom VB6 programs which I'm finding won't run on 64-bit Windows 7 boxes. So, yes, I need to learn both, but your article is a good motivator for me. Thanx!

    No problem. I just needed to make sure so there was no potential for misunderstanding.

    Good luck on learning C# and helping to remove VB6 from the planet ;-).

    Take care,

    Solomon..

    SQL#https://SQLsharp.com/ ( SQLCLR library ofover 340 Functions and Procedures)
    Sql Quantum Lifthttps://SqlQuantumLift.com/ ( company )
    Sql Quantum Leaphttps://SqlQuantumLeap.com/ ( blog )
    Info sitesCollations     •     Module Signing     •     SQLCLR

  • 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. In my experience, if a task could be done in T-SQL, it will always perform faster than in an external object. I have to admit that the article talked about some scenarios that I have not encounter yet, so testing is still the best way to truly know which has better performance.

    Many moons ago I had a project to expose data in an external non-relational proprietary database within SQL Server so DBAs could perform aggregations on this data and also create reports on it , along with many other benefits of having the data in SQL Server (like BI).

    Because the proprietary database had a COM SDK, I first tried using the sp_OA procedures. The T-SQL sp_OA procedures (OA stands for OLE Automation) allow SQL Server to run methods in a COM object. This method works for calls that have parameters with data types that map to SQL Server data types, like INTEGER and STRING, but not methods with parameters that do not, like ARRAY and OBJECT. This method did not work for me because the COM object used the data type of OBJECT to pass dates in.

    Because I was using SQL Server 2005 I could perform the operation using the SQLCLR. I wrote an operation to connect to the external non-relational proprietary database and get the required data all via the SDK. I wrote the whole operation as one external stored procedure. I was floored when the execution took over 30 minutes to run. After investigating the cause, I discovered that SQL Server did some pre-process tasks that took about 30 minutes to run and the actual tasks I wrote took 3 seconds. Further investigation lead me to believe that SQL Server tries to understand what is required if a rollback is called. I then changed the architecture of the operation to have multiple small SQLCLR tasks with one T-SQL to call them, process the data and then return the results. With this architecture, the tasks run in 3 seconds.

    I hope my trial and errors help someone.

  • 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#https://SQLsharp.com/ ( SQLCLR library ofover 340 Functions and Procedures)
    Sql Quantum Lifthttps://SqlQuantumLift.com/ ( company )
    Sql Quantum Leaphttps://SqlQuantumLeap.com/ ( blog )
    Info sitesCollations     •     Module Signing     •     SQLCLR

  • Is it possible in SQL2012 to reference other dll's within the SQLCLR assembly?

    Thanks

  • 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 🙂

  • 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?

  • 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.

Viewing 15 posts - 1 through 15 (of 18 total)

You must be logged in to reply to this topic. Login to reply