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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T21:35:58+00:00 2026-05-20T21:35:58+00:00

Im trying to use Keys and Indexson a database and having difficulty. My requirements

  • 0

Im trying to use Keys and Indexson a database and having difficulty. My requirements are as follows:

  1. I want to be able to have empty string values in the Document No column as well as values
  2. The constraint should check for Unique Values based on a DatabaseID and DocumentNo ( eg you can have the same document no for 2 different database ID’s)

The table is similar to this (Extra columns removed for simplicity)

RecordID (bigint)
DocumentNo (varchar(12))
DatabaseID (bigint)

So the constraint should not allow inserting or updating a record if there is already a document no for the specified document no and database ID. A blank document no should be allowed to be entered as there are multiple rows with no document no.

Is this possible? if so please could you let me know how.

EDIT:
Here is the Query to Create the View and Constraint:

SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
GO
    CREATE VIEW vNoDuplicateDoNos
    WITH SCHEMABINDING
    AS
        SELECT [PODocumentNo],[SageDatabaseID]
        FROM dbo.[Order]
        WHERE [PODocumentNo] <> ''
    GO
    --Create an index on the view.
    CREATE UNIQUE CLUSTERED INDEX CI_V1_ID 
        ON vNoDuplicateDoNos ([PODocumentNo],[SageDatabaseID]);
    GO
  • 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-20T21:35:59+00:00Added an answer on May 20, 2026 at 9:35 pm

    I am not sure about using the GUI for this, but you should use a UNIQUE INDEX with a WHERE condition (assuming you are using SQL Server 2008 or newer):

    http://msdn.microsoft.com/en-us/library/ms188783.aspx

    CREATE UNIQUE NONCLUSTERED INDEX UIX_TableName_DocumentNo_DatabaseID
    ON dbo.TableName
    (DocumentNo, DatabaseID)
    WHERE DocumentNo <> ''
    ON IndexesFileGroup -- omit this line if you do not have a File Group for Indexes
    

    Also, I was not able to find an option for the WHERE condition via the GUI in the “Manage Indexes and Keys” dialog.

    To be fair, this was suggested by “Martin” in a comment on the Question. I just felt it needed to be explicitly stated with an example.

    IF you are using a version of SQL Server prior to 2008 (when Filtered Indexes were added), you can use a Trigger as follows:

    CREATE TRIGGER dbo.TableName_PreventDuplicatesTrigger
    ON dbo.TableName
    AFTER INSERT, UPDATE
    AS
        SET NOCOUNT ON
    
        IF (EXISTS(
                    SELECT  1
                    FROM    dbo.TableName tn
                    INNER JOIN  INSERTED ins
                            ON  ins.DocumentNo = tn.DocumentNo
                            AND ins.DatabaseID = tn.DatabaseID
                    WHERE   ins.DocumentNo <> ''
            ))
        BEGIN
            ROLLBACK TRAN
            RAISERROR('Duplicate DocumentNo/DatabaseID combination detected!', 16, 1)
        END
    GO
    

    The above trigger will look for any existing records that match the two fields but only if the inserted or updated DocumentNo is not empty. If found, it calls ROLLBACK which will cancel the INSERT or UPDATE statement and the RAISERROR will display a message as to what caused the ROLLBACK.

    Another option when using a version of SQL Server prior to 2008 is to create an Indexed View on DocumentNo and DatabaseID while filtering out empty DocumentNo records. This was suggested by “Martin” in the comments to this Answer.

    SET ANSI_NULLS ON
    SET QUOTED_IDENTIFIER ON
    GO
    CREATE VIEW dbo.UniqueDocumentAndDatabase
    WITH SCHEMABINDING
    AS
        SELECT  DocumentNo, DatabaseID
        FROM    dbo.TableName
        WHERE   DocumentNo <> ''
    GO
    
    CREATE UNIQUE CLUSTERED INDEX UIX_UniqueDocumentAndDatabase
    ON dbo.UniqueDocumentAndDatabase
    (DocumentNo, DatabaseID)
    GO
    

    Please note that the two SET options as well as the WITH SCHEMABINDING are required for Indexed Views. It is also required that SET ANSI_NULLS ON was used when creating the base table.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having trouble curling a backup from Heroku. I'm trying to use heroku add:keys
I'm trying to use merge keys with a Rails YAML fixture. I have: BOOK:
I have inherited a medium sized database that we are trying to use with
I'm trying to learn how to use keys and to break the habit of
I am trying use gem tire to search in my application. I have tables
Hi I'm trying use a datepicker on a field I have. I'm trying to
I'm trying to use GetAsyncKeyState(i) on windows7 x64 with C# to get pressed keys.
I'm trying to use foreign keys properly to maintain data integrity. I'm not really
I'm trying to use CommonCrypto to generate keys using PBKDF2 but I can't seem
I am trying to use MAXScript to delete all animation Keys from my scene

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.