CASE Statement in Check constraint?

  • Is there a way to put a CASE Statement in a table check constraint?

    For example, on the table:

    CREATE TABLE dbo.roysched

    (

        title_id [tid] NOT NULL,

        lorange  int   NULL,

        hirange  int   NULL,

        royalty  int   NULL,

    CONSTRAINT FK__roysched__title___0DAF0CB0

        FOREIGN KEY (title_id)

        REFERENCES dbo.titles (title_id)

    )

    I would like to do something like this:

    ALTER TABLE roysched

    ADD CONSTRAINT CK_title_id

    CHECK (CASE royalty WHEN 10 THEN title_id = 'BU2075')

    The intended effect being that the only title_id that can have a royalty of 10 is 'BU2075'.

    Thanks

    Noel

  • Try this

    ALTER TABLE roysched

    ADD CONSTRAINT CK_title_id

    CHECK (CASE royalty WHEN 10 THEN 'BU2075' ELSE title_id  END = title_id  )


    Simon Sabin
    SQL Server MVP

    http://sqlblogcasts.com/blogs/simons

  • That's the ticket! Thank you!

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply