Grab Deal : Flat 30% off on live classes + 2 free self-paced courses! - SCHEDULE CALL

Unlocking the Power of CLR Stored Procedures: Question And Answer

Q.1. How Does CLR Stored Procedure Work?

Ans: A CLR stored procedure in SQL Server executes its code within a .NET Common Language Runtime (CLR) instance hosted by the SQL Server. Unlike extrinsic processes like Component Object Model (COM)-based extended stored procedures (XPs), CLR stored procedures run within the context of the server, treating database objects as if they were native and local. However, it still needs to consider the client that calls it remotely. This is opposite to the context in which client and middle-tier ADO.NET code run, where communicating with the database requires a remote connection even if the database is physically on the same computer and the ADO.NET code runs locally.

Q.2. How Does .NET Access Local Objects on The Server or Transmit Data or Messages to The Client?

Ans: .NET does not have a built-in method for accessing local objects on the server or sending data and messages directly to the client. To accomplish these tasks, developers must rely on a separate set of classes found in the "Microsoft.SqlServer.Server" namespace.

Q.3. Who Supplied Microsoft.SqlServer.Server to SQL SERVER 2012?

Ans: The "Microsoft.SqlServer.Server" namespace is provided by the System.Data.dll .NET Framework assembly. It does not require adding any specific notes to the project to use this namespace. The location of this namespace within System.Data.dll emphasizes the tight integration between .NET and SQL Server.

Q.4. What Does The SqlContext Object Do?

Ans: The static SqlContext object provides access to the server-side context in which your code runs in a SQL CLR stored procedure. It also has a client channel, represented by its "Pipe" property, which allows it to output data and text. Developers can use this object to interact with the calling client and perform tasks within the server-side context.

Q.5. What is a SqlPipe Object?

Ans: The SqlPipe object allows the SQL CLR code to transfer data and messages back to the calling client. It has methods like "Send," "SendResultsStart," "SendResultsRow," and "SendResultsEnd" to accomplish this. By using the SqlPipe object, you can send query results or other data from the server to the client, effectively communicating between the SQL CLR code and the calling application.

Q.6. Show The Implementation of The Function spContacts From spTest.cs?

Ans: Unfortunately, the content provided does not include the implementation of the function "spContacts" from "spTest.cs" as mentioned. To provide the implementation, the actual code of the function is needed.

public static void spContacts ()
SqlConnection conn = new SqlConnection("context connection-true"); SqlCommand cm = new SqlCommand("SELECT * FROM Person. Person", conn);
SqlDataReader dr = cm.ExecuteReader();
SqlContext.Pipe.Send("Starting data dump");
SqlContext.Pipe.Send("Data dump complete");

Q.7. How Server-Side Code Uses SqlClient Objects?

Ans: Server-side code uses SqlClient objects in a unique way compared to traditional client applications or middle-tier assemblies. It establishes a context connection with the database using the "context connection=true" connection string, allowing it to run within the context of the SQL CLR assembly. The SqlContext.Pipe object is used to send data back to the client using the "Send" method, allowing the server-side code to communicate with the calling application.

Q.8. How Can You Use The SqlDataRecord and Microsoft.SqlServer.Server Together?

Ans: In a CLR stored procedure, developers can use the SqlDataRecord and Microsoft.SqlServer.Server together to return output sets one row at a time. The SqlDataRecord object allows creating a single row with metadata, which can be sent back to the calling client. This approach is useful when you need to examine the data before returning it or when you want to return custom data structures from the stored procedure.

Q.9. How to Use SqlPipe.Send Using SqlDataRecord and SqlMetaData Objects to Output A Single-Column and Single-Row Result Set From a Stored Procedure?

Ans: To output a single-column and single-row result set from a stored procedure using SqlPipe.Send and SqlDataRecord objects. This involves creating a SqlDataRecord object with a single column defined using SqlMetaData, executing a query to obtain the data, and then using the SqlPipe.Send method to send the data back to the client.

public static void spContactCount()
SqlConnection conn = new SqlConnection("context connection=true");
SqlCommand cm = new SqlCommand("SELECT COUNT(*) FROM Person. Person", conn); SqlDataRecord drc = new SqlDataRecord(new SqlMetaData("ContactCount", SqlDbType.Int));
drc.SetInt32(0, (Int32) cm. ExecuteScalar());

Q.10. Which Namespace Contains the SqlDbType Enumeration?

