Kerala Microsoft Users Group

how to generate id in a specified format in sql.

Latest post 06-01-2009 6:52 AM by NinethSense. 13 replies.
  • 05-27-2009 4:52 AM

    how to generate id in a specified format in sql.

    how to generete id in a specified format in sql database table,other than auto generated ids?

     

    What I hear, I forget. What I see, I remember. What I do, I understand. - Confucius

     

    Anish Varghese

    http://anishmarokey.blogspot.com/

     

    • Post Points: 65
  • 05-27-2009 5:10 AM In reply to

    Re: how to generate id in a specified format in sql.

    globally unique Identifier (GUID)

    Declare @GUID as uniqueidentifier

    SELECT @GUID = newid()

    Hear and forget. See and remember. Do and understand

    • Post Points: 25
  • 05-27-2009 5:18 AM In reply to

    Re: how to generate id in a specified format in sql.

    if i want to create like book1, book2 etc

     

    What I hear, I forget. What I see, I remember. What I do, I understand. - Confucius

     

    Anish Varghese

    http://anishmarokey.blogspot.com/

     

    • Post Points: 25
  • 05-27-2009 6:03 AM In reply to

    Re: how to generate id in a specified format in sql.

    May be a trigger?

    Praveen V Nair, PMP
    Tech Blog | PM Blog

    • Post Points: 5
  • 05-27-2009 6:13 AM In reply to

    Re: how to generate id in a specified format in sql.

    A wired try here.

    INSERT INTO test (name) SELECT 'Book' + CONVERT(VARCHAR, MAX(id+1)) FROM test 
    

    I hate this anyway. Performance as well as concurrency reasons.

    Praveen V Nair, PMP
    Tech Blog | PM Blog

    • Post Points: 25
  • 05-27-2009 7:24 AM In reply to

    Re: how to generate id in a specified format in sql.

    Create a Tabel with a Column  LastValue.

    Create a Trigger On_INSERT and Increment the LastValue of the Table.

    use this  Value as   SELECT 'book' + @LastValue

    Hear and forget. See and remember. Do and understand

    • Post Points: 5
  • 05-28-2009 2:51 AM In reply to

    Re: how to generate id in a specified format in sql.

     

    Hi Anish,

    check out the following link.

    http://www.sqlteam.com/article/custom-auto-generated-sequences-with-sql-server

    Hope this helps.

     

    Smitha

    • Post Points: 25
  • 05-28-2009 3:13 AM In reply to

    Re: how to generate id in a specified format in sql.

    What about creating an Identity column and concating it with the text? Or I will prefer Praveen's way, but concurrency will be an issue there.

    Thanks

    Anuraj P
    http://www.dotnetthoughts.net

    THIS POSTING IS PROVIDED "AS IS" WITH NO WARRANTIES, AND CONFERS NO RIGHTS.
    BEWARE OF BUGS IN THE ABOVE CODE; I HAVE ONLY PROVED IT CORRECT, NOT TRIED IT.

    • Post Points: 25
  • 05-28-2009 3:44 AM In reply to

    Re: how to generate id in a specified format in sql.

    Better use identity column only and when you want to display data use "Book"+id. Like:

    SELECT 'Book' + CONVERT(VARCHAR, id) FROM test

    Well all depends on your requirements.

    Praveen V Nair, PMP
    Tech Blog | PM Blog

    • Post Points: 25
  • 05-28-2009 4:36 AM In reply to

    Re: how to generate id in a specified format in sql.

    thanks all.i got it.

     

    What I hear, I forget. What I see, I remember. What I do, I understand. - Confucius

     

    Anish Varghese

    http://anishmarokey.blogspot.com/

     

    • Post Points: 5
  • 06-01-2009 3:18 AM In reply to

    Re: how to generate id in a specified format in sql.

    ET IDENTITY_INSERT ON Place this statement before your insert statement, it allows you to enter specific id's into an identity seed column. Place this line immediately after your insert statement. SET IDENTITY_INSERT

    OFF New id's will continue to be generated for new inserts. I am assuming there will be a one time insert of existing customers and equipment from some other legacy system? If this is going to be a bolt on to an existing system, then you don't need to generate new id's they are already being generated so you just need a unique index.

    Regards

    FELIX

    • Post Points: 25
  • 06-01-2009 5:34 AM In reply to

    Re: how to generate id in a specified format in sql.

    @Felix

    If you meant @@IDENTITY, it works only after an INSERT operation. If we use this, it will be a double work as we will have to do INSERT first, then do an UPDATE.

    We usually look for less overhead methods.

    Wll, I am not sure whether I got correctly what you meant. Sorry.

    Praveen V Nair, PMP
    Tech Blog | PM Blog

    • Post Points: 25
  • 06-01-2009 6:17 AM In reply to

    Re: how to generate id in a specified format in sql.

    Hi Felix

    You have copied the test from this link. Please post source when you copy from other sites.

    http://www.daniweb.com/forums/thread93042.html#

    • Post Points: 25
  • 06-01-2009 6:52 AM In reply to

    Re: how to generate id in a specified format in sql.

    oh man...

    @Felix: as Shoban said, always publish the source if you copied it from somewhere. Otherwise is is illegal. Read this: Plagiarism

    Praveen V Nair, PMP
    Tech Blog | PM Blog

    • Post Points: 5
Page 1 of 1 (14 items) | RSS