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

  • Home
  • SEARCH
  • 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 6866397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:10:42+00:00 2026-05-27T03:10:42+00:00

I have a problem with a constraint method in sql. This is my table

  • 0

I have a problem with a constraint method in sql.

This is my table

CREATE TABLE [relations].[CompoundKey_Contacts](
    [compoundId] [varchar](32) NOT NULL,
    [companyId] [varchar](32) NULL,
    [personId] [varchar](32) NULL,
    [contactInfoId] [varchar](32) NOT NULL)

When you add a row to this table it should check that this combination of person and company does not already exist in the table. For this I use a constraint function

Constraint

ALTER TABLE [relations].[CompoundKey_Contacts]  WITH NOCHECK ADD  CONSTRAINT [CK_CompoundKey_Contacts] CHECK  (([relations].[doesThisCompoundKeyExist]([personId],[companyId])='NO'))
GO

ALTER TABLE [relations].[CompoundKey_Contacts] CHECK CONSTRAINT [CK_CompoundKey_Contacts]
GO

Function

CREATE function [relations].[doesThisCompoundKeyExist](
    @personId varchar(32),
    @companyId varchar(32)
)
returns varchar(3)
as
begin
    declare @exists varchar(32)

    if(@companyId is null and @personId is null)
      set @exists = 'YES'
    else if(@personId is null)
        if exists(select compoundId from relations.CompoundKey_Contacts where personId is null AND companyId = @companyId)
            set @exists = 'YES' 'This is where to code enters, but it should come to the else and return 'NO'
        else
            set @exists = 'NO'
    else if(@companyId is null)
        if exists(select compoundId from relations.CompoundKey_Contacts where personId = @personId AND companyId is null)
            set @exists = 'YES'
        else
            set @exists = 'NO'
    else if exists(
        select compoundId from relations.CompoundKey_Contacts where personId = @personId AND companyId = @companyId
        )
        set @exists = 'YES'
    else
        set @exists = 'NO'
    return @exists
end;

My insert statement that fails

insert into relations.CompoundKey_Contacts (companyId, contactInfoId, personId, compoundId) values ('COM-000015945', 'INF-000144406', null, 'CPK-000000067');

The problem is this. When I run an insert on the table with a unique insert it still fails. I have of course checked that it rely is unique with a select statement. And here comes the funny part. When I do debug it and check where it fails and break out that code part and run it free without being in a function it behaves as it should so the following code works if its not run in a function

if exists(select compoundId from relations.CompoundKey_Contacts where personId is null AND companyId = 'COM-000015945')
                print 'YES'
            else
                print 'NO' 'Returns NO as it should.

This is the error msg I get

The INSERT statement conflicted with the CHECK constraint "CK_CompoundKey_Contacts". The conflict occurred in database "domas", table "relations.CompoundKey_Contacts".
The statement has been terminated.

I run this on both a Sql Server 2012 and Sql Server ‘DENALI’ CTP3

  • 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-05-27T03:10:43+00:00Added an answer on May 27, 2026 at 3:10 am

    Using a UDF in a check constraint won’t work reliably as you can see

    Use a unique constraint on a computed column if you require extra logic

    ALTER TABLE CompoundKey_Contacts
        ADD CompoundKey AS ISNULL(personID, 'NOPERSONID') + ISNULL(companyId, 'NOCOMPANYID');
    ALTER TABLE CompoundKey_Contacts WITH CHECK
        ADD CONSTRAINT UQ_CompoundKey_Contacts_CompoundKey UNIQUE (CompoundKey);
    

    Or a simple unique constraint

    ALTER TABLE CompoundKey_Contacts WITH CHECK
        ADD CONSTRAINT UQ_CompoundKey_OtherUnique UNIQUE (personID, companyId);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have problem compilin this code..can anyone tell whats wrong with the syntax CREATE
I have a weird problem where after setting nocheck on a foreign constraint and
I'm having a problem with my application and @Max constraint annotation. My controller method
I am getting this error when running a save insert/update command: SEVERE: null java.sql.SQLIntegrityConstraintViolationException:
So, this is not your average 'conditional sort by' question... I have a rather
I have a method that pulls down data from a SQL Server Compact db:
I have problem in some JavaScript that I am writing where the Switch statement
I have problem with return statment >.< I want to store all magazine names
I have problem with starting processes in impersonated context in ASP.NET 2.0. I am
I have problem with ActionLink. I'd like to pass to my ActionLink parameter for

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.