Ans: The SqlDbType enumeration is part of the "System.Data.SqlTypes" namespace. When writing SQL CLR code, developers must include the appropriate "using" statement to access this namespace.

Q.11. How Can You Send Back Multiple SqlDataRecord Objects?

Ans: To send back multiple SqlDataRecord objects, you can use the "SqlContext" object's "SendResultsStart" method to send the first object and then use the "SendResultsRow" method to send all subsequent SqlDataRecord objects. After sending all the desired objects, call the "SendResultsEnd" method to complete the data transfer.

Q.12. State The usage Guidelines of CLR Stored Procedure?

Ans: The usage guidelines of CLR stored procedures suggest focusing on server-side data access and computation tasks. Avoid writing CLR stored procedures for basic "CRUD" (Create, Read, Update, Delete) operations, as traditional T-SQL stored procedures often perform these tasks more efficiently. Instead, utilize CLR stored procedures for higher-value computations, such as fuzzy search algorithms or data validation using regular expressions. The integration of SQL CLR can be powerful when complex logic and processing are required, leveraging the rich functionality of .NET and the SQL Server environment.


CLR (Common Language Runtime) stored procedures in SQL Server provide a powerful way to execute .NET code within the server's context, allowing developers to leverage the capabilities of .NET and interact with the database in novel ways. By running within the server, CLR stored procedures can treat database objects as local and native, making them particularly useful for performing complex computations and custom data manipulations.

To work with CLR stored procedures, developers need to use the "Microsoft.SqlServer.Server" namespace to access server-side context and interact with the calling client using the "SqlPipe" object. Additionally, the "SqlDataRecord" and "SqlMetaData" objects offer a means to return data row by row, giving flexibility in returning custom result sets.

However, it is crucial to use CLR stored procedures judiciously. While they excel at performing advanced computations and data manipulations, simple CRUD operations are often better handled through traditional T-SQL stored procedures for better performance. It is essential to consider the specific requirements and complexity of the task before deciding to implement a CLR stored procedure.

Trending Courses

Cyber Security

  • Introduction to cybersecurity
  • Cryptography and Secure Communication 
  • Cloud Computing Architectural Framework
  • Security Architectures and Models

Upcoming Class

-0 day 14 Jun 2024


  • Introduction and Software Testing
  • Software Test Life Cycle
  • Automation Testing and API Testing
  • Selenium framework development using Testing

Upcoming Class

-0 day 14 Jun 2024


  • Salesforce Configuration Introduction
  • Security & Automation Process
  • Sales & Service Cloud
  • Apex Programming, SOQL & SOSL

Upcoming Class

-0 day 14 Jun 2024

Business Analyst

  • BA & Stakeholders Overview
  • BPMN, Requirement Elicitation
  • BA Tools & Design Documents
  • Enterprise Analysis, Agile & Scrum

Upcoming Class

-0 day 14 Jun 2024

MS SQL Server

  • Introduction & Database Query
  • Programming, Indexes & System Functions
  • SSIS Package Development Procedures
  • SSRS Report Design

Upcoming Class

-0 day 14 Jun 2024

Data Science

  • Data Science Introduction
  • Hadoop and Spark Overview
  • Python & Intro to R Programming
  • Machine Learning

Upcoming Class

7 days 21 Jun 2024


  • Intro to DevOps
  • GIT and Maven
  • Jenkins & Ansible
  • Docker and Cloud Computing

Upcoming Class

1 day 15 Jun 2024


  • Architecture, HDFS & MapReduce
  • Unix Shell & Apache Pig Installation
  • HIVE Installation & User-Defined Functions
  • SQOOP & Hbase Installation

Upcoming Class

7 days 21 Jun 2024


  • Features of Python
  • Python Editors and IDEs
  • Data types and Variables
  • Python File Operation

Upcoming Class

-0 day 14 Jun 2024

Artificial Intelligence

  • Components of AI
  • Categories of Machine Learning
  • Recurrent Neural Networks
  • Recurrent Neural Networks

Upcoming Class

15 days 29 Jun 2024

Machine Learning

  • Introduction to Machine Learning & Python
  • Machine Learning: Supervised Learning
  • Machine Learning: Unsupervised Learning

Upcoming Class

28 days 12 Jul 2024


  • Introduction to Tableau Desktop
  • Data Transformation Methods
  • Configuring tableau server
  • Integration with R & Hadoop

Upcoming Class

7 days 21 Jun 2024