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 9236939
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:25:15+00:00 2026-06-18T07:25:15+00:00

I want to add a check constraint to an already-existing table which does a

  • 0

I want to add a check constraint to an already-existing table which does a check based upon a SELECT COUNT(*) statement. I know my SELECT statement but when I have created the DROP AND CREATE TO script I am unsure where to add my code and what format it should take?

Is there a way to do this in the GUI, or could someone show me an example of a CREATE table statement with a CHECK constraint, considering of a SELECT COUNT(*)?

I know that a CHECK constraint just has to evaluate to a boolean from here: http://msdn.microsoft.com/en-gb/library/ms188258(v=sql.105).aspx

  • 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. Editorial Team
    Editorial Team
    2026-06-18T07:25:16+00:00Added an answer on June 18, 2026 at 7:25 am

    The first thing of note is that you cannot directly use sub-queries in check constraints, but creating a function is the way around that. This will help you:

    CREATE FUNCTION dbo.fnRedRows()
    RETURNS INT
    AS
    BEGIN
        DECLARE @Return INT
        SELECT @Return=COUNT(*) FROM dbo.Red
        RETURN @Return
    END
    GO
    CREATE TABLE dbo.Red 
    (
        id INT IDENTITY(1,1), 
        test VARCHAR(max), 
        CONSTRAINT CK_MaxRows CHECK (dbo.fnRedRows()<5)
    )
    GO
    INSERT INTO dbo.Red (test) VALUES ('HI')
    INSERT INTO dbo.Red (test) VALUES ('The')
    INSERT INTO dbo.Red (test) VALUES ('first four')
    INSERT INTO dbo.Red (test) VALUES ('will work')
    INSERT INTO dbo.Red (test) VALUES ('This one will fail')
    GO
    DROP TABLE dbo.Red
    GO
    DROP FUNCTION dbo.fnRedRows
    GO
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table with which I want to add a check constraint after
I want to add a feature to my registration form which I will check
I have two already-existing tables which look (in part) roughly like this: CREATE TABLE
We want to add a constraint in a Oracle database to check if the
I want to add a check constraint that checks whether a field has a
I want add my custom sidebar next right column all page. Please check this
I want to add a background color to the container(div.checkbox_db), when the checkbox(input.check-group) is
Hi I want to add an if statement that checks to see if a
I have a fairly simple check constraint, where I want to force two columns
I want to select two columns in my table and make them unique but

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.