Results 1 to 3 of 3
  1. #1
    Join Date
    Oct 2004
    Location
    Durban - South Africa
    Posts
    13

    Unhappy Unanswered: create a unique Random integer

    Hi there,
    I am trying to create a UID that is unique within my SQL Server. There are many users accessing the Server in seperate databases, but then I want to combine all the data from these tables, keeping the ID from each one as a primary key.

    I have written the following function, but when i call it as a default value for a field, it does not produce a unique number.

    Code:
     CREATE FUNCTION GETNEXTID(@CURDATE DATETIME)
    RETURNS BIGINT
    AS
    BEGIN
    RETURN (SELECT CAST(
    CAST(DATEPART(YY,@CURDATE) AS VARCHAR) +
    RIGHT('0' + CAST(DATEPART(M,@CURDATE) AS VARCHAR),2) +
    RIGHT('0' + CAST(DATEPART(D,@CURDATE) AS VARCHAR),2) +
    RIGHT('0' + CAST(DATEPART(HH,@CURDATE) AS VARCHAR),2) +
    RIGHT('0' + CAST(DATEPART(SS,@CURDATE) AS VARCHAR),2) +
    RIGHT('00' + CAST(DATEPART(MS,@CURDATE) AS VARCHAR),3) AS BIGINT))
    END
    Can anyone help?
    TrevorW
    If at first you don't succeed, call it version 1.0

  2. #2
    Join Date
    Oct 2003
    Posts
    60
    I would create a table as follows:

    Create Table DatabaseId
    (
    DBId Int Identity,
    DBName vachar (40) NOT NULL
    )

    When you import data from the different databases, prefix your unique identifier with DBId. That should give you an unique number across the DB's.
    jaraba

  3. #3
    Join Date
    Sep 2003
    Posts
    364
    Take a look at newid()

Posting Permissions

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