what t-sql command assigns and returns a unique id simultaneously?

  • I am trying to implement a file management system for images, but also for other data types.

    I want to store the files in the file system and store the files information: size, original file name, timestamp of upload, userid, and the address to the files location, as well as other information.

    Part of the process requires asigning a unique id. I ran across a t-sql command awhile back and can't remember what it was. essentially it simultanious grabbed a unique identifier from the database and returned it in one call so that it could be used to give the file a unique file name so that it would not overwrite another preexisting file.

    Anyone know what the command was or anything that would do similar. I am looking for any kind of resource or documentation.

    I appreciate any help!

  • Select @GUID = NewID()

  • You could also execute "SELECT @@IDENTITY" on the connection immediately after the insert to get the unique identifier if it is an identity field.

  • I'd never use @@Identity because it is not scope sensitive and, in the presence of triggers, will surely return an incorrect value.  Always use SCOPE_IDENTITY for such a thing.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

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

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