Start your free trial now, and begin learning software, business and creative skills—anytime, anywhere—with video instruction from recognized industry experts.

Start Your Free Trial Now

Writing functions with .NET


SQL Server: Triggers, Stored Procedures, and Functions

with Martin Guidry

Video: Writing functions with .NET

Writing functions with .NET provides you with in-depth training on Business. Taught by Martin Guidry as part of the SQL Server: Triggers, Stored Procedures, and Functions
Expand all | Collapse all
  1. 2m 15s
    1. Welcome
    2. What you should know
    3. Using the exercise files
  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. Using input and output parameters
      5m 24s
    5. Returning data using cursors
      3m 45s
    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 35s
    1. Creating a basic web form and connecting to a database
      2m 57s
    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

please wait ...
Watch the Online Video Course SQL Server: Triggers, Stored Procedures, and Functions
Video Duration: 5m 7s1h 52m Advanced Sep 24, 2012

Viewers: in countries Watching now:

View Course Description

This course investigates several key database-programming concepts: triggers, stored procedures, functions, and .NET CLR (Common Language Runtime) assemblies. Author Martin Guidry shows how to combine these techniques and create a high-quality database using Microsoft SQL Server 2012. The course also covers real-world uses of the INSERT, UPDATE, and DELETE procedures, and how to build a basic web form to connect to your database.

Topics include:
  • Comparing triggers, functions, and stored procedures
  • Installing and configuring SQL Server
  • Creating a stored procedure
  • Returning data using data sets
  • Creating user-defined functions
  • Using "after," "instead," and nested triggers
  • Modifying existing stored procedures
  • Implementing logging on DELETE
  • Choosing between T-SQL and CLR
  • Executing a stored procedure
  • Passing parameters
Business Developer IT
SQL Server
Martin Guidry

Writing functions with .NET

In this section I am going to talk about using C# to write a user defined function. So here in Visual Studio we'll create a new project. Again, I want it to be of type Class Library and I'll call it a CLRfunctions. I prepared some code ahead of time that you can find in your exercise files, so I'll copy and paste that over to Visual Studio and we can talk about it. The top eight lines are typical using statements that we have with any SQL Server stored procedure or function, and then I created a new class, starting on line 10, public class myFunctions. There is one method in there called isActive, it is decorated with Microsoft.Sql.Server .Server.Sql.Function. This will provide a clue to the compiler that we are in fact writing a SQL function.

Notice on line 13 we declared the method to be public static and the Return type is SqlString. When we're working with C# code, that's going to eventually be executed inside of SQL Server, we have to use slightly different data types, rather than use things like sting, an integer, we've to specify it as a SqlString or a SqlInteger. So we work with slightly different data types. Here we have SqlIntegar as the Return type of the function, and we're passing one parameter called ActiveBit and that is a SqlInt, in this case at 32bit SqlInteger, so SqlInt32.

The point of this function, it is going to read the bit, the 1 or 0 we used to store whether or not someone is active, and just return something that's a little more friendly, return some text that actually spells out the word Active or Not Active, based on the value of that bit. So we see some fairly straightforward logic; line 15, 16, 17 and 18, a simple if statement, if (ActiveBit == 1) we'll return the phrase Active, else, we return the phrase Not Active.

So this is all the code we need. We go ahead and Build Solution, and in the lower left we see the build succeeded. That's certainly good news. Now we'll need to save all of this in a place that's easy to find, and now that should be everything we need to do in Visual Studio. We will flip over to SQL Server Management Studio, we will need to import the New Assembly, and we'll have to browse to where we just dropped that. We successfully created the assembly CLRFunctions.

So now it's time to create our function. I have again pre-staged some code for you that's in your exercise files. We will copy and paste that. We see it says CREATE FUNCTION, we gave it a name, and then it's going to take one parameter. That parameter is an integer. We say it RETURNS NVARCHAR. So we talked earlier a little bit about the differences in data types, C# is going to return what it calls a SqlString. SQL recognizes is that as the NVARCHAR data type, and we'll have to match those up.

If those didn't match we would get an error when creating this function. And in the bottom part EXTERNAL NAME, which again goes assemblyname.classname.method, so that CLRfunctions.myFunctions.isActive. We execute that. It says Command(s) completed successfully. That's good news. I can come over here in my Functions section and expand out Scalar-valued Functions, hit a quick refresh, and I see isActive is now a valid function.

So now we're going to write a SELECT statement that actually uses that. So I want to SELECT FROM the Authors table. And I want to select three things: FirstName, LastName, and this new function we just created. That takes one input parameter and that parameter will be the ActiveBit.

We run that, and we see every line Returns Not Active, which is in fact accurate. All of our Authors are In Active, based on things we did in previous demos. This might be a little bit more interested if we had an author who was active, so let's go ahead and do that. We'll make John Doe, we'll make him Active, and we'll run this again. We see our function is operating properly. Anytime the Active field is equal to 1, our function writes the phrase Active, and anytime the Active field is not equal to 1, it writes the phrase Not Active.

There are currently no FAQs about SQL Server: Triggers, Stored Procedures, and Functions.

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.

* Estimated file size

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


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

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


Upgrade to View Courses Offline


With our new Desktop App, Annual Premium Members can download courses for Internet-free viewing.

Upgrade Now

After upgrading, download Desktop App Here.

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 ?

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:

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.

Learn more, save more. Upgrade today!

Get our Annual Premium Membership at our best savings yet.

Upgrade to our Annual Premium Membership today and get even more value from your subscription:

“In a way, I feel like you are rooting for me. Like you are really invested in my experience, and want me to get as much out of these courses as possible this is the best place to start on your journey to learning new material.”— Nadine H.

Start your FREE 10-day trial

Begin learning software, business, and creative skills—anytime,
anywhere—with video instruction from recognized industry experts. provides
Unlimited access to over 4,000 courses—more than 100,000 video tutorials
Expert-led instruction
On-the-go learning. Watch from your computer, tablet, or mobile device. Switch back and forth as you choose.
Start Your FREE Trial Now

A trusted source for knowledge.


We provide training to more than 4 million people, and our members tell us that helps them stay ahead of software updates, pick up brand-new skills, switch careers, land promotions, and explore new hobbies. What can we help you do?

Thanks for signing up.

We’ll send you a confirmation email shortly.

Sign up and receive emails about 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

Sign up and receive emails about 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.