How to use identity?

  • Hi,

    I want to increment one column like

    BC1, BC2, Bc3, like this......

    Is it possible by IDentity or any other way?

  • You cannot use an IDENTITY column for this. You will have to use a Computed Column.

    DECLARE @tblTable TABLE

    (

    Col1 TINYINT IDENTITY(1,1) NOT NULL,

    Col2 AS 'BC' + CAST( Col1 AS VARCHAR(3) ),

    Col3 INT

    )

    INSERT INTO @tblTable VALUES( 1 )

    INSERT INTO @tblTable VALUES( 2 )

    INSERT INTO @tblTable VALUES( 3 )

    INSERT INTO @tblTable VALUES( 4 )

    INSERT INTO @tblTable VALUES( 5 )

    SELECT * FROM @tblTable


    Kingston Dhasian

    How to post data/code on a forum to get the best help - Jeff Moden
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

  • Wonderful solution Kingston !!!

  • I agree that the solution is fine... but why the need? What is the business requirement for this? I ask for the same reason for the question in the original post... I just want to know. Thanks.

    --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 3 (of 3 total)

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