Duplicates

  • I need to help with a script;

    I wish for the script to first check 3 different columns and not allow any type of update to the database if there is a match....

  • Let's rephrase that to see if I'm hearing the question correctly.

    Are you looking to simply enforce uniqueness across those three columns? If you simply create a UNIQUE CONSTRAINT on those three fields -

    alter table MyTable

    add CONSTRAINT uc_MyTable unique (col1,col2,col3)

    This will prevent anyone inserting or updating rows to something that would result in duplicates across col1, col2, col3

    ----------------------------------------------------------------------------------
    Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?

  • Matt's solution will cause an error to be thrown whenever the constraint is validated so you will want to catch the error.

  • Msg 1919, Level 16, State 1, Line 1

    Column 'Application_Name' in table 'Vista Application Compatibility' is of a type that is invalid for use as a key column in an index.

    Msg 1750, Level 16, State 0, Line 1

    Could not create constraint. See previous errors.

    what wrong with my columns is the datatype incorrect. I am using text.

  • You cannot index a text datatype. The TEXT datatype is stored as binary data and was intended mainly to store documents, etc.. and is being deprecated. It also needs to be manipulated using special functions (READTEXT, WRITETEXT). So if you don't have to have unlimited storage, get away from the TEXT datatype.

    In SQL Server 2005 you should be using varchar(max). You really should evaluate if you really need that much storage space for a single column though. If you are actually just storing an application name varchar(100) should be more than adequate. Best practice is to limit row width by sizing your columns appropriately.

Viewing 5 posts - 1 through 4 (of 4 total)

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