Logo

Navigation
  • Home
  • Services
    • ERP Solutions
    • Implementation Solutions
    • Support and Maintenance Solutions
    • Custom Solutions
    • Upgrade Solutions
    • Training and Mentoring
    • Web Solutions
    • Production Support
    • Architecture Designing
    • Independent Validation and Testing Services
    • Infrastructure Management
  • Expertise
    • Microsoft Development Expertise
    • Mobile Development
    • SQL Server Database and BI
    • SAP BI, SAP Hana, SAP BO
    • Oracle and BI
    • Oracle RAC
  • Technical Training
    • Learn Data Management
      • Business Intelligence
      • Data Mining
      • Data Modeling
      • Data Warehousing
      • Disaster Recovery
    • Learn Concepts
      • Application Development
      • Client Server
      • Cloud Computing Tutorials
      • Cluster Computing
      • CRM Tutorial
      • EDI Tutorials
      • ERP Tutorials
      • NLP
      • OOPS
      • Concepts
      • SOA Tutorial
      • Supply Chain
      • Technology Trends
      • UML
      • Virtualization
      • Web 2.0
    • Learn Java
      • JavaScript Tutorial
      • JSP Tutorials
      • J2EE
    • Learn Microsoft
      • MSAS
      • ASP.NET
      • ASP.NET 2.0
      • C Sharp
      • MS Project Training
      • Silverlight
      • SQL Server 2005
      • VB.NET 2005
    • Learn Networking
      • Networking
      • Wireless
    • Learn Oracle
      • Oracle 10g
      • PL/SQL
      • Oracle 11g Tutorials
      • Oracle 9i
      • Oracle Apps
    • Learn Programming
      • Ajax Tutorial
      • C Language
      • C++ Tutorials
      • CSS Tutorial
      • CSS3 Tutorial
      • JavaScript Tutorial
      • jQuery Tutorial
      • MainFrame
      • PHP Tutorial
      • VBScript Tutorial
      • XML Tutorial
    • Learn Software Testing
      • Software Testing Types
      • SQA
      • Testing
  • Career Training
    • Career Improvement
      • Career Articles
      • Certification Articles
      • Conflict Management
      • Core Skills
      • Decision Making
      • Entrepreneurship
      • Goal Setting
      • Life Skills
      • Performance Development
      • Personal Excellence
      • Personality Development
      • Problem Solving
      • Relationship Management
      • Self Confidence
      • Self Supervision
      • Social Networking
      • Strategic Planning
      • Time Management
    • Education Help
      • Career Tracks
      • Essay Writing
      • Internship Tips
      • Online Education
      • Scholarships
      • Student Loans
    • Managerial Skills
      • Business Communication
      • Business Networking
      • Facilitator Skills
      • Managing Change
      • Marketing Management
      • Meeting Management
      • Process Management
      • Project Management
      • Project Management Life Cycle
      • Project Management Process
      • Project Risk Management
      • Relationship Management
      • Task Management
      • Team Building
      • Virtual Team Management
    • Essential Life Skills
      • Anger Management
      • Anxiety Management
      • Attitude Development
      • Coaching and Mentoring
      • Emotional Intelligence
      • Stress Management
      • Positive Thinking
    • Communication Skills
      • Conversation Skills
      • Cross Culture Competence
      • English Vocabulary
      • Listening Skills
      • Public Speaking Skills
      • Questioning Skills
    • Soft Skills
      • Assertive Skills
      • Influence Skills
      • Leadership Skills
      • Memory Skills
      • People Skills
      • Presentation Skills
    • Finding a Job
      • Etiquette Tips
      • Group Discussions
      • HR Interviews
      • Interview Notes
      • Job Search Tips
      • Resume Tips
      • Sample Resumes
 

NET CLR in SQL Server 2005

By Exforsys | on November 1, 2005 |
SQL Server 2005

Using .NET CLR in SQL Server 2005

In this tutorial we will introduce the concepts relating to CLR integration with SQL Server 2005 including how to implement managed code.

New types and aggregates extend the functionality of SQL Server 2005. The Common Language Runtime enables developers write procedures, triggers and functions in any of the languages supported. SQL server CLR integration is achieved in a few steps:

1. The developer has to write the managed code as a set of class definitions. These include stored procedures, functions, triggers written as static methods of a class. User defined types and aggregates are written as classes and the code is created and compiled as an assembly.

2. The assembly is then uploaded into the SQL server database and stored in the System catalog using the CREATE ASSEMBLY data definition language.

3. Thereafter, T-SQL objects—routines, types and aggregates—are created and bound to entry points within the assembly. CREATE PROCDEDURE/ FUNTION/TRIGGER/TYPE/AGGREGATE statements are used.

4. These routines etc then function like T-SQL routines.

Development, deployment and debugging of managed code in SQL Server 2005 is made easy by the new SQL Server Project template. The project is compiled into an assembly and when the project is deployed the assembly is uploaded as a binary assembly into the SQL Server database associated with the project. The process of deployment also automatically creates routines, types and aggregates in the database. These are based on the custom aggregates defined in the code. The source code and the debugging symbols are also uploaded with the assembly. Significantly debugging is a key feature in SQL Server 2005 and the debugging works seamlessly allowing the user to step from CLR methods to T-SQL and vice versa.

References to other assemblies in the data base can also be added.

CLR support is intended to augment the expressive power of T-SQL query language. It enhances the power to write recursive queries, to use analytical functions such as RANK and ROW_NUMBER and new relational operators such as EXCEPT, INTERSECT, APPLY, PIVOT and UNPIVOT. Developers can take advantage of the extensive libraries of .NET framework API and utilize the rich data structures such as arrays, lists etc. The computational power of CLR can be harnessed to strengthen the T-SQL.

Data access codes written in CLR is verbose compared to those written in T-SQL. The programming models use similar query language but are different in procedural portions.

The SQL statement written in CLR supported languages are not compiled or validated till they are executed and hence debugging and performance are impacted. The model is similar to that of the ADO.NET and is used in the client or middle tiers to leverage existing skills.

It is advisable that T-SQL procedural and row based processing should be used only when the logic is not expressible using the declarative language. If forward only, read only row navigation is involved in a result set with processing for each row, it is better to use CLR. If data access and computation are involved, the procedural code should be divided into a CLR portion and a T-SQL portion.

CLR provides an efficient alternative to XPs. The stored procedures can be expressed better as table valued functions and can be manipulated using query language. It provides granular control, reliability, Data access, additional data types and scalability. It also gives a performance advantage as it functions within the SQL Server and reduces the delay that occurs due to the transitions between managed code and native code.

CLR also enables developers to place their code outside the database and select the language of their choice. This programming logic can be moved into the database and reduce the amount of data flowing on the network. This helps in data validation, network traffic reduction and the CLR ensures that the programming language does not interfere with the right code location decision.

Database Programming tasks

A number of database tasks and problems have been solved by CLR integration. Data validation using the .NET framework library helps augment the T-SQL built in function library with useful functions.

The developer now has the option of writing stored procedures and table valued functions to return result sets in T-SQL or using CLR. The “caller pipe” within the stored procedure needs to be properly defined in T-SQL. When the stored procedure is invoked fro any client data access API the caller will return the result set to the invoking API bypassing all the T-SQL frames on the stack. SQL Server 2005 introduces a new type of caller. The result set is made available through the SqlDataReader object when the query is executed using the ADO.NET provider. The SqlDataReader can be consumed within the stored procedure. Managed routines return results to the caller using a static instance of SqlPipe available within the SqlContext class and the same is implemented by the table valued function which allows the SQL Server to retrieve the results using the ExecuteAndSend() to send the results to the invoker of the stored procedure.

Table valued functions (TVF) are also enabled by CLR support when written in managed languages. Tabular results are returned. A significant feature is the capability to stream the results produced. The managed TVFs return a standard IEnumerable interface. The IEnumerator object provided by this interface can be retrieved by the query processor and is opaque to SQL Server until it is accessed by another method.

The developer has to make up his mind as to what he wishes to use—T-SQL or CLR. Composition requirements, the source of data and the need for result sets on the run, or typing requirements will determine the kind of code he will use. For instance if he needs to manipulate the results produced inside a table valued function or a stored procedure table valued functions are better. If the results need to streamed CLR is a better option. Again external data sources would be better with a CLR based implementation.

Performing Custom Aggregations over Data

Aggregation on data can be done in a number of ways. If the function is not built in, a function can be added. It can be written as a user defined aggregate, a CLR stored procedure or a server side cursor in T-SQL. The kind of solution to be used will be determined by composability requirements, aggregation algorithm and the need for side effects.

User defined types are one of the most important aspects of SQL Server 2005. It extends the scalar type system of the database. Custom date or time data types are good examples of User defined data types that extend the scalar type system. However UDT should not be used to model complex business objects as it is treated as a unit by the SQL Server to which it is opaque. There is also a limitation on size, indexing and the entire value needs to be updated every time any value is updated.

« « Native HTTP Support in SQL Server 2005
SQL Server Management Objects » »

Author Description

Avatar

Editorial Team at Exforsys is a team of IT Consulting and Training team led by Chandra Vennapoosa.

Free Training

RSSSubscribe 392 Followers
  • Popular
  • Recent
  • SQL Server 2005 – Management studio interface Summary Page

    November 23, 2005 - 0 Comment
  • SQL Server 2005 – Mirror Server

    January 11, 2006 - 0 Comment
  • SQL Server Monitoring Tools – Server Profiler

    December 12, 2005 - 0 Comment
  • SQL Server 2005 – Service Broker

    October 17, 2005 - 0 Comment
  • Maintain indexes in a SQL Server 2005 database

    December 20, 2005 - 0 Comment
  • SQL Server 2005 Architecture Overview

    November 23, 2005 - 0 Comment
  • SQL Server 2005 Replication Enhancements

    January 11, 2006 - 0 Comment
  • SQL Server 2005 – Using DDL Triggers

    December 12, 2005 - 0 Comment
  • Using Notification Services in SQL Server 2005

    October 25, 2005 - 0 Comment
  • SQL Server 2005 – Tuning a Database

    December 24, 2005 - 0 Comment
  • SQL Server 2005 – Configuring Replication

    January 11, 2006 - 0 Comment
  • SQL Server 2005 Replication Enhancements

    January 11, 2006 - 0 Comment
  • SQL Server 2005 – Mirror Server

    January 11, 2006 - 0 Comment
  • SQL Server 2005 – Introduction to Data Availability

    January 1, 2006 - 0 Comment
  • SQL Server 2005 – Backing up a Database

    January 1, 2006 - 0 Comment
  • SQL Server 2005 – Using Database Snapshots

    December 26, 2005 - 0 Comment
  • SQL Server 2005 – Disaster Recovery

    December 26, 2005 - 0 Comment
  • SQL Server 2005 – Managing Certificates

    December 26, 2005 - 0 Comment
  • SQL Server 2005 – Managing Permissions

    December 26, 2005 - 0 Comment
  • Managing SQL Server 2005 Security

    December 24, 2005 - 0 Comment

Exforsys e-Newsletter

ebook
 

Related Articles

  • SQL Server 2005 – Configuring Replication
  • SQL Server 2005 Replication Enhancements
  • SQL Server 2005 – Mirror Server
  • SQL Server 2005 – Introduction to Data Availability
  • SQL Server 2005 – Backing up a Database

Latest Articles

  • Project Management Techniques
  • Product Development Best Practices
  • Importance of Quality Data Management
  • How to Maximize Quality Assurance
  • Utilizing Effective Quality Assurance Strategies
  • Sitemap
  • Privacy Policy
  • DMCA
  • Trademark Information
  • Contact Us
© 2023. All Rights Reserved.IT Training and Consulting
This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish.AcceptReject Read More
Privacy & Cookies Policy

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Non-necessary
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
SAVE & ACCEPT