Getting a Unique Field Value

Results 1 to 3 of 3

Thread: Getting a Unique Field Value

  1. #1
    Join Date
    Dec 1969

    Default Getting a Unique Field Value

    What are some good methods for getting Unique values for fields in MS SQL?<BR><BR>1. First, how do I generate a number that I can be assured is unique when it is written to the database?<BR><BR>2. If I allow the user to edit this value, what code can I use to be sure that they don&#039;t change it to a value for a record that already exists. For instance, if I have record ID 1, 2 and 3, the user edits 3 and tries to change it to 1, but 1 already exists. I can&#039;t use a "SELECT COUNT(*) WHERE ID=1" method, because the record ID 1 will return a hit (what if I were actually editing 1. I would get the same results). It may or may not be the one that I&#039;m trying to edit.<BR><BR>Remember, I am working with MS SQL Data, NOT Access. I know that Access has a data type called AutoNumber that will handle this for me, and it is great. But, I don&#039;t have this available in SQL.

  2. #2
    Join Date
    Dec 1969

    Default @@IDENTITY in SQL Server... essentially the same thing.<BR><BR>Look it up in the TSQL docs.<BR><BR>*WHY* do you want to allow editing of the value??? SHUDDER. That would scare the daylights out of me!<BR><BR>If you want to give people an aribtrary login name and/or password, keep that separate from the counter-based ID.<BR><BR>

  3. #3
    jamar Guest

    Default RE: Getting a Unique Field Value

    Answers:<BR><BR>1. The field&#039;s datatype should either be an integer, tinyint,<BR>smallint, etc. AND the Identity property should be true<BR>(keyword if you&#039;re using ddl or just check the box if you&#039;re creating the table through ent. mgr.)<BR><BR>2. You should not even bother to allow the user to edit this field. <BR><BR>3. I&#039;ve posted a good example using @@identity within the past two weeks, if you need more specifics than tsql help, etc. let me know and i&#039;ll repost it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts