We launched a new IT training category! Check out the 140+ courses now.

Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Using CLR with SQL Server 2012

From: SQL Server: Triggers, Stored Procedures, and Functions

Video: Using CLR with SQL Server 2012

In order to use .NET code in our SQL Server we must first enable CLR Integration. CLR Integration is a default feature, but the default is that it is turned off. So we merely need to turn it on. We will do this by using the sp_configure command. The feature we're interested in configuring is called clr enabled. Notice there is a space in there, no underscore. We want to set that equal to a value of 1, but the syntax here rather than use the equal sign so we use a comma.

Using CLR with SQL Server 2012

In order to use .NET code in our SQL Server we must first enable CLR Integration. CLR Integration is a default feature, but the default is that it is turned off. So we merely need to turn it on. We will do this by using the sp_configure command. The feature we're interested in configuring is called clr enabled. Notice there is a space in there, no underscore. We want to set that equal to a value of 1, but the syntax here rather than use the equal sign so we use a comma.

So this will reconfigure our server to set clr enabled equal to 1. After issuing a command like that you have to issue another command called RECONFIGURE and that should be all we need. We notice at the bottom it says configuration options clr enabled change from 0 to 1. So in other word, it went from turned off to turn on. So that's the first step. CLR is now enabled for the entire instance of SQL Server. There's no way to turn this on and turn it off for one particular database.

It is a setting that applies to the entire server, not just one database. Also, once it turned on it will remain on. So even if you reboot the server when it comes back up, this would still be on. The only way to turn it off would be to issue the same command and use a 0 instead of 1. Now that clr is enabled we can add an assembly. So under myDatabase we have Programmability and the fourth option below that is Assemblies. It should already be one assembly and there a default assembly Microsoft.SqlServer.Types.

We can now add our own assembly. An assembly is basically a DLL that was written in any of the .NET languages. For now I am going to assume that DLL already exists. Let's say it was written by a different developer, although a few movies down the road we're going to be ride in our own DLLs. So we right-click, say New Assembly, and we will have to say where that file is and now have an assembly called Hello World. Again, that's a DLL that was written in C#.

Compiled inside of that assembly is some code to write a stored procedure. So we will now need to tell SQL Server that we want create a new stored procedure based on the C# code. The beginning of the syntax is very similar to other stored procedures. We will need the keyword CREATE and the keyword PROC and then we have to give it a name. Then instead of writing code here we're going to tell the machine that this is external to SQL Server.

So we say EXTERNAL name and then we have to tell it the name. The first part of this is going to be the name of the Assembly which is helloworld. The next part is the name of the class that's inside of the Assembly, which I happen to know is HelloWorldProc. Then the third part is the name of the particular method we're looking for which is also HelloWorld.

So that successfully created a new stored procedure. Again the name in the EXTERNAL name is three parts separated by periods. It's the name of the assembly, dot name of the class, dot name of the method, and we have successfully created a stored procedure. We can go onto our Stored Procedure section and see a new one called test.CLR. That's exactly what I expected, and let's go ahead and execute that.

And it returns the phrase Hello world! Again, we did not write any T-SQL there. All of that was implemented in C#, was compiled into a DLL, we imported the DLL as an assembly, we created a new stored procedure based on that DLL, and now we can execute that stored procedure just the exact same way as any other stored procedure.

Show transcript

This video is part of

Image for SQL Server: Triggers, Stored Procedures, and Functions
 
Expand all | Collapse all
  1. 2m 15s
    1. Welcome
      51s
    2. What you should know
      51s
    3. Using the exercise files
      33s
  2. 11m 1s
    1. Comparing triggers, functions, and procedures
      3m 25s
    2. Why use a stored procedure?
      4m 59s
    3. Why use functions?
      1m 27s
    4. Why use triggers?
      1m 10s
  3. 6m 2s
    1. Configuring your environment
      4m 53s
    2. Downloading and installing a sample database
      1m 9s
  4. 26m 25s
    1. Creating a stored procedure
      2m 46s
    2. Modifying a stored procedure
      2m 34s
    3. Returning data using data sets
      3m 45s
    4. Returning data using cursors
      3m 45s
    5. Using input and output parameters
      5m 24s
    6. Using security and permissions
      5m 24s
    7. Using transactions
      2m 47s
  5. 11m 56s
    1. Creating a user-defined function
      4m 59s
    2. Exploring single-value functions
      4m 18s
    3. Exploring table value functions
      2m 39s
  6. 9m 31s
    1. Using "after" triggers
      3m 47s
    2. Using "instead of" triggers
      2m 9s
    3. Using nested triggers
      1m 38s
    4. Using database-level triggers
      1m 57s
  7. 12m 43s
    1. Exploring a real-world INSERT procedure
      5m 32s
    2. Exploring a real-world UPDATE procedure
      3m 13s
    3. Implementing logging on DELETE
      3m 58s
  8. 19m 38s
    1. Understanding the Common Language Runtime (CLR) and the .NET framework
      1m 52s
    2. Using CLR with SQL Server 2012
      4m 11s
    3. Writing stored procedures with C# .NET
      5m 51s
    4. Writing functions with .NET
      5m 7s
    5. Choosing between T-SQL vs. CLR
      2m 37s
  9. 11m 34s
    1. Creating a basic web form and connecting to a database
      2m 56s
    2. Executing a stored procedure
      2m 4s
    3. Passing parameters
      3m 41s
    4. Getting return values
      2m 53s
  10. 1m 43s
    1. Next steps
      1m 43s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ .

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed SQL Server: Triggers, Stored Procedures, and Functions.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member ?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferences from the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.