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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:27:51+00:00 2026-05-26T16:27:51+00:00

I’m using SQL Server 2008. I have a database table that looks like this

  • 0

I’m using SQL Server 2008. I have a database table that looks like this (with unimportant columns ommitted):

CREATE TABLE [dbo].[ImageDocument_FaxProperties](
    [FaxPropertyID] [int] PRIMARY KEY IDENTITY(1,1),
    [Agent] [varchar](25) NULL,
    [ParentImageDocumentId] [uniqueidentifier] NULL
)

I want to create a constraint where the same Agent can have multiple rows as long as the ParentImageDocumentIds are the same on each row, but an Agent cannot have rows with different ParentImageDocumentIds. I understand this isn’t a good table structure, but it’s legacy & I’m not allowed to change it. NULL ParentImageDocumentIds should be considered different.

For example:

PK   Agent      ParentImageDocumentId  -This is ok
#    person1    {D09C3900-0300}        {.. other columns ..}
#    person1    {D09C3900-0300}        {.. other columns ..}

PK   Agent      ParentImageDocumentId  -Check constraint prevents 2nd row insertion
#    person1    NULL                   {.. other columns ..}
#    person1    NULL                   {.. other columns ..}

PK   Agent      ParentImageDocumentId  -Check constraint prevents 2nd row insertion
#    person1    NULL                   {.. other columns ..}
#    person1    {A13E5B21-93DE}        {.. other columns ..}

PK   Agent      ParentImageDocumentId  -Check constraint prevents 2nd row insertion
#    person1    {D09C3900-0300}        {.. other columns ..}
#    person1    {A13E5B21-93DE}        {.. other columns ..}

I’m wondering what’s the best way to write a constraint for this. A unique index on Agent won’t work, because they can sometimes have both. A unique on Agent, ParentImageDocumentId would allow them to have different GUIDs, which is not okay. A filtered index with “WHERE ParentImageDocumentId IS NULL AND Agent IS NOT NULL” will work to prevent double NULLs, but not different GUIDs or GUID & NULL.

Below are two working solutions, however I’m wondering if there’s a better way. An indexed schemabound view allows me to create a more complicated filtered index. An alternative was a table-level check constraint using a function, this should work, but adding the check constraint is SUPER SLOW. I’m guessing it’s re-running my function for every row in the table, but that should be unnecessary for adding a table-level constraint since the function is not checking a specific row. Is there a way around this? I’m leaning towards the indexed view, but wondered if there are alternatives (aside from change my table structure) & which alternative is the best.

Solution #1:

CREATE VIEW ImageDocument_FaxProperties_Assignments WITH SCHEMABINDING
AS
    SELECT Agent, ParentImageDocumentId, COUNT_BIG(*) as numPages FROM dbo.ImageDocument_FaxProperties
    WHERE Status IN ( 'PROC', 'LINKING' )
    AND Agent IS NOT NULL
    GROUP BY Agent, ParentImageDocumentId
GO

CREATE UNIQUE CLUSTERED INDEX [IDX_ImageDocument_FaxProperties_Assignments_Unique] ON [ImageDocument_FaxProperties_Assignments] (Agent)
GO

Solution #2:

CREATE FUNCTION ImageDocument_FaxProperties_Assignments_CheckConstraint() RETURNS BIT
AS
BEGIN
    DECLARE @Result BIT = 0;
    WITH Assignments AS
    (SELECT Agent, DENSE_RANK() OVER (PARTITION BY Agent ORDER BY ParentImageDocumentId) AS assignmentNum
    FROM dbo.ImageDocument_FaxProperties
    WHERE Status IN ( 'PROC', 'LINKING' )
    AND Agent IS NOT NULL
    )

    SELECT @Result = 1 FROM Assignments WHERE assignmentNum > 1

    RETURN @Result
END
GO
ALTER TABLE [ImageDocument_FaxProperties]
ADD CONSTRAINT ImageDocument_FaxProperties_Assignments_Unique CHECK (dbo.ImageDocument_FaxProperties_Assignments_CheckConstraint() = 0)
  • 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-26T16:27:52+00:00Added an answer on May 26, 2026 at 4:27 pm

    The indexed view will be the better option of the two you propose.

    This kind of logic in UDFs is inefficient (it is evaluated for each row) and difficult to get right. See for example the following posts

    • Scalar UDFs wrapped in CHECK constraints are very slow and may fail for multirow updates
    • Snapshot isolation: A threat for integrity? (Part 4)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a reasonable size flat file database of text documents mostly saved in
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I'm making a simple page using Google Maps API 3. My first. One marker

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.