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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:47:05+00:00 2026-06-13T06:47:05+00:00

I have table designed in SQL-Server 2008 R2. I have a column in that

  • 0

I have table designed in SQL-Server 2008 R2.

I have a column in that table which needs to be checked against another table when data is inserting.

ALTER TABLE Table1
        WITH CHECK ADD CONSTRAINT CK_Code
        CHECK (MyField in (Select Field From Table2))

This cause an error

Sub-queries are not allowed in this context. Only scalar expressions are allowed.

I have looked at this question about Check Constraint – Subqueries are not allowed in this context.

Is there any way of achieving this without using a trigger?

  • 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-13T06:47:07+00:00Added an answer on June 13, 2026 at 6:47 am

    Note, what you really want is a foreign key constraint. That said, to get a “query” into a check you can write a function that contains the query and outputs a scalar value, then use that function in the check constraint.

    CREATE FUNCTION myFunction (
        @field DATATYPE(?)
    )
    RETURNS VARCHAR(5)
    AS
    BEGIN
        IF EXISTS (SELECT* FROM Table2 WHERE MYFIELD = @field)
            return 'True'
        return 'False'
    END
    

    Something like that. Not tested.

    Then you can add it to your check like so

    ALTER TABLE Table1
        WITH CHECK ADD CONSTRAINT CK_Code
        CHECK (myFunction(MYFIELD) = 'True')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have SQL Server 2008 with a table called ProductCategories designed like this: Id
We have a normalized SQL Server 2008 database designed using generic tables. So, instead
I have a SQL Server 2008 database. This database has a Table called Book.
I have a table in a SQL Server database that contains a row I
I have a table in Sql Server that contain an int field price that
I have a data model like this in SQL Server: table Note - varchar
I have table in which I am inserting rows for employee but next time
I have table of data that is sorted as follows: Item | Sample |
I am using Dynamic Data with linq to SQL and SQL Server 2008. I
I have a query in SQL Server that I am trying to convert to

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.