Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 178859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T14:18:18+00:00 2026-05-11T14:18:18+00:00

I’m implementing ‘check’ constraints that simply call a CLR function for each constrained column.

  • 0

I’m implementing ‘check’ constraints that simply call a CLR function for each constrained column.

Each CLR function is one or two lines of code that attempts to construct an instance of the user-defined C# data class associated with that column. For example, a ‘Score’ class has a constructor which throws a meaningful error message when construction fails (i.e. when the score is outside a valid range).

First, what do you think of that approach? For me, it centralizes my data types in C#, making them available throughout my application, while also enforcing the same constraints within the database, so it prevents invalid manual edits in management studio that non-programmers may try to make. It’s working well so far, although updating the assembly causes constraints to be disabled, requiring a recheck of all constraints (which is perfectly reasonable). I use DBCC CHECKCONSTRAINTS WITH ALL_CONSTRAINTS to make sure the data in all tables is still valid for enabled and disabled constraints, making corrections as necessary, until there are no errors. Then I re-enable the constraints on all the tables via ALTER TABLE [tablename] WITH CHECK CHECK CONSTRAINT ALL. Is there a T-SQL statement to re-enable with check all check constraints on ALL tables, or do I have to re-enable them table by table?

Finally, for the CLR functions used in the check constraints, I can either:

  1. Include a try/catch in each function to catch data construction errors, returning false on error, and true on success, so that the CLR doesn’t raise an error in the database engine, or…
  2. Leave out the try/catch, just construct the instance and return true, allowing that aforementioned ‘meaningful’ error message to be raised in the database engine.

I prefer 2, because my functions are simpler without the error code, and when someone using management studio makes an invalid column edit, they’ll get the meaningful message from the CLR like 'Value for type X didn't match regular expression '^p[1-9]\d?$'' instead of some generic SQL error like ‘constraint violated’. Are there any severe negative consequences of allowing CLR errors through to SQL Server, or is it just like any other insert/update failure resulting from a constraint violation?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T14:18:18+00:00Added an answer on May 11, 2026 at 2:18 pm

    For example, a "Score" class has a constructor which throws a meaningful error message when construction fails (i.e. when the score is outside a valid range). First, what do you think of that approach?

    It worries me a bit, because calling a ctor requires memory allocation, which is relatively expensive. For each row inserted, you’re calling a ctor — and only for its side-effects.

    Also expensive are exceptions. They’re great when you need them, but this is a case where you vould use them in a ctor context, but not in a check context.

    A refactoring could reduce both costs, by having the check exist as a class static or free function, then both the check constraint and the ctor could call that:

    class Score {    private:    int score;    public:    static bool valid( int score ) {      return score > 0  ;     }       Score( int s ) {     if( ! valid( s ) ) {        throw InvalidParameter();     }      score = s;    } }    

    Check constraint calls Score::valid(), no construction or exception needed.

    Of course, you still have the overhead, for each row, of a CLR call. Whether that’s acceptable is something you’ll have to decide.

    Is there a T-SQL statement to re-enable with check all check constraints on ALL tables, or do I have to re-enable them table by table?

    No, but you can do this to generate the commands:

    select 'ALTER TABLE ' || name || ' WITH CHECK CHECK CONSTRAINT ALL;'  from sys.tables ; 

    and then run the resultset against the database.

    Comments from the OP:

    I use base classes called ConstrainedNumber and RegexConstrainedString for all my data types. I could easily move those two classes’ simple constructor code to a separate public boolean IsValueValid method as you suggested, and probably will.

    The CLR overhead (and memory allocation) would only occur for inserts and updates. Given the simplicity of the methods, and rate at which table updates will occur, I don’t think the performance impact will anything to worry about for my system.

    I still really want to raise exceptions for the information they’ll provide to management studio users. I like the IsValueValid method, because it gives me the ‘option’ of not throwing errors. Within applications using my data types, I could still get the exception by constructing an instance 🙂

    I’m not sure I agree with the exception throwing, but again, the "take-home message" is that by decomposing the problem into parts, you can select what parts you’re wiling to pay for, without paying for parts you don’t use. The ctor you don’t use, because you were only calling it to get the side-effect. So we decomposed creation and checking. We can further decompose throwing:

    class Score {    private:    int score;    public:    static bool IsValid( int score ) {      return score > 0  ;     }     static checkValid( int score ) {     if( ! isValid( s ) ) {        throw InvalidParameter();     }        Score( int s ) {     checkValid( s ) ;         score = s;    } }    

    Now a user can call the ctor, and get the check and possible exception and construction, call checkValid and get the check and exception, or isValid to just get the validity, paying the runtime cost for only what he needs.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 117k
  • Answers 118k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer class Program { static void Main(string[] args) { Timer timer… May 11, 2026 at 10:50 pm
  • Editorial Team
    Editorial Team added an answer Use GROUP_CONCAT SELECT GROUP_CONCAT(bar) FROM TABLE GROUP BY foo; May 11, 2026 at 10:50 pm
  • Editorial Team
    Editorial Team added an answer Your code doesn't work because the function is not returning… May 11, 2026 at 10:50 pm

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.