Results 1 to 4 of 4
  1. #1
    Join Date
    Jun 2010
    Posts
    28

    Unanswered: Check Constraint

    Hi All

    Is it possible to have a CHECK constraint on a column that only accept a list of values from, say another table?

    I created a function that retrieves all available international dialling codes that the user can insert into the telephones table and I want to restrict them to only being able to insert these international codes.

    I then call the function within the CHECK statement.

    It doesnt work though. Any Ideas?

    Thanks

    Code:
    CREATE TABLE tbl_telephones
    (
    tel_id INT NOT NULL,
    tel_int_code INT SPARSE NULL,
    tel_number VARCHAR(25) NOT NULL,
    CONSTRAINT pk__tbl__telephones__tel_id PRIMARY KEY CLUSTERED(tel_id),
    CONSTRAINT chk__tbl_telephones__tel_id CHECK(tel_int_code IN(SELECT * FROM dbo.fn_available_int_codes()))
    )
    GO
    Code:
    Msg 1046, Level 15, State 1, Line 8
    Subqueries are not allowed in this context. Only scalar expressions are allowed.

  2. #2
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    what you are thinking of is a foreign key
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  3. #3
    Join Date
    Sep 2011
    Posts
    75
    Hello,

    You can create a foreign key referring to that code column

  4. #4
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    Quote Originally Posted by jassi.singh View Post
    Hello,

    You can create a foreign key referring to that code column
    you miserable copycat, do you not even read threads you post to?

    have a look at the post just before yours and tell me how you are adding anything of value when you repeat what someone else says
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •